SoFunction
Updated on 2025-04-07

Uniapp implements detailed code for WeChat applet payment (front-end)

1. This article mainly introduces the WeChat payment process of uniapp WeChat applet and the WeChat payment process of the built-in web page of the mini program to jump to H5 pages.

2. WeChat payment within WeChat mini program

({
    url:"Request Path",
    method:"method",
    data:{
        Data required by the backend
    },
    success(payRes){
        //payRes has several main parameters here that will call uni's payment interface to use. The following will introduce each data        //After this request is the most important step for the front-end, you need to pull up the payment password box to complete the payment        
        ({
		    "provider": "wxpay",//Payment method			"timeStamp":,//Time stamp			"nonceStr":,//Random string			"package":,//The prepay_id returned by the interface			"signType":,//Signature algorithm, needs to be consistent with the background ordering			"paySign":,sign
			success: function (resSuccess) {
				// When triggering this success, it is triggered after entering the password to pay successfully. You can do some page jumps and success prompts.             },
									
			fail: function (err) {
                //Distance when payment fails, such as cancellation of payment, insufficient balance, etc.			}
		});
        
    }
})

3. Redirect the H5 page to WeChat payment within the mini program

1. Because H5 payment is not an internal WeChat payment, it needs to be installed using WeChat's JS-SDK package, so the implementation writing method will change, but it is similar.

2. Now enter the command on the console (if installed, I am the version 16.6 here) npm i jweixin-module -S

<script>
//There are two ways to introduce them here. One is the node's requirement, and the other is the commonly used import method. The effects are the same.const jweixin = require("jweixin ")

export default {
    data(){
        return {

        }        
    },

    methods:{

        ({
            url:"Request Path",
            method:"method",
            data:{
                Data required by the backend
            },
            success(payRes)=>{

                 payMent(){

                //First, initialize jweixin to configure                ({
                    debug:false,//Whether to enable scheduling mode, there will be pop-up windows when opening test payment.                    appId: , // The unique identifier of the official account					timestamp: , // Generate the signature timestamp					nonceStr: , // Generate a random string of signatures					signature: , //sign					jsApiList: ['chooseWXPay'] // Required                })

                ({
	                timestamp: , // Pay signature timestamp					nonceStr: , // Payment signature random string, no longer than 32 digits					package: , // The prepay_id parameter value returned by the unified payment interface					signType: , // Signature method					paySign: , // Payment signature                    success(res){
                        //success                    },
                    fail(res){
                        //fail                    }
                })
             }
        
            }
        })
    }
}
</scrpit>

4. How to jump to other H5 pages with the built-in browser for applets

1. The mini program cannot directly jump to a certain web page (I didn't find it), but you can use <web-view> to nest H5 web pages, default full screen, and the jump URL is written in src.

2. The H5 page here is generated by uni. The automatically generated H5 project is in hash mode. If we need to pass parameters to the H5 page, we must bring #/ when writing the URL in src, for example: http://192.168.0.1/#/?id=sadas. You can also change the routing mode to history in the lieutenant

3. I wrote a separate page on the web-view and passed the H5 URL a web-view page through the route parameter transmission method (because the user information needs to be passed in). Due to the routing length, I need to use encodeURIComponent to convert the format first, and then use decodeURIComponent to parse the incoming parameters on the web-view page.

//Jump page&lt;script&gt;
export default{
    methods:{
        goPage(){
            ({
                url:`/page/webView/webView?url=${encodeURIComponent("Access Address"))}`
            })
        }
    }        
}
&lt;/script&gt;
//web-view
&lt;template&gt;
    &lt;web-view :src="url"&gt;&lt;/web-view&gt;
&lt;/template&gt;
&lt;script&gt;
export default{
    data(){
        return {
            url:""
        }
    }
    onLoad(option){
         =decodeURIComponen()
    }
}
&lt;/script&gt;

5. Problems encountered in the process

1. WeChat payment written by H5 cannot be used on Android, ios is fine, I searched for a long time but couldn't find a solution

Summarize

This is the article about uniapp implementing WeChat mini program payment (front-end). For more related uniapp WeChat mini program payment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!