Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Last active April 25, 2018 16:58
Show Gist options
  • Save a-r-m-i-n/ce808de89aafd51cacf6 to your computer and use it in GitHub Desktop.
Save a-r-m-i-n/ce808de89aafd51cacf6 to your computer and use it in GitHub Desktop.
PayPal Classic API - Express Checkout
{
"require": {
"paypal/merchant-sdk-php": "*"
}
}
<?php
$config = array(
'mode' => 'sandbox', // or 'live'
'acct1.UserName' => 'vendor_api1.example.com',
'acct1.Password' => 'MW1M19EETX3YJX3Y',
'acct1.Signature' => 'BcaYf-Baofqt7oxiopZb8bHnZGVRAuoqoUoMH3iALBfE4mOKgUQO4ciK'
);
This example uses the Merchant PHP SDK of Paypal. To get it, just use composer.
<?php
// paypal/merchant-sdk-php required
require_once('vendor/autoload.php');
require_once('config.php');
$setExpressCheckoutRequestDetails = new \PayPal\EBLBaseComponents\SetExpressCheckoutRequestDetailsType();
$setExpressCheckoutRequestDetails->ReturnURL = 'http://domain.com/step2.php?success=1';
$setExpressCheckoutRequestDetails->CancelURL = 'http://domain.com/step2.php?success=0';
$paymentDetail = new \PayPal\EBLBaseComponents\PaymentDetailsType();
$paymentDetail->OrderTotal = new \PayPal\CoreComponentTypes\BasicAmountType('EUR', 10.00);
$paymentDetail->ItemTotal = new \PayPal\CoreComponentTypes\BasicAmountType('EUR', 5.88);
$paymentDetail->TaxTotal = new \PayPal\CoreComponentTypes\BasicAmountType('EUR', 1.12);
$paymentDetail->ShippingTotal = new \PayPal\CoreComponentTypes\BasicAmountType('EUR', 3.00);
$paymentDetail->PaymentAction = 'Sale';
$paymentDetail->OrderDescription = 'This is a test';
$shippingAddress = new \PayPal\EBLBaseComponents\AddressType();
$shippingAddress->Street1 = 'Testweg 1';
$shippingAddress->CityName = 'Köln';
$shippingAddress->PostalCode = '51109';
$shippingAddress->Country = 'DE';
$paymentDetail->ShipToAddress = $shippingAddress;
$setExpressCheckoutRequestDetails->PaymentDetails = array($paymentDetail);
$expressCheckoutReq = new \PayPal\PayPalAPI\SetExpressCheckoutReq();
$expressCheckoutRequest = new \PayPal\PayPalAPI\SetExpressCheckoutRequestType($setExpressCheckoutRequestDetails);
$expressCheckoutReq->SetExpressCheckoutRequest = $expressCheckoutRequest;
$service = new \PayPal\Service\PayPalAPIInterfaceServiceService($config);
$response = $service->SetExpressCheckout($expressCheckoutReq);
var_dump($response); // Helpful if $response->Token is empty, cause an error occured
// Remove 'sandbox.' for live mode
$redirectUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $response->Token . '&useraction=commit';
// We request in this file a token from Paypal. After that we need to redirect the customer to the URL above.
echo "<a href='$redirectUrl'>$redirectUrl</a>";
<?php
// paypal/merchant-sdk-php required
require_once('vendor/autoload.php');
require_once('config.php');
if ($_GET['success'] != '1') {
echo "Request not successful!";
die;
}
$doExpressCheckoutPaymentReq = new \PayPal\PayPalAPI\DoExpressCheckoutPaymentReq();
$doExpressCheckoutPaymentRequestDetails = new \PayPal\EBLBaseComponents\DoExpressCheckoutPaymentRequestDetailsType();
$doExpressCheckoutPaymentRequestDetails->Token = $_GET['token']; // given from Paypal
$doExpressCheckoutPaymentRequestDetails->PayerID = $_GET['PayerID']; // given from Paypal
$paymentDetails = new \PayPal\EBLBaseComponents\PaymentDetailsType();
$paymentDetails->OrderTotal = new \PayPal\CoreComponentTypes\BasicAmountType('EUR', 10.00);
$doExpressCheckoutPaymentRequestDetails->PaymentDetails = array($paymentDetails);
$doExpressCheckoutPaymentRequest = new \PayPal\PayPalAPI\DoExpressCheckoutPaymentRequestType($doExpressCheckoutPaymentRequestDetails);
$doExpressCheckoutPaymentReq->DoExpressCheckoutPaymentRequest = $doExpressCheckoutPaymentRequest;
$service = new \PayPal\Service\PayPalAPIInterfaceServiceService($config);
$response = $service->DoExpressCheckoutPayment($doExpressCheckoutPaymentReq);
var_dump($response); // If $response->Ack is 'success', the payment is done.
@MahbbRah
Copy link

MahbbRah commented Jan 3, 2018

Thank you so much it is really informative 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment