更新记录

1.1.1(2023-08-15)

新增抖音支付!抖音小程序担保支付

1.0.1(2023-03-11)

上传示例项目

1.0.0(2023-03-11)

第一次发布

查看更多

平台兼容性

阿里云 腾讯云 支付宝云
×

云函数类插件通用教程

使用云函数类插件的前提是:使用HBuilderX 2.9+


微信支付v3支持

  • 微信native(二维码付款)
  • 微信JSAPI(小程序/公众号支付)
  • 微信APP支付
  • 微信(消费者投诉2.0)
  • 商家转载到零钱v3
  • 企业付款到零钱v2
  • 现金红包v2
  • 更多功能自行测试,支持大部分接口

支付宝支付【证书模式】

  • 支付宝(当面付)
  • 支付宝(APP支付)
  • 统一下单接口(小程序支付)
  • 转账到支付宝
  • 查询支付宝余额
  • 支付宝小程序登录
  • 现金红包
  • 更多功能自行测试(如支付宝小程序相关接口一样适用),支持大部分接口

抖音小程序支付

  • 支付
  • 验签
  • 退款
  • 查询订单状态,等

下载后需要初始化 db_init.json 创建 mymp-pay-certificates 数据表,用于存放【微信支付平台证书】 请先试用,在购买!

使用方法

  • 微信支付

    'use strict';
    const db = uniCloud.database();
    const getNotifyUrl = function(cloudInfo,payid,notify){
        let notifyUrl = '';
        if(cloudInfo.provider=='tencent'){
            notifyUrl = `https://${cloudInfo.spaceId}.service.tcloudbase.com/pay/${notify}/${payid}`;
        }else if(cloudInfo.provider=='aliyun'){
            notifyUrl = `https://fc-${cloudInfo.spaceId}.next.bspapp.com/pay/${notify}/${payid}`;
        }
        return notifyUrl;
    };
    const getOrder = async function(){
        let money = Math.ceil(Math.random()*10);
        let create_order = await db.collection('mymp-pay-order').add({
            money: money,
            state: -1,
            create_date: Date.now()
        });
        return {
            id: create_order.id,
            money: money
        };
    };
    const returnError = function(){
        return {
            mpserverlessComposedResponse: true,
            statusCode: 400,
            headers: {
                'content-type': 'application/json'
            },
            body: JSON.stringify({"code": "FAIL","message": "失败"})
        };
    };
    const returnSuccess = function(){
        return {
            mpserverlessComposedResponse: true,
            statusCode: 200,
            headers: {
                'content-type': 'text/html'
            },
            body: ''
        };
    };
    const MympPay = require('mymp-pay');
    const wxData = require("./data/wx_pay.js");
    /**
    const wxData = {
        pay_type: 'weixin',//固定值 weixin
        apiclient_cert: '',
        apiclient_key: '',
        merchant_certificate_serial: '证书序列号 微信支付后台查看',
        merchant_id: '微信支付商户号',
        v3_key: 'APIv3密钥',
        v2_key: 'APIv2密钥 可留空',
    };
     */
    const aliData = require("./data/my_alipay.js");
    /**
    const wxData = {
        pay_type: 'alipay',//固定值 alipay
        app_id: '',
        private_key: '',
        app_cert: '',
        alipay_public_cert: '',
        alipay_root_cert: ''
    };
     */
    module.exports = {
        async _before(){
            this.cloudInfo = this.getCloudInfo();
            this.wxpay = await MympPay(wxData);
            this.alipay = await MympPay(aliData);
        },
        /**
         * 前端查询订单支付状态
         */
        async query_order(e){
            let dd = await db.collection('mymp-pay-order').where({
                _id: e.order_id
            }).limit(1).get();
            if(!dd.data.length){
                return {
                    errCode: 'error',
                    errMsg: '订单不存在'
                };
            }
            if(dd.data[0].state===0){
                return Object.assign({errCode: 0}, dd.data[0]);
            }else{
                return {
                    errCode: 'error',
                    errMsg: '订单状态未更新'
                };
            }
        },
        /**
         * 微信支付结果通知
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml
         */
        async wx_notify(){
            const httpInfo = this.getHttpInfo();
            let {
                'wechatpay-timestamp': wxTimestamp, 
                'wechatpay-nonce': wxNonce, 
                'wechatpay-serial': wxSerial, 
                'wechatpay-signature': wxSignature
            } = httpInfo.headers;//微信支付验签必要参数
            if(!wxTimestamp||!wxNonce||!wxSerial||!wxSignature){
                return returnError();
            }
            //获取订单号
            let arrID = httpInfo.path.split('/');
            let orderID = arrID[arrID.length-1]||arrID[arrID.length-2];
            if(!orderID){
                return returnError();
            }
            //查询订单
            let orderDT = await db.collection('mymp-pay-order').where({
                _id: orderID
            }).limit(1).get();
            if(!orderDT.data.length){
                return returnError();
            }
            let orderDATA = orderDT.data[0];
            if(orderDATA.state===0){
                return returnError();
            }
            //验证签名 云函数可直接传入 event
            let isSignaTure = await this.wxpay.v3.verifyNotify(httpInfo);
            if(!isSignaTure){
                return returnError();
            }
            //解密数据
            let decryptData = await this.wxpay.v3.decryptData(httpInfo);
            if(decryptData.event_type==='TRANSACTION.SUCCESS'){//TRANSACTION.SUCCESS 是支付结果通知
                if(decryptData.decrypt_data.trade_state==='SUCCESS'){
                    //对比商户号
                    if(decryptData.decrypt_data.mchid!==this.wxpay.mchid()){
                        return returnError();
                    }
                    //对比商户订单ID
                    if(decryptData.decrypt_data.out_trade_no!==orderDATA._id){
                        return returnError();
                    }
                    //对比支付金额
                    if(decryptData.decrypt_data.amount.total!==orderDATA.money){
                        return returnError();
                    }
                    //支付成功逻辑
                    await db.collection('mymp-pay-order').doc(orderDATA._id).update({
                        state: 0
                    });
                    return returnSuccess();
                }
            }
            return returnError();
        },
        /**
         * 微信native下单API(二维码收款)
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
         */
        async wx_native(){
            let order = await getOrder();
            let payBody = {
                "mchid": this.wxpay.mchid(),//获取微信商户号
                "out_trade_no": order.id,
                "appid": "wx127dcd247234c521",
                "description": "Image形象店-深圳腾大-QQ公仔",
                "notify_url": getNotifyUrl(this.cloudInfo, order.id, 'wx_notify'),
                "amount": {
                    "total": order.money,
                    "currency": "CNY"
                }
            };
            let res = await this.wxpay.v3.native(payBody);
            return Object.assign({order_id: order.id}, res);
        },
        /**
         * 微信JSAPI下单(公众号/小程序)
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
         */
        async wx_jsapi(){
            let order = await getOrder();
            var payBody = {
                "mchid": this.wxpay.mchid(),//获取微信商户号
                "out_trade_no": order.id,
                "appid": "wx9f03fc0ef91de25d",
                "description": "Image形象店-深圳腾大-QQ公仔",
                "notify_url": getNotifyUrl(this.cloudInfo, order.id, 'wx_notify'),
                "amount": {
                    "total": order.money,
                    "currency": "CNY"
                },
                "payer": {
                    "openid": "o0Vjz5b-xmVyhLGo5Uz9MCifNK84"
                }
            };
            let res = await this.wxpay.v3.jsapi(payBody);
            return Object.assign({order_id: order.id}, res);
        },
        /**
         * 微信APP支付
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml
         */
        async wx_app(){
            let order = await getOrder();
            var payBody = {
                "mchid": this.wxpay.mchid(),//获取微信商户号
                "out_trade_no": order.id,
                "appid": "wx9f03fc0ef91de25d",
                "description": "Image形象店-深圳腾大-QQ公仔",
                "notify_url": getNotifyUrl(this.cloudInfo, order.id, 'wx_notify'),
                "amount": {
                    "total": order.money,
                    "currency": "CNY"
                }
            };
            let res = await this.wxpay.v3.app(payBody);
            return Object.assign({order_id: order.id}, res);
        },
        /**
         * 微信 退款
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml
         */
        async wx_refunds(e){
            let dd = await db.collection('mymp-pay-order').where({
                _id: e.order_id
            }).limit(1).get();
            if(!dd.data.length){
                return {
                    errCode: 'error',
                    errMsg: '订单不存在'
                };
            }
            //openApi是通用方法,支持大部分接口
            let res = await this.wxpay.v3.openApi({
                url: '/v3/refund/domestic/refunds',
                method: 'POST',
                data: {
                    "out_trade_no": e.order_id,
                    "out_refund_no": "refunds_"+e.order_id,
                    "reason": "退款",
                    "amount": {
                        "refund": dd.data[0].money,//退款金额
                        "total": dd.data[0].money,//原订单金额
                        "currency": "CNY"
                    }
                }
            });
            if(res.status===200){
                if(res.data.out_refund_no&&res.data.refund_id){
                    return {
                        errCode: 0
                    };
                }
            }
            return {
                errCode: 'error',
                errMsg: '操作失败,同一个退款单号多次执行不会多退,可再次重试!'
            };
        },
        //订单操作
        async wx_order(){
            //https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_2.shtml
            //1、微信支付订单号查询
            let transaction_id = '4200001447202206133476334683';
            let res = await this.wxpay.v3.transaction_id.query(transaction_id);
            return res;
    
            //2、商户订单号查询
            let out_trade_no = '121775250122141655069686054';
            let res1 = await this.wxpay.v3.out_trade_no.query(out_trade_no);
            return res1;    
    
            //关闭订单
            //https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_3.shtml
            let res2 = await this.wxpay.v3.out_trade_no.close(out_trade_no);
            return res2;
        },
    
        //通用 openApi 方法,openApi支持大部分接口,
        //https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_5.shtml(对应接口使用方法)
        async wx_open_api(){
            //比如合单支付
            let res = await this.wxpay.v3.openApi({
                url: '/v3/combine-transactions/native',//直接填接口的链接,去掉域名部分
                method: 'POST',//请求方式 有些接口可能是 GET
                //请求参数 如果参数为空 可以 data: ''
                data: {
                    "combine_appid": "wx9f03fc0ef91de25d",
                    "combine_out_trade_no": "2015"+Date.now(),
                    "combine_mchid": this.wxpay.mchid(),
                    "scene_info": {
                        "device_id": "POS1:123",
                        "payer_client_ip": "14.17.22.32"
                    },
                    "sub_orders": [{
                        "mchid": this.wxpay.mchid(),
                        "attach": "深圳分店",
                        "amount": {
                            "total_amount": 10,
                            "currency": "CNY"
                        },
                        "out_trade_no": "2016"+Date.now(),
                        "description": "腾讯充值中心-QQ会员充值",
                        "settle_info": {
                            "profit_sharing": true,
                            "subsidy_amount": 10
                        }
                    }, {
                        "mchid": this.wxpay.mchid(),
                        "attach": "深圳分店",
                        "amount": {
                            "total_amount": 10,
                            "currency": "CNY"
                        },
                        "out_trade_no": "2017"+Date.now(),
                        "description": "腾讯充值中心-QQ会员充值",
                        "settle_info": {
                            "profit_sharing": true,
                            "subsidy_amount": 10
                        }
                    }],
                    "notify_url": "https://yourapp.com/notify"
                }
            });
            if(res.status===200){
                if(res.data.code_url){
                    return {
                        errCode: 0,
                        data: res.data
                    };
                }
            }
            return {
                errCode: 'error',
                errMsg: JSON.stringify(res.data)
            };
    
        },
    
        //通用 openApi 方法,openApi支持大部分接口,
        //https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_11.shtml(消费者投诉2.0)
        async wx_open_api2(){
            let uurl = '/v3/merchant-service/complaints-v2?';
                uurl += `limit=10&offset=0&begin_date=2023-01-01&end_date=2023-01-29&complainted_mchid=${this.wxpay.mchid()}`;
            let res = await this.wxpay.v3.openApi({
                url: uurl,
                method: 'GET',
                data: ''
            });
    
            //敏感信息解密,(手机号码)
            //let payer_phone = this.wxpay.v3.rsa.decrypt(data.payer_phone);
    
            return res;
        },
    
        /**
         * 商家转载到零钱
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_1.shtml
         */
        async wx_transfer_v3(){
            //openApi是通用方法,支持大部分接口
            let res = await this.wxpay.v3.openApi({
                url: '/v3/transfer/batches',
                method: 'POST',
                data: {
                    "appid": "wx9f03fc0ef91de25d",
                    "out_batch_no": Date.now().toString(),
                    "batch_name": "2019年1月深圳分部报销单",
                    "batch_remark": "2019年1月深圳分部报销单",
                    "total_amount": 50,
                    "total_num": 1,
                    "transfer_detail_list": [
                        {
                            "out_detail_no": Date.now().toString(),
                            "transfer_amount": 50,
                            "transfer_remark": "2020年4月报销",
                            "openid": "o0Vjz5b-xmVyhLGo5Uz9MCifNK84"
                        }
                    ]
                }
            });
            if(res.status===200){
                if(res.data.out_batch_no&&res.data.batch_id){
                    return {
                        errCode: 0,
                        data: res.data
                    };
                }
            }
            return {
                errCode: 'error',
                errMsg: JSON.stringify(res.data)
            };
        },
    
        /**
         * 企业付款到零钱
         * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_1.shtml
         */
        async wx_transfer_v2(){
            let timestamp = Date.now();
            let postDts = {
                mch_appid: 'wx127dcd247234c521',
                mchid: this.wxpay.mchid(),
                nonce_str: "q1"+timestamp,
                partner_trade_no: "qq"+timestamp,
                openid: 'oFWHj5ERyb84vu61c5dirZEJNWi8',
                check_name: 'NO_CHECK',
                amount: 40 + Math.ceil(Math.random()*10),
                desc: '节日快乐~~'
            };
            let res = await this.wxpay.v2.transfers(postDts);
            return res;
        },
        /**
         * 查询 企业付款到零钱v2
         * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
         */
        async query_wx_transfer_v2(){
            let postDts = {
                appid: 'wx127dcd247234c521',
                mch_id: this.wxpay.mchid(),
                nonce_str: "随机字符串",
                partner_trade_no: "商户订单号"
            };
            let res = await this.wxpay.v2.gettransferinfo(postDts);
            return res; 
        },
        /**
         * 现金红包v2
         * https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
         */
        async wx_sendredpack_v2(){
            let clientInfo = this.getClientInfo();
            let timestamp = Date.now();
            let postDts = {
                wxappid: 'wxe758de3e77f7535a',
                mch_id: this.wxpay.mchid(),
                nonce_str: "q12177525012214"+timestamp,
                mch_billno: "12150242"+timestamp,
                send_name: '天虹百货',
                re_openid: 'oXf5MwIu8aE6rxrNF5kQ16Qn7nzQ',
                total_amount: 100,
                total_num: 1,
                wishing: '感谢您参加猜灯谜活动,祝您元宵节快乐!',
                client_ip: clientInfo.clientIP,
                act_name: '猜灯谜抢红包活动',
                remark: '猜越多得越多,快来抢!'
            };
            let res = await this.wxpay.v2.sendredpack(postDts);
            return res;
        },
        /**
         * 查询 现金红包v2
         * https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
         */
        async query_wx_sendredpack_v2(){
            let postDts = {
                appid: 'wx127dcd247234c521',
                mch_id: this.wxpay.mchid(),
                nonce_str: "随机字符串",
                mch_billno: "商户订单号",
                bill_type: 'MCHT'
            };
            let res = await this.wxpay.v2.gethbinfo(postDts);
            return res; 
        },
    }
  • 支付宝支付

    'use strict';
    const db = uniCloud.database();
    const dbCmd = db.command;
    const getNotifyUrl = function(cloudInfo,payid,notify){
        let notifyUrl = '';
        if(cloudInfo.provider=='tencent'){
            notifyUrl = `https://${cloudInfo.spaceId}.service.tcloudbase.com/ali-pay/${notify}/${payid}`;
        }else if(cloudInfo.provider=='aliyun'){
            notifyUrl = `https://fc-${cloudInfo.spaceId}.next.bspapp.com/ali-pay/${notify}/${payid}`;
        }
        return notifyUrl;
    };
    const getOrder = async function(){
        let money = Math.ceil(Math.random()*10);
        let create_order = await db.collection('mymp-pay-order').add({
            money: money,
            state: -1,
            create_date: Date.now()
        });
        return {
            id: create_order.id,
            money: money
        };
    };
    const MympPay = require('mymp-pay');
    const aliData = require("./data/my_alipay.js");
    /**
    const wxData = {
        pay_type: 'alipay',//固定值 alipay
        //seller_id支付宝账号ID//可在账号中心查看//https://open.alipay.com/develop/manage/account/manage
        seller_id: '2088011925529301',//用于支付结果通知效验收款账号
        app_id: '',
        private_key: '',
        app_cert: '',
        alipay_public_cert: '',
        alipay_root_cert: ''
    };
     */
    const returnError = function(){
        return {
            mpserverlessComposedResponse: true,
            statusCode: 200,
            headers: {
                'content-type': 'text/html'
            },
            body: 'fail'
        };
    };
    const returnSuccess = function(){
        return {
            mpserverlessComposedResponse: true,
            statusCode: 200,
            headers: {
                'content-type': 'text/html'
            },
            body: 'success'
        };
    };
    
    module.exports = {
        async _before(){
            this.cloudInfo = this.getCloudInfo();
            this.alipay = await MympPay(aliData);
        },
    
        /**
         * 支付结果通知
         * https://opendocs.alipay.com/open/204/105301
         */
        async ali_notify(){
            const httpInfo = this.getHttpInfo();
            uniCloud.logger.log(`httpInfo:`,httpInfo);
            //获取订单号
            let arrID = httpInfo.path.split('/');
            let orderID = arrID[arrID.length-1]||arrID[arrID.length-2];
            if(!orderID){
                return returnError();
            }
            //查询订单
            let orderDT = await db.collection('mymp-pay-order').where({
                _id: orderID
            }).limit(1).get();
            if(!orderDT.data.length){
                return returnError();
            }
            let orderDATA = orderDT.data[0];
            if(orderDATA.state===0){
                return returnError();
            }
    
            //验证签名 云函数可直接传入 event
            let res = await this.alipay.alipay.verifyNotify(httpInfo);
            if(!res){
                return returnError();
            }
    
            //获取数据
            let bodyObj = this.alipay.alipay.getBodyObj(httpInfo);
    
            if(bodyObj.out_trade_no!==orderDATA._id){
                return returnError();//订单号异常
            }
            if(bodyObj.total_amount!==(orderDATA.money/100).toString()){
                return returnError();//支付金额异常
            }
            //校验通知中的 seller_id(或者 seller_email ) 是否为 out_trade_no 这笔单据的对应的操作方(有的时候,一个商家可能有多个seller_id/seller_email)。
            if(bodyObj.seller_id!==this.alipay.seller_id()){
                //seller_email一般是收款支付宝账号
                //seller_id 是支付宝账号对应的user_id//不是应用appid
                //主要是效验收款放是否为自己
                return returnError();//收款账号异常
            }
            if(bodyObj.app_id!==this.alipay.app_id()){
                return returnError();
            }
    
            //bodyObj.trade_status==='WAIT_BUYER_PAY'//交易创建,等待买家付款。(不触发通知)
            //bodyObj.trade_status==='TRADE_CLOSED'//未付款交易超时关闭,或支付完成后全额退款。
            //bodyObj.trade_status==='TRADE_SUCCESS'//交易支付成功。
            //bodyObj.trade_status==='TRADE_FINISHED'//交易结束,不可退款。
            if(bodyObj.trade_status==='TRADE_SUCCESS'){
                //支付成功逻辑
                await db.collection('mymp-pay-order').doc(orderDATA._id).update({
                    state: 0,
                    notify: dbCmd.inc(1),
                    trade_status: bodyObj.trade_status
                });
            }else{
                //退款或其他通知 逻辑
                await db.collection('mymp-pay-order').doc(orderDATA._id).update({
                    notify: dbCmd.inc(1),
                    trade_status: bodyObj.trade_status
                });
            }
    
            return returnSuccess();
        },
    
        /**
         * 当面付(二维码收款)
         * https://opendocs.alipay.com/open/02ekfg?scene=19
         */
        async alipay_trade_precreate(){
            let order = await getOrder();
            let res = await this.alipay.alipay.openApi(
                {//公共请求参数部分,一般情况之需要 method 与 notify_url,其他数据系统内置
                    method: 'alipay.trade.precreate',
                    notify_url: getNotifyUrl(this.cloudInfo, order.id, 'ali_notify')
                },
                {//请求参数 部分,根据接口所需数据填写
                    out_trade_no: order.id,
                    total_amount: (order.money/100).toString(),
                    subject: 'Iphone608 32000G',
                    product_code: 'FACE_TO_FACE_PAYMENT',
                    body: '开发—body测试'
                }
            );
            return res;
        },
    
        /**
         * APP支付
         * https://opendocs.alipay.com/open/02e7gq?scene=20
         */
        async alipay_trade_app_pay(){
            let order = await getOrder();
            let res = await this.alipay.alipay.openApi(
                {//公共请求参数部分,一般情况之需要 method 与 notify_url,其他数据系统内置
                    method: 'alipay.trade.app.pay',
                    notify_url: getNotifyUrl(this.cloudInfo, order.id, 'ali_notify')
                },
                {//请求参数 部分,根据接口所需数据填写
                    out_trade_no: order.id,
                    total_amount: (order.money/100).toString(),
                    subject: 'Iphone608 32000G',
                    product_code: 'FACE_TO_FACE_PAYMENT',
                    body: '开发—body测试'
                },
                {//其它接口一般用不到,目前以下参数只用于APP支付接口
                    method: 'GET'
                }
            );
            return res;
        },
    
        /**
         * 统一下单接口(小程序支付)
         * https://opendocs.alipay.com/mini/03l5wn
         */
        async alipay_trade_create(){
            let order = await getOrder();
            let res = await this.alipay.alipay.openApi(
                {//公共请求参数部分,一般情况之需要 method 与 notify_url,其他数据系统内置
                    method: 'alipay.trade.create',
                    notify_url: getNotifyUrl(this.cloudInfo, order.id, 'ali_notify')
                },
                {//请求参数 部分,根据接口所需数据填写
                    out_trade_no: order.id,
                    total_amount: (order.money/100).toString(),
                    subject: 'Iphone608 32000G',
                    product_code: 'FACE_TO_FACE_PAYMENT',
                    body: '开发—body测试',
                    buyer_id: '2088922775769161'
                }
            );
            return res;
        },
    
        /**
         * 退款接口
         * https://opendocs.alipay.com/mini/03l736
         */
        async alipay_trade_refund(){
            let res = await this.alipay.alipay.openApi('alipay.trade.refund',//当参数只有method时可以为字符串
                {//请求参数 部分,根据接口所需数据填写
                    out_trade_no: '640af9bf819ce8bdcfcfc0b1',
                    refund_amount: '0.08',
                    refund_reason: '退款测试'
                }
            );
            return res;
        },
    
        /**
         * 转账到支付宝
         * https://opendocs.alipay.com/open/02byuo?scene=ca56bca529e64125a2786703c6192d41
         */
        async alipay_fund_trans_uni_transfer(){
            let res = await this.alipay.alipay.openApi('alipay.fund.trans.uni.transfer',{
                out_biz_no: 'date'+Date.now(),
                trans_amount: 1.0+parseFloat(Math.ceil(Math.random()*10)/10)+'',
                product_code: 'TRANS_ACCOUNT_NO_PWD',
                biz_scene: 'DIRECT_TRANSFER',
                order_title: '测试开发—201905代发',
                payee_info: {
                    identity_type: 'ALIPAY_LOGON_ID',//ALIPAY_USER_ID ALIPAY_LOGON_ID
                    identity: '16****@qq.com',
                    name: '王**' //当 identity_type=ALIPAY_LOGON_ID 时,本字段必填。
                },
                remark: '业务备注,测试开发',
                business_params: '{\"payer_show_name_use_alias\":\"true\"}'
            });
            return res;
        },
        /**
         * 查询支付宝余额
         * https://opendocs.alipay.com/open/02awe3
         */
        async alipayDataBillBalanceQuery(){
            return await this.alipay.alipay.openApi('alipay.data.bill.balance.query',{});
        },
    
        /**
         * 接口使用说明
         */
        async aliOpenApi(){
            //openApi支持大部分接口,使用方法类似
            //如支付宝小程序登录//https://opendocs.alipay.com/open/05nai1
            let aliOauth = await this.alipay.alipay.openApi({
                method: 'alipay.system.oauth.token',
                grant_type: 'authorization_code',
                code: ''
            });//注意 alipay.system.oauth.token 接口没有 biz_content 参数,所以没有第二个参数
    
            //小程序发送模板信息//https://opendocs.alipay.com/mini/03l21i
            let send = await this.alipay.alipay.openApi('alipay.open.app.mini.templatemessage.send',{
                to_user_id: '',
                user_template_id: '',
                page: 'pages/index/index?templatemessage=true',
                data: JSON.stringify({
                    "keyword1":{
                        "value" : "您订阅的签到提醒时间到啦",
                    },
                    "keyword2":{
                        "value" : "打卡小程序签到领金币~~",
                    }
                })
            });
            return '';
        }
    }
  • 抖音支付

    'use strict';
    const db = uniCloud.database();
    const dbCmd = db.command;
    const getNotifyUrl = function(cloudInfo,payid,notify){
        let notifyUrl = '';
        if(cloudInfo.provider=='tencent'){
            notifyUrl = `https://${cloudInfo.spaceId}.service.tcloudbase.com/ali-pay/${notify}/${payid}`;
        }else if(cloudInfo.provider=='aliyun'){
            notifyUrl = `https://fc-${cloudInfo.spaceId}.next.bspapp.com/ali-pay/${notify}/${payid}`;
        }
        return notifyUrl;
    };
    const getOrder = async function(){
        let money = Math.ceil(Math.random()*10);
        let create_order = await db.collection('mymp-pay-order').add({
            money: money,
            state: -1,
            create_date: Date.now()
        });
        return {
            id: create_order.id,
            money: money
        };
    };
    const MympPay = require('mymp-pay');
    const returnError = function(){
        return {
            mpserverlessComposedResponse: true,
            statusCode: 400,
            headers: {
                'content-type': 'application/json'
            },
            body: JSON.stringify({
                "code": "FAIL",
                "message": "失败"
            })
        };
    };
    const returnSuccess = function(){
        return {
            "err_no": 0,
            "err_tips": "success"
        };
    };
    
    module.exports = {
        async _before(){
            this.cloudInfo = this.getCloudInfo();
            this.ttpay = await MympPay({
                pay_type: 'toutiao',//固定值
                salt: '***',
                token: '***',
            });
        },
    
        /**
         * 支付结果通知
         * https://opendocs.alipay.com/open/204/105301
         */
        async tt_notify(){
            const httpInfo = this.getHttpInfo();
            uniCloud.logger.log(`httpInfo:`,httpInfo);
            //获取订单号
            let arrID = httpInfo.path.split('/');
            let orderID = arrID[arrID.length-1]||arrID[arrID.length-2];
            if(!orderID){
                return returnError();
            }
            //查询订单
            let orderDT = await db.collection('mymp-pay-order').where({
                _id: orderID
            }).limit(1).get();
            if(!orderDT.data.length){
                return returnError();
            }
            let orderDATA = orderDT.data[0];
            if(orderDATA.state===0){
                return returnError();
            }
    
            //验证签名 云函数可直接传入 event
            let res = await this.ttpay.verifyNotify(httpInfo);
            if(!res){
                return returnError();
            }
    
            //处理业务
    
            return returnSuccess();
        },
    
        /**
         * 预下单接口
         * [](https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/pay-list/pay)
         */
        async tt_createOrder(){
            var payBody = {
                app_id: '',
                out_order_no: '',
                total_amount: 100,
                subject: '',
                body: '',
                valid_time: 1800,
                notify_url: getNotifyUrl(this.cloudInfo, 'order_id', 'tt_notify'),
                sign: '',//留空即可
            };
            let res = await this.ttpay.createOrder(payBody);
            return res;
        },
    
        /**
         * 退款
         * [](https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/refund-list/refund)
         */
        async tt_createRefund(){
            let res = await this.ttpay.createRefund({
                app_id: '',
                out_order_no: '',
                out_refund_no: '',
                reason: `退款`,
                refund_amount: ''
            });
            return res;
        },
    
        /**
         * 支付结果查询
         * [](https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/pay-list/query)
         */
        async tt_queryOrder(){
            let res = await this.ttpay.queryOrder({
                app_id: '',
                out_order_no: '',
            });
            return res;
        },
    
        /**
         * 更多接口使用说明
         */
        async ttOpenApi(){
            //openApi支持大部分接口,使用方法类似,第一个参数,请求参数(sign程序会自动加上),第二个 请求链接,
    
            //比如,查询余额:[](https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/withdraw/query-balance)
            let url = 'https://developer.toutiao.com/api/apps/ecpay/saas/query_merchant_balance';
            let res = await this.ttpay.openApi({
                channel_type: 'wx',
            },url);
            return res;
        }
    }

联系微信

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问