Home Web Front-end uni-app How to make WeChat scan code payment in uniapp

How to make WeChat scan code payment in uniapp

Apr 20, 2023 am 09:07 AM

Uniapp is a cross-platform development framework that can be used to develop iOS, Android and Web applications. When developing WeChat applet or application, you need to add WeChat payment function. This article will introduce how to implement WeChat scan code payment in uniapp.

1. Apply for WeChat Payment

First of all, you need to register and apply for WeChat Payment. For the application process, please refer to the official WeChat Payment document. After successful application, you can obtain various certificates, keys and other information necessary for WeChat payment.

2. Install the uni-pay plug-in

In uniapp, you need to install the uni-pay plug-in to use the WeChat payment function. Open HBuildX, select the plug-in market, search and select the uni-pay plug-in to install.

3. Create a payment request

On the page where payment is required, add a payment button. When the payment button is clicked, a request is sent to the server, the payment parameters are obtained, the payment QR code is generated and displayed to on the page. The code example is as follows:

uni.request({
  url: 'https://api.example.com/pay.php',
  data: {
    // 微信支付必需参数
    appid: 'wx8888888888888888', // 公众账号ID
    mch_id: '1900007811', // 商户号
    body: '商品名称', // 商品描述
    out_trade_no: '202001010101', // 商户订单号
    total_fee: 88, // 总金额
    spbill_create_ip: '127.0.0.1', // 终端IP
    notify_url: 'https://api.example.com/notify.php', // 通知地址
    trade_type: 'NATIVE' // 交易类型
  },
  success: function(res) {
    // 生成二维码
    let imageUrl = res.data.code_url;
    // 将imageUrl放到<image src="{{ imageUrl }}"></image> 标签中
  }
})
Copy after login

4. Payment result notification

When the user scans the payment QR code and completes the payment, he or she will receive a payment result notification. On the server side, it is necessary to develop an interface for receiving payment result notifications, verify the validity of the received notifications, and update the order status. The code example is as follows:

$input_data = file_get_contents("php://input");
$data = json_decode($input_data, true);
if (isset($data['transaction_id'])) {
  // 支付成功
  // 更新订单状态
  // 返回通知
  echo json_encode([
    'return_code' => 'SUCCESS',
    'return_msg' => 'OK'
  ]);
} else {
  // 支付失败
}
Copy after login

5. Summary

The above is the basic process of implementing WeChat scan code payment in uniapp. It should be noted that the WeChat payment interface involves sensitive information, so data security needs to be ensured during development. In order to prevent problems such as repeated submissions, some processing logic needs to be added to the payment page. Through the above steps, developers can quickly and easily implement the WeChat QR code payment function in uniapp.

The above is the detailed content of How to make WeChat scan code payment in uniapp. 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24