Pick up items from shopping cart with PHP

Asked

Viewed 289 times

1

I have this code that makes a transparent purchase of Pagseguro.

$params = array(
            'email'                     => $PAGSEGURO_EMAIL,  
            'token'                     => $PAGSEGURO_TOKEN,
            'creditCardToken'           => $creditCardToken,
            'senderHash'                => $senderHash,
            'receiverEmail'             => $PAGSEGURO_EMAIL,
            'paymentMode'               => 'default', 
            'paymentMethod'             => 'creditCard', 
            'currency'                  => 'BRL',
            // 'extraAmount'               => '1.00',

            'itemId1'                   => '0001',
            'itemDescription1'          => 'PHP Test',  
            'itemAmount1'               => $numero,  
            'itemQuantity1'             => 1,

            'itemId2'                   => '0002',
            'itemDescription2'          => 'PHP Test2',  
            'itemAmount2'               => $numero,  
            'itemQuantity2'             => 1,

            'reference'                 => 'REF1234',


            'senderName'                => $senderName,
            'senderCPF'                 => $senderCPF,
            'senderAreaCode'            => 83,
            'senderPhone'               => $senderPhone,
            'senderEmail'               => $senderEmail,
            'shippingAddressStreet'     => $shippingAddressStreet,
            'shippingAddressNumber'     => $shippingAddressNumber,
            'shippingAddressDistrict'   => $shippingAddressDistrict,
            'shippingAddressPostalCode' => $shippingAddressPostalCode,
            'shippingAddressCity'       => $shippingAddressCity,
            'shippingAddressState'      => $shippingAddressState,
            'shippingAddressCountry'    => 'BRA',
            'shippingType'              => 1,
            'shippingCost'              => $shippingCost,
            'installmentQuantity'       => 1,
            'installmentValue'          => $installmentValue,
            'creditCardHolderName'      => 'Chuck Norris',
            'creditCardHolderCPF'       => '54793120652',
            'creditCardHolderBirthDate' => '01/01/1990',
            'creditCardHolderAreaCode'  => 83,
            'creditCardHolderPhone'     => '999999999',
            'billingAddressStreet'     => 'Address',
            'billingAddressNumber'     => '1234',
            'billingAddressDistrict'   => 'Bairro',
            'billingAddressPostalCode' => '58075000',
            'billingAddressCity'       => 'João Pessoa',
            'billingAddressState'      => 'PB',
            'billingAddressCountry'    => 'BRA'
        );

How do I get PHP to catch dynamically the cart items and place inside the array $params[]? So that all products of the trolley can be sent?

This part takes the products to be generated within the array by PHP:

            'itemId1'                   => '0001',
            'itemDescription1'          => 'PHP Test',  
            'itemAmount1'               => $numero,  
            'itemQuantity1'             => 1,

            'itemId2'                   => '0002',
            'itemDescription2'          => 'PHP Test2',  
            'itemAmount2'               => $numero,  
            'itemQuantity2'             => 1,

Would look like this?

$params = array(



        'email'                     => $PAGSEGURO_EMAIL,  
        'token'                     => $PAGSEGURO_TOKEN,
        'creditCardToken'           => $creditCardToken,
        'senderHash'                => $senderHash,
        'receiverEmail'             => $PAGSEGURO_EMAIL,
        'paymentMode'               => 'default', 
        'paymentMethod'             => 'creditCard', 
        'currency'                  => 'BRL',
        // 'extraAmount'               => '1.00',

        'reference'                 => 'REF1234',    

        'senderName'                => $senderName,
        'senderCPF'                 => $senderCPF,
        'senderAreaCode'            => 83,
        'senderPhone'               => $senderPhone,
        'senderEmail'               => $senderEmail,
        'shippingAddressStreet'     => $shippingAddressStreet,
        'shippingAddressNumber'     => $shippingAddressNumber,
        'shippingAddressDistrict'   => $shippingAddressDistrict,
        'shippingAddressPostalCode' => $shippingAddressPostalCode,
        'shippingAddressCity'       => $shippingAddressCity,
        'shippingAddressState'      => $shippingAddressState,
        'shippingAddressCountry'    => 'BRA',
        'shippingType'              => 1,
        'shippingCost'              => '1.00',
        'installmentQuantity'       => 1,
        'installmentValue'          => '3.00',
        'creditCardHolderName'      => 'Chuck Norris',
        'creditCardHolderCPF'       => '54793120652',
        'creditCardHolderBirthDate' => '01/01/1990',
        'creditCardHolderAreaCode'  => 83,
        'creditCardHolderPhone'     => '999999999',
        'billingAddressStreet'     => 'Address',
        'billingAddressNumber'     => '1234',
        'billingAddressDistrict'   => 'Bairro',
        'billingAddressPostalCode' => '58075000',
        'billingAddressCity'       => 'João Pessoa',
        'billingAddressState'      => 'PB',
        'billingAddressCountry'    => 'BRA'
    );


$filter = preg_grep('/^itemId([0-9]{1,})/', array_keys( $params ));
    $result = array();
    for($i = 0; $i < count($filter); $i++)
    {

        $result['itemId' . ($i + 1)] = $params['itemId' . ($i + 1)];
        $result['itemDescription' . ($i + 1)] = $params['itemDescription' . ($i + 1)];
        $result['itemAmount' . ($i + 1)] = $params['itemAmount' . ($i + 1)];
        $result['itemQuantity' . ($i + 1)] = $params['itemQuantity' . ($i + 1)];

    }


    $header = array('Content-Type' => 'application/json; charset=UTF-8;');
    $response = curlExec($PAGSEGURO_API_URL."/transactions", $params, $header);
    $json = json_decode(json_encode(simplexml_load_string($response)));
?>
  • would be so what? I answered you what’s in the question take a look?

  • I did what you asked that is to pick up the cart items!

  • How could I send the cart items to Pagseguro? $header = array('Content-Type' => 'application/json; charset=UTF-8;'); $Response = curlExec($PAGSEGURO_API_URL."/transactions", $params, $header); $json_decode(json_encode(simplexml_load_string($Response)));

  • And then the solution worked out.?

  • it did work :)

  • Because you do not score the questions and accept as an answer, your user has many questions open! because?

  • I don’t know how I do it, can you tell me?

Show 3 more comments

1 answer

1


To search for specific fields you first need to find out how many items this has array which characterize the part of the keys, for example, the code first checks with preg_grep how many items have the key itemId independent of the numbering, and with that amount of itemId repeated makes a for by quantity and generates a new array with the results, example:

<?php

    $params = array(
                'email'                     => $PAGSEGURO_EMAIL,  
                'token'                     => $PAGSEGURO_TOKEN,
                'creditCardToken'           => $creditCardToken,
                'senderHash'                => $senderHash,
                'receiverEmail'             => $PAGSEGURO_EMAIL,
                'paymentMode'               => 'default', 
                'paymentMethod'             => 'creditCard', 
                'currency'                  => 'BRL',
                'extraAmount'               => '1.00',

                'itemId1'                   => '0001',
                'itemDescription1'          => 'PHP Test',  
                'itemAmount1'               => $numero,  
                'itemQuantity1'             => 1,

                'itemId2'                   => '0002',
                'itemDescription2'          => 'PHP Test2',  
                'itemAmount2'               => $numero,  
                'itemQuantity2'             => 1
    );

    $filter = preg_grep('/^itemId([0-9]{1,})/', array_keys( $params ));
    $result = array();
    for($i = 0; $i < count($filter); $i++)
    {

        $result['itemId' . ($i + 1)] = $params['itemId' . ($i + 1)];
        $result['itemDescription' . ($i + 1)] = $params['itemDescription' . ($i + 1)];
        $result['itemAmount' . ($i + 1)] = $params['itemAmount' . ($i + 1)];
        $result['itemQuantity' . ($i + 1)] = $params['itemQuantity' . ($i + 1)];

    }

    var_dump($result);

Online Example

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.