Home Backend Development PHP Tutorial PHP implementation method related to WeChat mini program payment

PHP implementation method related to WeChat mini program payment

May 31, 2023 pm 09:10 PM
WeChat Pay php implementation Mini program payment

With the widespread use of WeChat mini programs, WeChat mini program payment has become a necessary payment method for more and more merchants. In order to facilitate merchants to access the WeChat applet payment function, this article will introduce an implementation method based on PHP.

1. Preparation work
Before accessing WeChat mini program payment, you need to prepare the following steps:

1.1 Obtain WeChat payment merchant number and API key
The merchant number is the unique identifier applied by the merchant on the WeChat payment platform, and the API key is the payment key generated when applying for the merchant and is used for signature verification.
1.2 Configure WeChat payment certificate
Merchant needs to place the WeChat payment certificate in the security directory of the server and provide the certificate path.
1.3 Build an HTTPS server
WeChat Pay requires merchants to use the HTTPS protocol for data transmission, so an HTTPS certificate needs to be configured on the server.

2. WeChat Mini Program payment process
The WeChat Mini Program payment process generally includes the following steps:

2.1 User orders
Users select products through the Mini Program and place orders , the applet sends the order information to the merchant server for processing.
2.2 Merchant Server Ordering
After receiving the user’s order request, the merchant server generates a prepaid order and returns it to the mini program.
2.3 The mini program activates payment
After receiving the prepayment order, the mini program activates the payment function through the WeChat payment API.
2.4 Payment result notification
After the payment is completed, the WeChat server will notify the merchant server of the payment result, and the merchant server will send a response to the WeChat server after processing the order.

3.PHP implementation method
The following is the implementation method of WeChat applet payment based on PHP:

3.1 Merchant server ordering
After the merchant server receives the user's order request, It is necessary to generate a prepayment order and return the prepayment order information to the applet. The following is the PHP code implementation for placing an order:

<?php
public function unifiedOrder($data)
{
    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    $data['appid'] = $this->appId;
    $data['mch_id'] = $this->mchId;
    $data['nonce_str'] = $this->getNonceStr();
    $data['notify_url'] = $this->notifyUrl;
    $data['trade_type'] = 'JSAPI';
    $data['openid'] = $openid;
    $data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
    $data['sign'] = $this->sign($data);

    $xml = $this->arrayToXml($data);
    $response = $this->postXmlCurl($xml, $url);
    $result = $this->xmlToArray($response);
    return $result;
}
Copy after login

3.2 Mini program calls up payment
After the mini program receives the prepayment order, it needs to call the WeChat payment API for payment. The following is the PHP code implementation of the payment call:

<?php
public function getPayParams($prepayId)
{
    $data['appId'] = $this->appId;
    $data['timeStamp'] = time();
    $data['nonceStr'] = $this->getNonceStr();
    $data['package'] = 'prepay_id=' . $prepayId;
    $data['signType'] = 'MD5';
    $data['paySign'] = $this->sign($data);

    return $data;
}
Copy after login

3.3 Payment result notification
After the WeChat payment is completed, the WeChat server will notify the merchant server of the payment result. The following is the PHP code implementation of payment result notification:

<?php
public function notify()
{
    $xml = file_get_contents("php://input");
    $data = $this->xmlToArray($xml);

    if ($this->checkSign($data)) {
        //处理支付结果
        $orderNo = $data['out_trade_no'];
        $transactionId = $data['transaction_id'];
        //返回成功信息
        return $this->arrayToXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
    } else {
        return $this->arrayToXml(['return_code' => 'FAIL', 'return_msg' => '签名验证失败']);
    }
}
Copy after login

4. Summary
Through the implementation of the above steps, we can easily access the WeChat mini program payment function and provide mini program users with a more convenient payment method. . It should be noted that the security and accuracy of data need to be ensured during the payment process, while potential payment risks must be avoided.

The above is the detailed content of PHP implementation method related to WeChat mini program payment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

If you forget your WeChat payment password, how to retrieve it? If you forget your WeChat payment password, how to retrieve it? Feb 23, 2024 pm 09:40 PM

In WeChat, users can enter their payment password to make purchases, but how do they retrieve their payment password if they forget it? Users need to go to My-Services-Wallet-Payment Settings-to recover their payment password if they forget it. This introduction to how to retrieve your payment password if you forget it will tell you the specific operation method. The following is a detailed introduction, so take a look! WeChat usage tutorial. How to find the WeChat payment password if you forget it? Answer: My-Service-Wallet-Payment Settings-Forgot payment password. Specific method: 1. First, click My. 2. Click on the service inside. 3. Click on the wallet inside. 4. Find the payment settings. 5. Click Forgot payment password. 6. Enter your own information for verification. 7. Then enter the new payment password to change it.

What should I do if I forget my WeChat payment password? What should I do if I forget my WeChat payment password? Jan 08, 2024 pm 05:02 PM

Solution for forgetting WeChat payment password: 1. Open WeChat APP, click "I" in the lower right corner to enter the personal center page; 2. In the personal center page, click "Pay" to enter the payment page; 3. On the payment page , click "..." in the upper right corner to enter the payment management page; 4. In the payment management page, find and click "Forgot payment password"; 5. Follow the page prompts and enter personal information for identity verification. After successful verification, you can Choose the method of "retrieve by swiping your face" or "retrieve by verifying bank card information" to retrieve your password, etc.

How to set up WeChat payment for Meituan Takeout How to set up WeChat payment How to set up WeChat payment for Meituan Takeout How to set up WeChat payment Mar 12, 2024 pm 10:34 PM

There are many food and snack shops provided in the Meituan takeout app, and all mobile phone users log in through their accounts. Add your personal delivery address and contact number to enjoy the most convenient takeout service. Open the homepage of the software, enter product keywords, and search online to find the corresponding product results. Just swipe up or down to purchase and place an order. The platform will also recommend dozens of nearby restaurants with high reviews based on the delivery address provided by the user. The store can also set up different payment methods. You can place an order with one click to complete the order. The rider can arrange the delivery immediately and the delivery speed is very fast. There are also takeout red envelopes of different amounts for use. Now the editor is online in detail for Meituan takeout users. We show you how to set up WeChat payment. 1. After selecting the product, submit the order and click Now

How to set the order of deduction for WeChat payment How to set the order of deduction for WeChat payment Sep 06, 2023 am 11:11 AM

Steps to set the order of deductions for WeChat payment: 1. Open the WeChat APP, click on the "Me" interface, click on "Services", and then click on "Collect and Payment"; 2. Click on "Prioritize Use This Payment Method" under the payment code on the collection and payment interface; 3. Select the preferred payment method you need.

Can Xianyu pay with WeChat? How to change to WeChat payment method? Can Xianyu pay with WeChat? How to change to WeChat payment method? Mar 12, 2024 pm 12:19 PM

When everyone has nothing to do, they will choose to browse the Xianyu platform. Everyone can find that there are a large number of products on this platform, which can allow everyone to see various second-hand products. Although these products are second-hand products, there is absolutely no problem with the quality of these products, so everyone can buy them with confidence. The prices are very affordable, and they still allow everyone to face-to-face with these products. It is entirely possible for sellers to communicate and conduct some price bargaining operations. As long as everyone negotiates properly, then you can choose to conduct transactions, and when everyone pays here, they want to make WeChat payment, but it seems that the platform It's not allowed. Please follow the editor to find out what the specific situation is. Xianyu

Can WeChat payment be canceled immediately after successful payment? Can WeChat payment be canceled immediately after successful payment? Nov 29, 2023 pm 02:19 PM

WeChat payment cannot be canceled immediately after successful payment. Refunds usually need to meet the following conditions: 1. The merchant's refund policy. The merchant will formulate its own refund policy, including the refund time window, refund amount and refund method; 2. Payment time, refunds usually require Apply within a certain time frame, and refunds may not be possible beyond this time frame; 3. Goods or service status. If the user has received the goods or enjoyed the service, the merchant may require the user to return the goods or provide corresponding proof; 4. Refund process, etc.

Sharing the steps to apply for a refund with WeChat Pay Sharing the steps to apply for a refund with WeChat Pay Mar 25, 2024 pm 06:31 PM

1. First, we need to open the WeChat APP on the mobile phone, and then click to log in to the WeChat account, so that we enter the WeChat homepage. 2. Click the [Me] button in the lower right corner of the WeChat homepage, then select the [Payment] option. We click to enter the payment page. 3. After entering the [Payment] page, click the [Wallet] option to enter, and click [Bill] in the upper right corner of the [Wallet] page.

How to pay with WeChat on Alibaba_How to pay with WeChat on Alibaba 1688 How to pay with WeChat on Alibaba_How to pay with WeChat on Alibaba 1688 Mar 20, 2024 pm 05:51 PM

Alibaba 1688 is a purchasing and wholesale website, and the items there are much cheaper than Taobao. So how does Alibaba use WeChat payment? The editor has compiled some relevant content to share with you. Friends in need can come and take a look. How does Alibaba use WeChat payment? Answer: WeChat payment cannot be used for the time being; 1. On the page where we purchase goods, we click [Change payment method] 2. Then in the pop-up page, we can only go to [Alipay, staged payment] , cashier] can be selected;

See all articles