WeChat Pay is a mobile-based payment system that allows online payments and refunds. This article provides a comprehensive guide on how to implement WeChat Pay and refund functionality with PHP, designed for developers looking to integrate WeChat Pay APIs.
First, you need to go to the WeChat Pay official website to register a merchant account. After registration, you will receive a merchant ID, which is your unique identifier in the WeChat Pay system.
In the WeChat Pay merchant backend, you need to generate an API key to communicate with WeChat Pay. This key must remain confidential and should only be used on the server side.
The unified order is the first step in the WeChat Pay process and is crucial to the overall payment flow. In the unified order interface, you need to provide the following parameters:
appid: Your WeChat public account or mini-program appid<br>mch_id: Merchant ID<br>nonce_str: Random string used to prevent duplicate submissions<br>body: Product description<br>out_trade_no: Merchant order number, which must be unique<br>total_fee: Order amount in cents<br>spbill_create_ip: Client IP<br>notify_url: Asynchronous notification URL, WeChat Pay will notify the merchant through this URL<br>trade_type: Trade type, such as JSAPI, NATIVE, APP, etc.<br>openid: The unique identifier of the user under the merchant's appid, required only for JSAPI payments<br>sign: Signature used to verify the legitimacy of the API call
Using the above parameters, create a unified order request and send it to the WeChat Pay API. The API will return a prepay_id as a payment credential.
Using the prepay_id returned from the previous step, you need to generate parameters to initiate the WeChat Pay process:
appId: Your WeChat public account or mini-program appid<br>timeStamp: The current timestamp, in seconds<br>nonceStr: Random string used to prevent duplicate submissions<br>package: The prepay_id value returned by the unified order interface<br>signType: Signature type, MD5 by default<br>paySign: Signature used to verify the legitimacy of the parameters
Convert the above parameters into a JSON format and send them to the client. The client can use these parameters to initiate WeChat Pay.
After the client receives the payment parameters, they initiate the WeChat Pay process. The user enters their payment password to confirm the payment.
Once the client completes the payment, WeChat will send a payment result notification to the specified asynchronous notification URL.
openid: User's unique identifier<br>out_trade_no: Merchant order number<br>transaction_id: WeChat Pay order number<br>total_fee: Order amount