Pick up shopping cart items with PHP

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 dynamically grab the cart items and put it inside the $ params [] array? Why can not all cart products be shipped?

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

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

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

Would it 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)));
?>
    
asked by anonymous 04.09.2017 / 15:04

1 answer

1

To search for specific fields you must first find out how many items have this array that characterize the part of the keys, for example, the code first checks with preg_grep how many items have the itemId key independent of numbering, and with that amount of itemId repeated it makes a for of the 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);

Example OnLine

04.09.2017 / 18:56