Paypal button payment docking and callback address configuration

Because the company wants to connect to the paypal payment method, it took a long time to figure it out, but it didn't work out.
This time, the simplest button configuration is used. There are actually many payment method configurations supported.

Register an account

I won’t talk about this, you can refer to: This article is the most detailed. If you don’t want to go now, right-click to open a new tab.

Payment method docking selection.

There are many kinds, but here I choose the simplest one. Button operation.

sandbox testing

Register an account and generate a sandbox account and test app

Paypal payment docking is actually two accounts transfer money to each other.

  1. Register a Merchant Account
  2. Register a personal account
  3. Generate app (individual users and business users, the payment request is bound with the app, according to my estimation, it should be paypal to bind the payment initiated by that institution) still refer to this article if the final result is
    like
    this
  4. This is the app I created
  5. It is best to create two sandbox accounts here instead of using the ones that come with the system. Note that one is business and one is personal
    insert image description here

Button configuration page

There are tutorials in the api that selects the button configuration first.
insert image description here
In fact, the official documents here are already very detailed. Official Documentation
My page is

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Ensures optimal rendering on mobile devices. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <!-- Optimal Internet Explorer compatibility -->
  </head>

  <body>
  <script
    src="https://www.paypal.com/sdk/js?client-id=sb"> // Required. Replace SB_CLIENT_ID with your sandbox client ID. 官方文档已经说的很清楚了这里是app 的client ID 所以不要在傻不拉几的一直病急乱投医了。
  </script>
  

  <div id="paypal-button-container"></div>

  <script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      // This function sets up the details of the transaction, including the amount and line item details.
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '100.02'
          },
// invoice_id It can't be repeated, mean is : The API caller-provided external invoice number for this order. Appears in both the payer's transaction history and the emails that the payer receives.
       "invoice_id":"CCCCCCCCCC" // 这里可以你自己定义,类似于商品的编号。不能重复,放心回调会返回的。
        }]
      });
    },
    // 支付成功后的函数
    onApprove: function(data, actions) { 
      // This function captures the funds from the transaction.
      return actions.order.capture().then(function(details) {
        // This function shows a transaction success message to your buyer.
        alert('Transaction completed by callback info' + details.payer.name.given_name);
	alert('Transaction completed by callback info: ' + data.orderID);
      });
    },
    // 支付取消后的函数
        onCancel: function (data) {
            console.log("Cancel payment!");
            window.location.href= "https://www.baidu.com";
            //cancel return url;
        },
        //支付失败的函数 
        onError: function (data) {
            console.log("payment failed!");
            window.location.href= "https://www.baidu.com";
            // error return url;
        }
  }).render('#paypal-button-container');
  //This function displays Smart Payment Buttons on your web page.
  </script>
</body>
</html> 

sandbox payment

Open the page and click the payment button, log in to your personal account and click payment. If there is still money in the account.
What? You don't know where the account is here

After the payment is completed, remember to log in to the main account to check whether the money has been added.

Sandbox IPN callback settings

Callbacks are asynchronous.
I am very grateful to the inspiration to me
and this brother who
first logs in to the merchant’s sandbox user Sandbox
insert image description here
login address Note: If it is a public network ip address
insert image description here
and then sends the payment request again, you will get the request body after payment.
Inside the request body is:

mc_gross=10.01&protection_eligibility=Eligible&address_status=confirmed&payer_id=UGP233DJP2RF6&address_street=NO+1+Nan+Jin+Road&payment_date=04%3A18%3A52+Jan+06%2C+2021+PST&payment_status=Completed&charset=gb2312&address_zip=200000&first_name=John&mc_fee=0.64&address_country_code=CN&address_name=Doe+John&notify_version=3.9&custom=&payer_status=verified&business=sb-eksxx4542815%40business.example.com&address_country=China&address_city=Shanghai&quantity=1&verify_sign=A68859pDcJKhpisZpaMkuQEJHzSPATI19GwzSqQalG-4vkgJ2elYAp00&payer_email=sb-bs3404551534%40personal.example.com&txn_id=4GS170274N9213147&payment_type=instant&last_name=Doe&address_state=Shanghai&receiver_email=sb-eksxx4542815%40business.example.com&payment_fee=0.64&shipping_discount=0.00&insurance_amount=0.00&receiver_id=GZX82WGTLDAL6&txn_type=express_checkout&item_name=&discount=0.00&mc_currency=USD&item_number=&residence_country=CN&test_ipn=1&shipping_method=Default&transaction_subject=&payment_gross=10.01&ipn_track_id=38aa83d901e58

The official seems to want us to reply to verify whether the transaction is successful. Below are the official requirements.

收到通知后需要进行通知确认

为了确保付款已进入您的PayPal账户,您必须验证用作“receiver_email”的电子邮件地址是否已在您的PayPal账户中注册并得到确认。

服务器收到即时付款通知后,您将需要通过构建一个发送到PayPal的HTTP POST对其进行确认。您的POST应发送到https://www.paypal.com/cgi-bin/webscr

(当您收到时)。您还需要将一个值为“_notify-validate”的名为“cmd”变量(例如,cmd=_notify-validate)附加到POST字符串。

PayPal将回复该POST,并在回复的正文中包含一个单词“VERIFIED”或“INVALID”。当您收到VERIFIED回复时,您需要在实施订单之前执行若干检查:

确认“payment_status”为“Completed”,因为系统也会为其他结果(如“Pending”或“Failed”)发送 IPN。
检查“txn_id”是否未重复,以防止欺诈者重复使用旧的已完成的交易
验证“receiver_email”是已在您的PayPal账户中注册的电子邮件地址,以防止将付款发送到欺诈者的账户
检查其他交易详情(如物品号和价格),以确认价格未改变
完成了以上检查后,您可以使用 IPN 数据更新您的数据库,并处理购物。
如果收到“无效”通知,则应将其视为可疑通知,并应对其进行调查。

The general callback steps are,

  1. parse request
  2. Find the txn_id sent from the official interface to check the payment status, whether the payment is successful
  3. Find the payment record in your own library
  4. All of the above are in line, and the order is confirmed asynchronously.

The next article writes the operation of querying the transaction status according to the paypal transaction number.

Guess you like

Origin blog.csdn.net/weixin_44961083/article/details/112337620