Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal Smart Payment Buttons integration with server-side REST API

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:

  1. Create a Payment Authorization (with payidand orderid)

  2. Send this payid to the client (javascript) to be approved.

  3. 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):

  1. User chooses the item and submits the form.

  2. 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.

  3. User should be redirected to a final page with Smart Buttons to complete the payment.

  4. 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:

  1. Get the Payment Authorization and pass it to the Client (final page with the buttons)

  2. Redirect the users after payment complete.

like image 728
Thiago Guimarães Avatar asked Apr 22 '20 16:04

Thiago Guimarães


People also ask

How do I integrate a PayPal button?

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.

What are PayPal smart payment buttons?

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.


1 Answers

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)

like image 157
Preston PHX Avatar answered Sep 25 '22 15:09

Preston PHX