How to Handle Asynchronous notify Callbacks for Alipay and WeChat Pay

In the previous article on this site, I introduced you to the use ofAlipay paymentandWeChat PayThe two articles only introduced the method of processing synchronous return data, but did not introduce the method of processing the asynchronous notification return data of the payment platform. The processing of only synchronization without asynchronous notification is obviously incomplete. If the user completes the payment and closes the page without waiting for the payment website to jump back to the website that initiated the payment, the website that initiated the payment will not receive the payment result notification. Today I will introduce to you how to add a method for handling asynchronous notifications on the payment initiation website.

The similarities and differences between asynchronous notifications and synchronous notifications

The similarity between the two notification methods is that the data returned is exactly the same. The main differences are in the following two points.

  1. The method for returning data from synchronous notification is a GET request. We can easily obtain the returned data through the $_GET global variable and process it. The request method for asynchronous notification is the POST method.
  2. We can easily print out the data returned by the synchronization notification. If an error occurs, it is convenient to troubleshoot the cause. However, the data returned by the synchronization notification cannot be printed directly. The returned data can only be viewed by writing to the Log or database.

With the above two differences, the method of processing asynchronous notification is definitely different from the method of processing synchronous notification. In fact, the main difference lies in the method of returning data, one is GET request and the other is POST request. Let’s take a look at the specific processing code.

Considerations for handling asynchronous notifications

Whether it is a notification or an asynchronous notification, the business logic for processing notifications on the website that initiates the payment is actually the same. Based on the DRY principle, we can put the two processing methods together and make judgments on the differences between the two payment methods and process them separately. The following is the method for obtaining request data for Alipay payment and WeChat payment. If the notification method is a synchronous notification, the order number can be obtained directly through the $_GET variable. If it is an asynchronous notification, Alipay can directly obtain the data through the $_POST variable. The data returned by WeChat payment is in XML format. We need to convert it into data through an auxiliary function and then obtain the data.

/**
 * 获取支付网关返回的 out_trade_no
 */
if ( $notify == 'return' ) {
   $out_trade_no = $_GET[ 'out_trade_no' ];
} else {
   if ( $method == 'alipay' ) {
      $out_trade_no = $_POST[ 'out_trade_no' ];
   } else {
      $out_trade_no = Helper::xml2array( file_get_contents( 'php://input' ) )[ 'out_trade_no' ];
   }
}

To verify that the synchronization notification is successful, we need to return a success message “success” to the payment gateway to prevent the payment gateway from sending notifications repeatedly.

if ( $response->isPaid() ) {
    if ( $notify == 'notify' ) {
      echo 'success';
    }
}

With the asynchronous notification method,Payment interfaceThe development is considered complete when the payment gateway page is closed after the user completes the payment. The website that initiates the payment can also quickly determine the user’s payment status and perform subsequent business processing such as automatic order processing and automatic shipping based on the user’s payment status.

Key points of Alipay WeChat payment development

In fact, in the process of opening up Alipay and WeChat payment, the docking of the payment interface is not the most important part. The interface precautions are fixed. There are only so many in total, and there will not be many changes. The business logic for processing orders is ever-changing, and because payment is directly designed into the transaction, we must be careful and prudent during the processing of business logic, and try to fully consider the occurrence and processing methods of various situations, so that we can be foolproof.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *