I know there are a few questions regarding PayPal Integration but I'm trying to implement PayPal Express Checkout with Smart Buttons and REST API and no success.
What I want to do is:
Create a Payment Authorization (with payid
and orderid
)
Send this payid
to the client (javascript) to be approved.
Redirect after payment to a confirmation page.
I have already created a Payment Authorization with the code bellow:
<?php
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
// Used for composer based installation
require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
// Use below for direct download installation
// require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
// After Step 1
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'client id', // ClientID
'cliente secret' // ClientSecret
)
);
// After Step 2
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal('1.00');
$amount->setCurrency('USD');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/your_redirect_url.html")
->setCancelUrl("https://example.com/your_cancel_url.html");
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
// After Step 3
try {
$payment->create($apiContext);
echo $payment;
echo "\n\nRedirect user to approval_url: " . $payment->getApprovalLink() . "\n";
echo $data= json_encode($payment->id, JSON_PRETTY_PRINT), "\n";
echo "</br>";echo "</br>";
echo $data= $payment->id;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// This will print the detailed information on the exception.
//REALLY HELPFUL FOR DEBUGGING
echo json_encode($ex->getData()->id, JSON_PRETTY_PRINT), "\n";
echo "</br>";echo "</br>";
echo $ex->getData()->id;
}
?>
My setup is as following (Please, correct if it's wrong):
User chooses the item and submits the form.
User is redirected to a new page with a new form and then fills the form with his name and other personal informations and then submits the form to be saved in the database and generate the Payment Authorization.
User should be redirected to a final page with Smart Buttons to complete the payment.
User should be redirected to a confirmation page, confirming his payment was successfully made.
The problem is that I'm stuck in the step 2 because once I generate the Payment Authorization, I don't know how to pass this id (and other required parameters) to the client, in this new page that is supposed to show the Smart Payment Buttons to complete the transaction.
I'm following along with the tutorial in PayPal documentation but I'm not able to understand.
The JavaScript makes calls to my server to get the payid
, it works, but it generates a new payid
and a new Payment Order, i want to use the one that was previously created when the user submitted the form and then, somehow, pass it to the final page with the Smart Buttons to complete the transaction and redirect to a confirmation page.
My Javascript:
<script>
paypal.Button.render({
onApprove: function(data) {
return fetch('/my-server/get-paypal-transaction', {
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(res) {
return res.json();
}).then(function(details) {
alert('Transaction approved by ' + details.payer_given_name);
}, '#paypal-button');
</script>
The problem is that it generates a new orderID
, a new transaction, and I want to retrieve the transaction that was generated when the user submitted the form in the step 2 but I don't know what I should do.
Ultimately what I need is:
Get the Payment Authorization and pass it to the Client (final page with the buttons)
Redirect the users after payment complete.
PayPal HTML buttons — Simply log in to the PayPal website to create, and optionally customize, a payment button. Then copy and paste the payment button's HTML code snippet to your website. Use this option if your HTML or web programming skills are limited and you sell only a few products on your site.
Smart Payment Buttons by PayPal are designed to improve the checkout experience and improve checkout conversions. With this integration, you can give customers the option to pay with PayPal, Venmo, and major credit cards and debit cards - on almost any device - when they checkout with PayPal.
You are looking for this front-end: https://developer.paypal.com/demo/checkout/#/pattern/server
For your backend, you should use the latest v2/orders SDK: https://github.com/paypal/Checkout-PHP-SDK
(Your v1/payments backend will work, but that SDK is deprecated and there is no reason to use it for a new integration)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With