Skip to content
Advertisement

How to make PayPalCheckout return value by post method?

I am using PayPalCheckoutSdk library following the examples, I have the following:

<?php
    require __DIR__ . '/PayPalCheckout/vendor/autoload.php';

    use PayPalCheckoutSdkCorePayPalHttpClient;
    use PayPalCheckoutSdkCoreSandboxEnvironment;

    $clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    $environment = new SandboxEnvironment($clientId, $clientSecret);
    $client = new PayPalHttpClient($environment);

    $invoiceNumber = uniqid();
    
    $items = array();

    $items[0] = [
        'name' => 'HTML5',
        'description' => 'Video streaming service',
        'type' => 'SERVICE',
        'sku' => 'sku03',
        'unit_amount' =>
            [
                'currency_code' => 'USD',
                'value' => '90.00',
            ],
        'quantity' => '1',
        'category' => 'DIGITAL_GOODS',
    ];
    $new_item = [
        'name' => 'CSS3',
        'description' => 'Video streaming service',
        'type' => 'SERVICE',
        'sku' => 'sku02',
        'unit_amount' =>
            [
                'currency_code' => 'USD',
                'value' => '45.00',
            ],
        'quantity' => '2',
        'category' => 'DIGITAL_GOODS',
    ];

    array_push($items , $new_item);

    use PayPalCheckoutSdkOrdersOrdersCreateRequest;
    $request = new OrdersCreateRequest();

    $request->prefer('return=representation');

    $request->body = [
        'intent' => 'CAPTURE',
        'application_context' => [
            'brand_name' => 'COMPANY',
            'locale' => 'us-US',
            'user_action' => 'PAY_NOW',
            "cancel_url" => "http://localhost/PayPal/cancel.php",
            "return_url" => "http://localhost/PayPal/return.php",
            'landing_page' => 'BILLING',
        ],
        'purchase_units' => [0 => [
            'reference_id' => $invoiceNumber,
            'amount' => [
                'currency_code' => 'USD',
                'value' => '160.00',
                'breakdown' => [
                    'item_total' => [
                        'currency_code' => 'USD',
                        'value' => '180.00',
                    ],
                    'shipping_discount' => [
                        'currency_code' => 'USD',
                        'value' => '20.00',
                    ],
                ],
            ],
            'items' =>
                $items,
        ]],
    ];

    try {
        $response= $client->execute($request);

        if ($response->statusCode == 201){
            for ($i = 0; $i < count($response->result->links); ++$i){
                $link = $response->result->links[$i];
                if ($link->rel =='approve') {
                    header("location: $link->href");
                }
            }
        } else {
            exit(1);
        }
    } catch (HttpException $ex) {
        echo $ex->statusCode;
        print_r($ex->getMessage());
    }
?>

I am receiving the data by get method print_r($_REQUEST);:

Array ( [token] => 3JX899952R0552721 [PayerID] => J95XSJRX4WXVS

And, that information is processed in the file return.php which has the following code: https://ideone.com/ncVjIt

I would like to be able to receive the information but by post method, what configurations should I make so that the data is sent by post and not by get?

Advertisement

Answer

As explained in comments, you can’t change the redirect method back from PayPal. It will always be a GET string appended to your return_url.

However, the ideal and recommended solution is to not use any redirects. At all. Instead, use the PayPal-Checkout-SDK you have to make two routes on your server, one for ‘Create Order’ and one for ‘Capture Order’, documented here, that return only JSON data (no HTML or text). The latter one should (on success) store the payment details in your database before it does the return (particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID)

Pair these two JSON-only routes with the following approval flow that does not use any redirects, and instead keeps your site loaded in the background (lightboxed) at all times during payment approval: https://developer.paypal.com/demo/checkout/#/pattern/server

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement