Home  >  Article  >  Backend Development  >  PHP WeChat payment flow chart idea analysis

PHP WeChat payment flow chart idea analysis

PHPz
PHPzOriginal
2023-04-12 13:53:13805browse

With the popularity of mobile payment, WeChat payment, as the most popular third-party payment method on the market, has become more and more common in our daily lives. Especially in the field of e-commerce, the usage rate of WeChat payment has increased significantly and it has become an indispensable payment method for many merchants. This article will introduce the basic process and technical principles of WeChat payment to help PHP developers better understand and apply WeChat payment. Let’s first take a look at the basic process of WeChat payment.

Basic process of WeChat payment

The basic process of WeChat payment can be divided into the user placing an order on the merchant APP or H5 page, the merchant calling the WeChat payment API to pay, WeChat returning the transaction results, and the merchant receiving the payment. Notice the four steps.

Specifically, when a user places an order on a merchant APP or H5 page, the merchant needs to pass the order information to the WeChat payment platform, and after the payment processing of the WeChat payment platform, the payment result is returned. When the payment result is returned, the merchant performs corresponding business processing based on the transaction result and displays the transaction result to the user.

WeChat payment flow chart:

  1. The user selects the product and submits the order;
  2. The merchant system receives the user's order request and calls the WeChat payment API to submit the transaction request;
  3. After receiving the merchant's request, the WeChat payment platform verifies the user's identity and payment amount, and initiates an order payment request to the user.
  4. The user confirms the payment and enters the payment password
  5. The WeChat payment platform will return the payment result to the merchant system and notify the user that the payment is successful.

WeChat payment technology principle

WeChat payment technology principle mainly consists of three parts: WeChat payment API, WeChat payment platform, and merchant system. Let’s take a look at the WeChat payment API first.

WeChat Payment API

When the merchant system initiates a payment request through WeChat Pay, it needs to send the payment request to the WeChat Payment platform through the WeChat Payment API. The WeChat payment API is a set of RESTful interfaces that are highly readable and easy to use. It mainly includes interfaces such as unified order placing, order query, order closing, refund application, and refund inquiry. Below we mainly introduce the use of the unified ordering interface.

Steps for using the unified ordering interface

  1. Get the prepaid order number
    In the merchant system, developers call the unified ordering interface of the WeChat payment API to send a message to WeChat The payment platform submits the payment request. After receiving the request, the WeChat payment platform verifies the order information submitted by the merchant, and then returns a prepayment order number to uniquely identify the order. The developer needs to save the prepayment order number and use it as an important parameter in the subsequent payment process.

Unified order interface calling example:

SetBody("test");
$input->SetOut_trade_no(WxPayConfig::MCHID . date("YmdHis"));
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url("http://www.example.com/wxpay/notify.php");
$input->SetTrade_type("APP");
$order = WxPayApi::unifiedOrder($input);
print_r($order);
?>
  1. Get payment parameters
    After receiving the prepayment order number returned by the WeChat payment platform, the merchant system needs to re- Call the unified ordering interface of WeChat payment API to obtain payment parameters. Payment parameters include APPID, timestamp, random string, signature method, payment signature, etc. The payment signature is the result of the merchant system or a third-party payment agent encrypting the payment parameters and uniquely identifies the transaction. The merchant system needs to save these payment parameters and return them to the mobile terminal, which calls up the WeChat SDK to complete the payment process.

Unified order interface calling example:

$parameters = [
    'appid' => $app_id,
    'partnerid' => $mch_id,
    'prepayid' => $prepay_id,
    'package' => 'Sign=WXPay',
    'noncestr' => $nonce_str,
    'timestamp' => $timestamp,
];
ksort($parameters);
$string = '';
foreach ($parameters as $key => $value) {
    $string .= $key . '=' . $value . '&';
}
$string .= 'key=' . $key;
$parameters['sign'] = strtoupper(md5($string));

WeChat payment platform

WeChat payment platform is a payment service platform of WeChat, which mainly consists of payment, settlement and clearing composed of other modules. The payment module is responsible for receiving and processing payment requests issued by merchants, and receiving and processing payment result notifications; the settlement module is responsible for merchant settlement, including merchant settlement inquiries, settlement strategy configuration, and generation of settlement lists; the liquidation module is responsible for regular liquidation and management Financial affairs of each functional module of the WeChat payment platform.

Merchant System

The merchant system is the client of WeChat payment and is responsible for receiving and processing various payment requests initiated by the WeChat payment platform and performing corresponding payment operations. The merchant system mentioned in this article mainly refers to the PHP merchant system, which transfers order information, obtains prepayment order numbers, obtains payment parameters and other basic payment operations by calling the WeChat payment API.

Summary

With the popularity and promotion of WeChat Pay in the domestic market, its technical principles and usage methods have become more and more mature. Developers need to understand the basic principles and usage of WeChat Pay in order to better apply WeChat Pay technology to meet users' payment needs. This article mainly introduces the basic process and technical principles of WeChat payment, hoping to be helpful to PHP developers.

The above is the detailed content of PHP WeChat payment flow chart idea analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn