Implementation of WeChat mini-program payment function (front-end) and mini-program payment function

Source: Internet
Author: User
Tags openid

Implementation of the applet payment function (front-end) and the applet payment function

Implementation of the mini-program payment function (front-end)

Only the code on the applet is provided:

Var app = getApp (); Page ({data :{}, onLoad: function (options) {// The parameter var that = this brought about by PAGE initialization options for page Jump // log on to get code wx. login ({success: function (res) {console. log (res. code) // obtain openid that. getOpenId (res. code) }}) ;}, getOpenId: function (code) {var that = this; wx. request ({url: "https://api.weixin.qq.com/sns/jscode2session? Appid = applet appid & secret = applet application key & js_code = "+ code +" & grant_type = authorization_code ", data :{}, method: 'get', success: function (res) {that. generateOrder (res. data. openid)}, fail: function () {// fail}, complete: function () {// complete})},/** generate Merchant Order */generateOrder: function (openid) {var that = this // unified payment wx. request ({url: 'background path', method: 'get', data: {gbid: 'item price', gname: 'item name', openId: openid (whether to pass the value of the product price and product name according to your own needs. openid is required)}, success: function (res) {var pay = res. data // initiate payment var timeStamp = pay [0]. timeStamp; var packages = pay [0]. package; var paySign = pay [0]. paySign; var nonceStr = pay [0]. nonceStr; var param = {"timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; that. pay (param) },},/* pay */pay: function (param) {console. log ("Payment") console. log (param) wx. requestPayment ({timeStamp: param. timeStamp, nonceStr: param. nonceStr, package: param. package, signType: param. signType, paySign: param. paySign, success: function (res) {// success wx. navigateBack ({delta: 1, // The success: function (res) {wx. showToast ({title: 'payment successfully', icon: 'success ', duration: 2000})}, fail: function () {// fail}, complete: function () {// complete}, fail: function (res) {// fail}, complete: function () {// complete }})}})

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.