Search code examples
laravelpaypal

PayPal integration failure with Laravel using v2 PHP SDK


I'm tryng to integrate simple payments through PayPal with a sandbox Business account. I found the PayPal-PHP-SDK for v1/payments is deprecated so I just wanted to test the newer v2 SDK with Laravel.

While trying to test the demo PayPal, I got more than one error like First one is net::ERR_CONNECTION_TIMED_OUT that's happen without any event on the paypal button When i click on paypal button i found this error Create_order_error {err: "TypeError: Failed to execute 'json' on 'Response': Also this error Uncaught (in promise) SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON Js?client-id=test&currency=USD:2 Uncaught TypeError: Failed to execute 'json' on 'Response': body stream Already read click_initiate_payment_reject {err: "TypeError: Failed to execute 'json' on 'Response':…al.com/

So I installed this

 composer require paypal/paypal-payouts-sdk ~1.0.0

dependent on this repository :

 https://github.com/paypal/Payouts-PHP-SDK

But I don't know if I'm using the right package... this is the code from GitHub:

   $request = new PayoutsPostRequest();
      $body= json_decode(
                '{
                    "sender_batch_header":
                    {
                      "email_subject": "SDK payouts test txn"
                    },
                    "items": [
                    {
                      "recipient_type": "EMAIL",
                      "receiver": "smyddddddddddddt@business.example.com",
                      "note": "Your 160 payout",
                      "sender_item_id": "Test_txn_12",
                      "amount":
                      {
                        "currency": "EUR",
                        "value": "160.00"
                      }
                    }]
                  }',             
                true);
      $request->body = $body;
      $en = new SandboxEnvironment($clientId, $clientSecret);
      $client = new PayPalHttpClient($en);
      $response = $client->execute($request);

i don't don't know if i'm using the right paypal button like this

    <!-- Set up a container element for the button -->
     <div id="paypal-button-container"></div>

    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>

    <script>
        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({

            // Call your server to set up the transaction
            createOrder: function(data, actions) {
                return fetch('{{route("create-demo")}}', {
                    method: 'post'
                }).then(function(res) {
                    console.log(res.json());
                    return res.json();
                }).then(function(orderData) {
                    return orderData.orderID;
                });
            },

            // Call your server to finalize the transaction
            onApprove: function(data, actions) {
                return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
                    method: 'post'
                }).then(function(res) {
                    return res.json();
                }).then(function(orderData) {

So if someone can help with details I would love if you can share a good resource

-----Edited-----

To send request using guzzle but i got this error
400 Bad Request response: {"name":"INVALID_REQUEST","message":"Request is not well-formed,

this is the code :

 try {
        
        $client = new Client();
        $req = $client->request('POST', $uri, [
            'headers' => [
                'Accept' => 'application/json',
                'Accept-Language' => 'en_US',
                //'Content-Type' => 'application/x-www-form-urlencoded',
                'Authorization' => 'Basic <'.$clientId.':'.$clientSecret.'>',
                'PayPal-Request-Id' => '7b92603e-77ed-4896-8e78-5dea2050476a'
            ],
            'auth' => [$clientId, $clientSecret, 'basic'],
            'json' => ['grant_type' => 'client_credentials'],
            'form_params' => [
                'intent' => 'CAPTURE',
                'grant_type' => 'client_credentials',
                'purchase_units' => [               
                    [
                        "reference_id" => "d9f80740-38f0-11e8-b467-0ed5f89f718b",
                        'amount' => [
                            'currency_code' => 'USD',
                            'value' => '100.00'
                        ]
                    ]
                ],
                "payment_source" => [
                    "paypal" => [
                        "experience_context" => [
                            "payment_method_preference" => "IMMEDIATE_PAYMENT_REQUIRED",
                            "payment_method_selected" => "PAYPAL",
                            "brand_name" => "EXAMPLE INC",
                            "locale" => "en-US",
                            "landing_page" => "LOGIN",
                            "shipping_preference" => "SET_PROVIDED_ADDRESS",
                            "user_action" => "PAY_NOW",
                            "return_url" => "http://127.0.0.1:8000/paypal/success",
                            "cancel_url" => "http://127.0.0.1:8000/paypal/cancelled",
                        ]
                    ]
                ]
            ]

        ]);
        $response = $client->send($req);
        return $response;
    }catch(\Exception $e){
        return $e;
    }

Solution

  • The Payouts-PHP-SDK is for sending money, not a PayPal Checkout integration.

    You can use the Checkout-PHP-SDK to abstract Orders v2 calls, although it was also deprecated recently and won't be maintained. The officially supported alternative is to do your own HTTPS calls (using curl or similar) to first obtain an access_token and then do each call.

    The frontend approval JS in your question seems cut off/incomplete, but looks correct as far as it goes; this is the demo site to reference