简体   繁体   English

如何在Laravel 5.4中为PayPal REST API创建“ CreatePayment”和“ PaymentExecute”控制器

[英]How to create “CreatePayment” and “PaymentExecute” controller for PayPal REST api in laravel 5.4

I am creating a application using Laravel 5.4 that require PayPal to do payments ,when i study the PayPal integration documentation i found that the REST API need 2 controller in server side name "/demo/checkout/api/paypal/payment/create/" and "/demo/checkout/api/paypal/payment/execute/", but the documentation of PayPal is blur, does anyone know what is it and the example of it? 我在使用Laravel 5.4创建一个需要PayPal进行付款的应用程序时,当我研究PayPal集成文档时发现REST API需要服务器端名称“ / demo / checkout / api / paypal / payment / create /”中的2个控制器和“ / demo / checkout / api / paypal / payment / execute /”,但是PayPal的文档模糊不清,有人知道它是什么以及它的示例吗?

Here is my code in frontend: 这是我在前端的代码:

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $("meta[name='csrf-token']").attr("content")
        }
    });

    paypal.Button.render({
      env: 'sandbox', // sandbox | production

      // Show the buyer a 'Pay Now' button in the checkout flow
      commit: true,

      // payment() is called when the button is clicked
      payment: function() {

        // Set up a url on your server to create the payment
        var CREATE_URL = '{{route('paypal.createpayment')}}';

        // Make a call to your server to set up the payment
        return paypal.request({
          method: 'post',
          url: CREATE_URL,
          headers: {
            'x-csrf-token': $("meta[name='csrf-token']").attr("content")
          }
        }).then(function(res) {
          return res.paymentID;
        });
      },

      // onAuthorize() is called when the buyer approves the payment
      onAuthorize: function(data, actions) {

        // Set up a url on your server to execute the payment
        var EXECUTE_URL = '/demo/checkout/api/paypal/payment/execute/';

        // Set up the data you need to pass to your server
        var data = {
          paymentID: data.paymentID,
          payerID: data.payerID
        };

        // Make a call to your server to execute the payment
        return paypal.request.post(EXECUTE_URL, data)
        .then(function (res) {
          window.alert('Payment Complete!');
        });
      }
    }, '#paypal-button-container');

The documentation is on this page with example in multiple programming language : https://developer.paypal.com/docs/api/quickstart/payments/#define-payment 此页面上的文档带有使用多种编程语言的示例: https : //developer.paypal.com/docs/api/quickstart/payments/#define-payment

So on the first call to your server, you need to define the payment and create the payment . 因此,在第一次调用服务器时,您需要定义付款创建付款

On the second call to your server (called inside the onAuthorize function) you need to execute the payment . 在第二次调用服务器时(在onAuthorize函数内部调用),您需要执行付款

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM