paypal/PayPal-PHP-Server-SDK

using OrdersController::ordersCreate to give a valid response for the PayPal JS SDK's Buttons API / createOrder callback

Closed this issue · 1 comments

In https://developer.paypal.com/demo/checkout/#/pattern/server there's this bit:

       // Call your server to set up the transaction
            createOrder: function(data, actions) {
                return fetch('/demo/checkout/api/paypal/order/create/', {
                    method: 'post'
                }).then(function(res) {
                    return res.json();
                }).then(function(orderData) {
                    return orderData.id;
                });
            },

I'm trying to write a PHP version of the /demo/checkout/api/paypal/order/create/ endpoint and am having some difficulty. Here's what I have so far:

use PaypalServerSdkLib\PaypalServerSdkClientBuilder;
use PaypalServerSdkLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PaypalServerSdkLib\Environment;
use PaypalServerSdkLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSdkLib\Models\CheckoutPaymentIntent;
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSdkLib\Models\Builders\AmountWithBreakdownBuilder;

...

        $client = PaypalServerSdkClientBuilder::init()
        ->clientCredentialsAuthCredentials(
            ClientCredentialsAuthCredentialsBuilder::init(
                'OAuthClientId',
                'OAuthClientSecret'
            )
        )
        ->environment(Environment::SANDBOX)
        /*
        ->loggingConfiguration(
            LoggingConfigurationBuilder::init()
                ->level(LogLevel::INFO)
                ->requestConfiguration(RequestLoggingConfigurationBuilder::init()->body(true))
                ->responseConfiguration(ResponseLoggingConfigurationBuilder::init()->headers(true))
        )
        */
        ->build();
        $ordersController = $client->getOrdersController();
        $collect = [
            'body' => OrderRequestBuilder::init(
                CheckoutPaymentIntent::CAPTURE,
                [
                    PurchaseUnitRequestBuilder::init(
                        AmountWithBreakdownBuilder::init(
                            'USD',
                            49
                        )->build()
                    )->build()
                ]
            )->build(),
            'prefer' => 'return=minimal'
        ];
        $apiResponse = $ordersController->ordersCreate($collect);

My question is... (assuming I'm on anything even resembling the right path) what do I do with $apiResponse? I tried to echo out $apiResponse->getResult() but that got me this error:

Error: Object of class PaypalServerSdkLib\Models\Order could not be converted to string

Any ideas?

Hey @prescriptionlifeline

I think you're on the right path. However the methods on the controllers return a response object, not a single value. If you look here, you'll see that they return an ApiResponse instance. This class has various methods you can call such as getStatusCode, getHeaders, and getResult, which you use to inspect the result and build your handling logic.

I believe you're looking for getResult() which would return an Order on successful requests that would contain the order Id you're after.

Edit: I forgot to add that many of these classes do not implement a toString method so you can't convert them directly to a string representation. Although they should implement a jsonSerialize method which will get you a Json representation of it as a string, which could work in a round-about way depending on your debugging needs.