Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I pass a fully formed purchase units array to OrderRequestBuilder #32

Open
zerocarbthirty opened this issue Jan 28, 2025 · 1 comment

Comments

@zerocarbthirty
Copy link

Hello,

Because PayPal broke something in their API our integration stopped working last week. We had been using paypal-checkout-sdk which is old and deprecated but it still worked until last week.

Anyhow I am trying to integrate PayPal-PHP-Server-SDK into our application and I am having the hardest time figuring out how I can just send an entire fully formed purchase_units array to the builder. I cannot really figure out what the builder wants here:

PurchaseUnitRequestBuilder::init(
                                   AmountWithBreakdownBuilder::init(
                                       'USD',
                                       '1.00'
                                   )->build()
                               )->build()
$purchase_units = array("intent" => "CAPTURE",
	"purchase_units" =>
        array(
        	0 => array(
                	"reference_id" => $service,
                        "description" => $ppService,
                        "amount" => array(
                                 "currency_code" => "USD",           
                                 "value" => $ppTotal,
                                 "breakdown" => array(
                                 	"item_total" => array(
                                                    "currency_code" => "USD",
                                                    "value" => $ppdue),
                                        "tax_total" => array(
                                                   "currency_code" => "USD",
                                                    "value" => $ppTax)
                                        ),
                                ),
	                            "items" => array(
	                            	0 => array(
                                    	"name" => $ppService,
                                    	"description" => Item 1,
                                    	"sku" => $service,
                                    	"unit_amount" =>
                                        array(
                                            "currency_code" => "USD",
                                            "value" => $up),
                                     "tax" =>
                                        array(
                                            "currency_code" => "USD",
                                            "value" => $ppTax),
                                    "quantity" => $qty,
                                    "category" => "DIGITAL_GOODS"
                                    )
                                )
                            )
                        ),
                        "application_context" =>
                            array(
                            "cancel_url" => "https://www.widgetsrus.com/step-2",
                            "return_url" => "https://www.widgetsrus.com/step-3",
                            "brand_name" => "Widgets R Us",
                            "shipping_preference" => "NO_SHIPPING",
                            "user_action" => "PAY_NOW"
                        )
                );

We'd really like to just keep using the same purchase_units definition that we've been using for a decade or so.

Any thoughts?

@sohaibtariq
Copy link

@zerocarbthirty Looking at the Documentation for the SDKs, you should be able to do something like this:

<?php

require_once "vendor/autoload.php";

use PayPalRESTAPIsLib\PayPalRESTAPIsClientBuilder;
use PayPalRESTAPIsLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PayPalRESTAPIsLib\Logging\LoggingConfigurationBuilder;
use PayPalRESTAPIsLib\Logging\RequestLoggingConfigurationBuilder;
use PayPalRESTAPIsLib\Logging\ResponseLoggingConfigurationBuilder;
use Psr\Log\LogLevel;
use PayPalRESTAPIsLib\Models\Builders\OrderRequestBuilder;
use PayPalRESTAPIsLib\Models\CheckoutPaymentIntent;
use PayPalRESTAPIsLib\Models\Builders\PurchaseUnitRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountWithBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\ItemBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\ApplicationContextBuilder;
use PayPalRESTAPIsLib\Exceptions\ApiException;
use PayPalRESTAPIsLib\Environment;

// Define your variables
$service = "service_id";
$ppService = "Service Description";
$ppTotal = "100.00";
$ppdue = "80.00";
$ppTax = "20.00";
$up = "100.00";
$qty = "1";

// Create the order request
$orderRequest = OrderRequestBuilder::init(
    CheckoutPaymentIntent::CAPTURE,
    [
        PurchaseUnitRequestBuilder::init(
            AmountWithBreakdownBuilder::init(
                'USD',
                $ppTotal
            )->breakdown(
                AmountBreakdownBuilder::init()
                    ->itemTotal('USD', $ppdue)
                    ->taxTotal('USD', $ppTax)
                    ->build()
            )->build()
        )->referenceId($service)
         ->description($ppService)
         ->items([
             ItemBuilder::init($ppService, 'USD', $up)
                 ->description('Item 1')
                 ->sku($service)
                 ->tax('USD', $ppTax)
                 ->quantity($qty)
                 ->category('DIGITAL_GOODS')
                 ->build()
         ])
         ->build()
    ]
)->applicationContext(
    ApplicationContextBuilder::init()
        ->cancelUrl("https://www.widgetsrus.com/step-2")
        ->returnUrl("https://www.widgetsrus.com/step-3")
        ->brandName("Widgets R Us")
        ->shippingPreference("NO_SHIPPING")
        ->userAction("PAY_NOW")
        ->build()
)->build();

$client = PayPalRESTAPIsClientBuilder::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();

try {
    $createOrderResult = $client->getOrdersController()->ordersCreate(['body' => $orderRequest, 'prefer' => 'return=minimal']);
    $orderId = $createOrderResult->id;
    echo "Order created with ID: $orderId
";
} catch (ApiException $exp) {
    echo "Caught $exp";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants