Cross border payment tripartite interface and order query and refund business

This is our first attempt to integrate the cross-border tripartite payment interface PayPal with Django2

First register the official website And Developer Platform:

After successful registration, on the account control page of the sandbox:

Two accounts will be created by default, one for the merchant and the other for the individual


  The process we demonstrated is to log in to the third-party website with a personal account to pay for the merchant account. Of course, if you don't want to use the default account, you can also click the blue button of Create account on the right to create it separately.

Then enter the application management page:

A payment application has been created by default and its client has been recorded_ ID and client_secret, I'll use it later




  Then, we can go back to the account management page to modify the payment balance of the personal account


  Maximum quota setting

ok, after that, the pre task is completed. Now run the command to install the sdk of paypal on the python side

pip3 install paypalrestsdk

Now you can create a new payment view in django

import paypalrestsdk  
def payment(request):  
  "mode": "sandbox", # sandbox Representative sandbox  
  "client_id": "Yours client_id,  
  "client_secret": "Yours client_secret" })  
    payment = paypalrestsdk.Payment({  
        "intent": "sale",  
        "payer": {  
            "payment_method": "paypal"},  
        "redirect_urls": {  
            "return_url": "http://localhost:8000/palpay/pay/",#Payment successful jump page  
            "cancel_url": "http://localhost:3000/paypal/cancel/"},#Cancel payment page  
        "transactions": [{  
            "amount": {  
                "total": "5.00",  
                "currency": "USD"},  
            "description": "This is an order test"}]})  
    if payment.create():  
        print("Payment created successfully")  
        for link in payment.links:  
            if link.rel == "approval_url":  
                approval_url = str(link.href)  
                print("Redirect for approval: %s" % (approval_url))  
                return redirect(approval_url)  
        return HttpResponse("Payment failed")

Here is an explanation of the key parameter, return_url is the page to call back after the payment is successful. paypal will send back a payer id, and then the server needs to verify the payment to really complete the payment. total is the payment amount, accurate to minutes, currency is the currency, and multi clock currencies are supported.

When Django's server creates a payment order, it redirects to paypal's sandbox environment. At this time, you must use the sandbox's personal account to log in and pay

After the payment is completed, it will jump back to the callback page just passed: http://localhost:8000/palpay/pay/?paymentId=PAYID-L3SYORA3C031930S1733650J&token=EC-9TG269735K620131N&PayerID=ETYYRCDN8C3XJ

Here, paypal will pass three parameters: payment id,token and payer id

At this time, in the callback method, we need to confirm and verify the payment through the payer id

def payment_execute(request):  
    paymentid = request.Get.get("paymentId") #order id  
    payerid = request.Get.get("PayerID")  #Payer id  
    payment = paypalrestsdk.Payment.find(paymentid)  
    if payment.execute({"payer_id": payerid}):  
        print("Payment execute successfully")  
        return HttpResponse("Payment successful")  
        print(payment.error) # Error Hash  
        return HttpResponse("Payment failed")

Author: Liu Yue's technology blog
Source: rare earth Nuggets
 The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

This transaction will end happily. Of course, sometimes we need to check the transaction flow, or we can view the transaction details through the interface

payment = paypalrestsdk.Payment.find("order number")  

By passing in the order id, we the status, flow id, and creation date of the transaction.

If you want to refund, you can use the serial number in the transaction details to refund

from paypalrestsdk import Sale  
sale = Sale.find("Serial number")  
# Make Refund API call  
# Set amount only if the refund is partial  
refund = sale.refund({  
    "amount": {  
        "total": "5.00",  
        "currency": "USD"}})  
# Check refund status  
if refund.success():  
    print("Refund[%s] Success" % (  
    print("Unable to Refund")  


Overall, there is no particular difficulty. The whole payment process is more compact than Alipay. But making payment security is the top priority. What Alipay does is slightly safer than Paypal in personal experience (at the personal experience level), at least in credit card fraud and theft control. Insurance is provided in terms of risk protection and compensation. Of course, due to the great differences in the financial environment, it does not mean that PayPal's risk control is not good, but the mechanism is different. In foreign countries, if the cardholder's credit card is stolen, the general card issuing organization will let the merchant bear the responsibility, while in China, more verification can only be set up in the transaction link, In essence, it is the responsibility of the cardholder. That's what makes Alipay's wind control look better.

Finally, about the rate, the official rate given by PayPal is 3.9% + $0.3 per transaction (according to your transaction flow, the proportion can be preferential, and the specific lower limit depends on the monthly business limit of the acceptor). However, this is a beautiful knife. I have to say that this rate is quite high, but domestic e-commerce making overseas payment generally still needs to access paypal as the payment method. Alipay's rate is basically around 1.2%, the specific rates also look at the transaction flow, the lower limit can be basically done, simply look at the rate seems Alipay has more advantages, but do not forget that such a comparison is not scientific, because all those who access Paypal are the areas that cover the foreign currency business, and the rate is the problem that investors should consider.

Posted on Fri, 03 Dec 2021 09:20:45 -0500 by kh411dz