Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Paypal Checkout REST api integration in Laravel 5.7 with smart payment buttons

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.78k
    Comment on it

    Hi All,

    Paypal has updated their payment integration API to new REST API and existing APIs have been deprecated since Feb 2019. Due to less documentation, it was very hard to integrate these APIs with Laravel. After spending some time, I was able to integrate these into my Laravel project.

    Please follow these steps:

    1: I am assuming you have set-up your sandbox account in Paypal and have client id and secrete keys.

    2: Add new checkout SDK PayPal/Checkout-PHP-SDK in your Laravel project:

    composer require paypal/paypal-checkout-sdk

    3: Now copy and paste the smart button Html into your view file where you need to add the payment button:

    <div id="paypal-button-container"></div>
    
    @section('script')
        <script src="https://www.paypal.com/sdk/js?client-id=client_id"></script>
        <script>
            // Render the PayPal button into #paypal-button-container
           // X-CSRF-TOKEN is also required to add in request, otherwise you will not be able to access the createorder url
            paypal.Buttons({
    
                // Call your server to set up the transaction
                createOrder: function(data, actions) {
                    var _token = "{{ csrf_token() }}";
                    return fetch('http://yoursite.com/createorder', {
                        method: 'post',
                        headers: {
                            'X-CSRF-TOKEN': _token,
                            'Content-Type': 'application/json',
                        },
                    }).then(function(res) {
                        return res.json();
                    }).then(function(orderData) {
                        return orderData.result.id;
                    });
                },
    
                // Call your server to finalize the transaction
                onApprove: function(data, actions) {
                    var _token = "{{ csrf_token() }}";
                    return fetch('http://yoursite.com/captureorder/' + data.orderID + '/capture/', {
                        method: 'post',
                        headers: {
                            'X-CSRF-TOKEN': _token,
                            'Content-Type': 'application/json',
                        },
                    }).then(function(res) {
                        return res.json();
                    }).then(function(orderData) {
                        // Three cases to handle:
                        //   (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                        //   (2) Other non-recoverable errors -> Show a failure message
                        //   (3) Successful transaction -> Show a success / thank you message
    
                        // Your server defines the structure of 'orderData', which may differ
                        var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
    
                        if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                            // Recoverable state, see: "Handle Funding Failures"
                            // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
                            return actions.restart();
                        }
    
                        if (errorDetail) {
                            var msg = 'Sorry, your transaction could not be processed.';
                            if (errorDetail.description) msg += '\n\n' + errorDetail.description;
                            if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
                            // Show a failure message
                            return alert(msg);
                        }
    
                        // Show a success message to the buyer
                        alert('Transaction completed by ' + orderData.result.payer.name.given_name);
                    });
                }
    
    
            }).render('#paypal-button-container');
        </script>

     

    In the above code, we are using 2 URLs createorder and captureorder. When we click on the Payment button, an order will be created in PayPal from createorder page and then the Paypal authentication window will appear for authentication.

    If authentication is done then Paypal will redirect us to captureorder page where Paypal will send us the response whether the order is captured or not.

    4: We have to create routes for createorder and captureorder in routes/web.php file and have to create some more URLs (return and cancel URLs)

    Route::post('createorder', array('as' => 'paypal.createorder','uses' => 'PaypalController@postPaymentWithpaypal'))
    ;
    Route::post('captureorder{orderId}/capture', array('as' => 'paypal.captureorder','uses' => 'PaypalController@capturePaymentWithpaypal'));
    Route::get('paypalreturn', array('as' => 'paypal.paypalreturn','uses' => 'PaypalController@returnpPaypal'));
    Route::get('paypalcancel', array('as' => 'paypal.paypalcancel','uses' => 'PaypalController@cancelPaypal'));

    5: Now create a controller paypalController.php and put the below code in that controller (you also need to create a paypal.php file inside the config folder for storing Paypal details. paypal.php code is also attached):

    namespace App\Http\Controllers;
    
    use App\Http\Requests;
    use Illuminate\Http\Request;
    use Validator;
    use URL;
    use Session;
    use Redirect;
    use Input;
    
    /** All Paypal Details class **/
    use PayPalCheckoutSdk\Core\PayPalHttpClient;
    use PayPalCheckoutSdk\Core\SandboxEnvironment;
    use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
    use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
    
    class PaypalController extends Controller
    {
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
    
            /** setup PayPal api context **/
            $paypal_conf = \Config::get('paypal');
    
            $clientId = $paypal_conf['client_id'];
            $clientSecret = $paypal_conf['secret'];
        }
    
    
        /**
         * This is the page where you have to display your payment page
         *
         * @return \Illuminate\Http\Response
         */
        public function payWithPaypal()
        {
            return view('Paypal1/paywithpaypal');
            // Paste smart button code in this view
        }
    
        /**
         * Your business logic will come here
         * Order will be created in this function and then paypal window will open for authentication
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function postPaymentWithpaypal(Request $request)
        {
            $paypal_conf = \Config::get('paypal');
            $clientId = $paypal_conf['client_id'];
            $clientSecret = $paypal_conf['secret'];
            $environment = new SandboxEnvironment($clientId, $clientSecret);
            $client = new PayPalHttpClient($environment);
    
            $request = new OrdersCreateRequest();
            $request->prefer('return=representation');
            $request->body = [
                                 "intent" => "CAPTURE",
                                 "purchase_units" => [[
                                     "reference_id" => "123456",
                                     "amount" => [
                                         "value" => "100",
                                         "currency_code" => "USD"
                                         //put other details here as well related to your order
                                     ]
                                 ]],
                                 "application_context" => [
                                      "cancel_url" => "http://yourproject.com/paypalreturn",
                                      "return_url" => "http://yourproject.com/paypalcancel"
                                 ]
                             ];
    
            try {
                // Call API with your client and get a response for your call
                $response = $client->execute($request);
                
                // If call returns body in response, you can get the deserialized version from the result attribute of the response
                echo json_encode($response); die;
            }catch (HttpException $ex) {
                echo $ex->statusCode;
                //echo json_encode($ex->getMessage());
    
            }
        }
    
      /**
         * After successful paypal authentication from paypal window, paypal will call this function to capture the order
         *
         * @param   $orderId returned from papal and also defined in routes
         * @return \Illuminate\Http\Response
         */
    
        public function capturePaymentWithpaypal($orderId)
        {
            $paypal_conf = \Config::get('paypal');
            $clientId = $paypal_conf['client_id'];
            $clientSecret = $paypal_conf['secret'];
            $environment = new SandboxEnvironment($clientId, $clientSecret);
            $client = new PayPalHttpClient($environment);
    
            // Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
            // $response->result->id gives the orderId of the order created above
            $request = new OrdersCaptureRequest($orderId);
            $request->prefer('return=representation');
            try {
                // Call API with your client and get a response for your call
                $response = $client->execute($request);
                
                // If call returns body in response, you can get the deserialized version from the result attribute of the response
                echo json_encode($response); die;
            }catch (HttpException $ex) {
                echo $ex->statusCode; die;
                //print_r($ex->getMessage());
            }
        }
      }
    
    // You can also define return and cancel urls here.

     

    config/paypal.php code

    <?php
    
    
    return array(
        'client_id' =>'your client id',
        'secret' => 'your secrete key',    
    
        'settings' => array(
            /**
             * Available option 'sandbox' or 'live'
             */
            'mode' => 'sandbox',
            /**
             * Specify the max request time in seconds
             */
            'http.ConnectionTimeOut' => 1000,
            /**
             * Whether want to log to a file
             */
            'log.LogEnabled' => true,
            /**
             * Specify the file that want to write on
             */
            'log.FileName' => storage_path() . '/logs/paypal.log',
            /**
             * Available option 'FINE', 'INFO', 'WARN' or 'ERROR'
             *
             * Logging is most verbose in the 'FINE' level and decreases as you
             * proceed towards ERROR
             */
            'log.LogLevel' => 'FINE'
        ),
    );

     

    I hope this explanation will help you to quickly integrate new Paypal checkout REST API SDK and Paypal smart payment buttons with your Laravel project.

    You can also improve this according to your requirement.

    Following are reference URLs:

    SDK: https://github.com/paypal/Checkout-PHP-SDK/tree/60d9b395e4a775097b005ff654e7b9e4009bdc99
    Smart Button: https://developer.paypal.com/demo/checkout/#/pattern/server
    Rest API for creating/capturing orders: https://developer.paypal.com/docs/api/orders/v2/#orders

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: