Last active
April 25, 2018 16:58
-
-
Save a-r-m-i-n/ce808de89aafd51cacf6 to your computer and use it in GitHub Desktop.
PayPal Classic API - Express Checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"paypal/merchant-sdk-php": "*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config = array( | |
'mode' => 'sandbox', // or 'live' | |
'acct1.UserName' => 'vendor_api1.example.com', | |
'acct1.Password' => 'MW1M19EETX3YJX3Y', | |
'acct1.Signature' => 'BcaYf-Baofqt7oxiopZb8bHnZGVRAuoqoUoMH3iALBfE4mOKgUQO4ciK' | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This example uses the Merchant PHP SDK of Paypal. To get it, just use composer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much it is really informative 👍