123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- // #ifndef APP-PLUS
- //const jweixin = require('/lib/jweixin.js');
- import jweixin from '/lib/jweixin.js';
- // #endif
- import dever from './core';
- import html from './html';
- var pay = {
- init: function(doc) {
- html.init(doc);
- }
- // 支付
- payment: function(type, order_no, param) {
- // 回跳页面地址
- if (type == 'alipay_app' || type == 'wechat_app' || type == 'wechat_xcx') {
- this.appPayment(type, param, function(res) {
- uni.redirectTo({
- url: '/pages/payment/callback?order_no=' + order_no
- })
- }, function(err) {
- tool.msg('支付异常,请重下单重试');
- tool.payBack();
- });
- return;
- }
- if (type == 'alipay_wap' || type == 'wechat_wap') {
- html.insert(param);
- uni.redirectTo({
- url: '/pages/payment/callback?order_no=' + order_no
- })
- return;
- }
- if (type == 'wechat_gzh') {
- this.wxJSApiPay(param, (res) => {
- tool.msg('支付成功', 'success');
- uni.redirectTo({
- url: '/pages/payment/callback?order_no=' + order_no
- })
- }, (err) => {
- tool.msg('支付异常,请重下单重试');
- tool.payBack();
- });
- return;
- }
- if (type == 'balance' && param.code == 1) {
- uni.redirectTo({
- url: '/pages/payment/callback?order_no=' + order_no
- })
- return;
- }
- },
- // app支付
- appPayment: function(type, param, callback, errorCallback) {
- let paymentType;
- switch (type) {
- // 微信APP
- case 'wechat_app':
- paymentType = 'wxpay'
- break;
- // 微信小程序
- case 'wechat_xcx':
- paymentType = 'wxpay'
- break;
- // 阿里APP支付
- case 'alipay_app':
- paymentType = 'alipay'
- break;
- }
- uni.requestPayment({
- provider: paymentType,
- orderInfo: param,
- success: function(res) {
- if (callback) {
- callback(res);
- }
- },
- fail: function(err) {
- console.log('fail:' + JSON.stringify(err));
- if (errorCallback) {
- errorCallback(err)
- }
- }
- });
- },
- // 微信公众号支付
- wxJSApiPay: function(data, callback, errorCallback) {
- jweixin.config({
- debug: false, // 开启调试模式
- appId: data.appId, // 必填,公众号的唯一标识
- timestamp: data.timeStamp, // 必填,生成签名的时间戳
- nonceStr: data.nonceStr, // 必填,生成签名的随机串
- signature: data.paySign, // 必填,签名,见附录1
- jsApiList: ['checkJsApi', 'startRecord', 'stopRecord', 'translateVoice', 'scanQRCode',
- 'openCard'
- ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
- });
- jweixin.ready(function() {
- jweixin.checkJsApi({
- jsApiList: ['checkJsApi', 'startRecord', 'stopRecord', 'translateVoice',
- 'scanQRCode', 'openCard'
- ],
- success: (res) => {},
- fail: function(res) {
- tool.msg('支付呼起失败');
- }
- });
- jweixin.chooseWXPay({
- timestamp: data.timeStamp,
- nonceStr: data.nonceStr,
- package: data.package,
- signType: data.signType,
- paySign: data.paySign,
- success: (res) => {
- tool.msg(res);
- // 前端判断返回方式,微信团队郑重提示:不保证绝对可靠,切记!
- if (res.errMsg == 'chooseWXPay:ok' && callback) {
- callback(res);
- } else {
- tool.msg('支付失败,请重试');
- }
- },
- cancel: (r) => {
- tool.msg('支付已取消');
- },
- fail: (err) => {
- if (errorCallback) {
- errorCallback(err)
- }
- }
- });
- });
- // config信息验证失败会执行error函数,
- jweixin.error(function(res) {
- console.log('jweixin.error失败:', res)
- });
- }
- }
- module.exports = pay;
- /*
- const pay = {
- //拉起支付
- pay : function(result, location, error) {
- var self = this;
- if (!error) {
- if (location.indexOf('?') == -1) {
- var fix = '?';
- } else {
- var fix = '&';
- }
- location = location + fix + 'order_id=' + result.order_id;
- }
-
- if (result.type == 'test') {
- self.alert('支付成功','none', function() {
- self.location(location, 'to');
- })
- } else if (self.source == 'h5') {
- uni.hideLoading();
- uni.showLoading({title: '跳转中', mask: true});
- self.html(result.order);
- } else if (self.source == 'app') {
- self.appPayment(result.type, result.order, function() {
- self.location(location, 'to');
- }, function() {
- if (!error) {
- self.location(location + '&error=1', 'to');
- } else {
- self.alert(error);
- }
- });
- }
- },
-
- //app支付
- appPayment : function(type, order, callback, errorCallback) {
- uni.requestPayment({
- provider: type,
- orderInfo: order, //微信、支付宝订单数据
- success: function (res) {
- //console.log('success:' + JSON.stringify(res));
- callback(res);
- },
- fail: function (err) {
- //console.log('fail:' + JSON.stringify(err));
- errorCallback(err)
- }
- });
- },
- }*/
|