Home Web Front-end uni-app How to call WeChat payment in uniapp

How to call WeChat payment in uniapp

May 26, 2023 pm 01:52 PM

With the popularity of mobile payment, WeChat payment is becoming more and more widely used on the mobile terminal. When developing some mobile applications, many developers will integrate WeChat payment functions to enable users to pay online. WeChat payment can also be integrated into applications developed based on uniapp. Let's take a look at how uniapp calls WeChat payment.

1. First obtain WeChat payment parameters

Before calling WeChat payment, you need to obtain WeChat payment parameter information and generate a unique order number. Obtaining WeChat payment parameters requires calling the backend interface. We assume that the parameters returned by the interface include the following parameters:

Parameter name Type Description
appId string The appId of WeChat developer platform, used for payment signature verification
nonceStr string Random string used for payment signature verification
timeStamp string Time stamp, used for payment signature verification
#package string The prepay_id parameter returned by the unified order interface Value, used to initiate WeChat payment
signType string Signature type, currently supports MD5 and HMAC-SHA256, the default is MD5

After obtaining these parameters, the front end needs to pass them to the WeChat payment interface.

2. Call up WeChat payment

In uniapp, to call WeChat payment, you only need to call the API provided by uniapp. The API code is as follows:

uni.requestPayment({
    provider: 'wxpay',
    timeStamp: payParams.timeStamp,
    nonceStr: payParams.nonceStr,
    package: payParams.package,
    signType: payParams.signType,
    paySign: payParams.paySign,
    success: function (res) {
        console.log('支付成功');
    },
    fail: function (res) {
        console.log(res);
        console.log('支付失败');
    }
});
Copy after login

Things to note Yes, payParams is the object of payment parameters, which has been obtained previously. Here you need to pass it as a parameter to the uni.requestPayment() function. In addition, the provider parameter needs to be set to wxpay, indicating the use of WeChat payment.

After successfully calling uni.requestPayment(), the WeChat payment page will automatically open, and the user can complete the payment operation on this page. If the user's payment is successful, the success callback function will be triggered; if the user's payment fails, the fail callback function will be triggered.

3. Payment result verification

After the user completes the payment, in order to ensure the security of the payment, the payment result needs to be verified. The verification method is to send the query order interface to the background to obtain the payment result status.

The way to send requests in uniapp is very similar to other front-end frameworks, so I won’t go into details here. However, it should be noted that the order number needs to be passed to the backend in the query order interface. The backend queries the payment result status through the order number and returns the results to the frontend. If the payment result is successful, it means that the order has been paid and the developer can perform subsequent operations on the front end.

Summary:

Through the above steps, we can integrate the WeChat payment function in uniapp very simply. However, it should be noted that WeChat Pay requires developers to first register and activate on the WeChat Developer Platform, and they need to generate orders and other related information on their own backend servers. With this information, we can jump to the payment page on the front end and verify the payment results.

When developing WeChat applets or mobile applications, if you need to use WeChat payment, you can implement it according to the above reference code.

The above is the detailed content of How to call WeChat 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

How do I use preprocessors (Sass, Less) with uni-app? How do I use preprocessors (Sass, Less) with uni-app? Mar 18, 2025 pm 12:20 PM

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

How do I use uni-app's animation API? How do I use uni-app's animation API? Mar 18, 2025 pm 12:21 PM

The article explains how to use uni-app's animation API, detailing steps to create and apply animations, key functions, and methods to combine and control animation timing.Character count: 159

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

What debugging tools are available for UniApp development? What debugging tools are available for UniApp development? Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? Mar 18, 2025 pm 12:22 PM

The article explains how to use uni-app's storage APIs (uni.setStorage, uni.getStorage) for local data management, discusses best practices, troubleshooting, and highlights limitations and considerations for effective use.

How do I use uni-app's API for accessing device features (camera, geolocation, etc.)? How do I use uni-app's API for accessing device features (camera, geolocation, etc.)? Mar 18, 2025 pm 12:06 PM

The article discusses using uni-app's APIs to access device features like camera and geolocation, including permission settings and error handling.Character count: 158

How do I validate user input in uni-app? How do I validate user input in uni-app? Mar 18, 2025 pm 12:17 PM

The article discusses validating user input in uni-app using JavaScript and data binding, emphasizing both client and server-side validation for data integrity. Plugins like uni-validate are recommended for form validation.

See all articles