• APIcloud制作APP 微信支付与支付宝支付


     首先要在云端绑定相应模块如alipay和wxpay其次编写代码。


     配置区域

    var cfg = {

    webName:'',//APP名字
    payDebug:true,
    isUseWxPay:true,
    isUseAliPay:true,
    wxApiKey : 'wx708c359136e7af15',//支付apikey
    wxMchId : '1488568362',//APPID
    wxPartnerKey : '706709a80d19f5587804a834bbe0158a',//PartnerKey
    wxNotifyUrl : 'http://whtouzi.zhonghuapaoyu.cn/index.php/api/user/userRecharge',//返回后台地址
    aliPayPid : '2088721942591903',//appid
    aliPayPriKey : '秘钥',
    aliPayPubKey : '公钥',

    aliPayNotifyURL : 'http://whtouzi.zhonghuapaoyu.cn/index.php/api/user/userRecharge',//返回后台地址


    //秒,验证码发送间隔
    checkCodeInterval:120
    };

    其次定义支付函数

    //微信支付
    function wxPay(func, params,debug) {

    var wx = api.require('wx');
    wx.isInstalled(function(ret, err){
    params.money=n;
    params.title='充值';
    params.content='充值'
    params.no=noo2;

    if(ret.installed){
    toast('正在进行微信支付,请稍候...');

    if (isNull(params.title)) {
    params.title = "充值";
    }


    if (isNull(params.content)) {
    params.content = "充值";
    }

    if (!isNull(params.money)) {
    var m = n;

    params.money = m < 0.01 ? 0.01 : m;
    } else {
    params.money = 0.01;
    }

    if (isNull(params.no)) {
    params.no = String(new Date().getTime()) + String(parseInt(Math.random() * 1000));
    }

    var tn = String(params.no) + String((new Date()).valueOf());
    tn = tn.substr(0,32);
    // alert('ccccccccccc');

    var wxPay = api.require('wxPay');
    wxPay.config({
    apiKey : cfg.wxApiKey,
    mchId : cfg.wxMchId,
    partnerKey : cfg.wxPartnerKey,
    notifyUrl : cfg.wxNotifyUrl
    }, function(ret, err) {
    if (ret.status) {
    // alert('11111111');
    // alert(ret.status);
    wxPay.pay({
    description : String(params.title),
    totalFee : String(parseInt(params.money * 100)),
    tradeNo : tn,
    }, function(ret, err) {

    var result = JSON.stringify(err);
    var results = JSON.stringify(ret);
    // alert(result+'***'+results);

    //alert(err.code);
    if (ret.status) {

    toast('支付成功');
    func('success');
    } else {

    if (err.code == -2) {
    toast('用户取消支付');
    func('cancel');
    }
    if (err.msg == 'NOTENOUGH') {
    toast('余额不足');
    func('fail');
    }
    if (err.msg == 'ORDERPAID') {
    toast('商户订单已支付');
    func('fail');
    }
    if (err.msg == 'ORDERCLOSED') {
    toast('订单已关闭');
    func('fail');
    }
    }
    });
    } else {
    toast('商户配置错误,错误码:' + err.code);
    }
    });
    }else{
    toast('您当前设备不支持微信支付,请安装微信客户端后使用。');
    }
    });
    }

    //支付宝支付
    function aliPay(func, params,debug) {
    /*if(typeof debug != 'undefined' && debug == true){
    tanConfirm(['调试支付成功','调试取消支付','取消'],'支付宝支付调试 Title:'+params.title+' Money:'+params.money,function(i){
    if(i==0){
    func('success');
    }
    if(i==1){
    func('cancel');
    }
    });
    return false;
    }*/
    params.money=n;
    params.title = "充值";
    params.content = "充值";
    if (isNull(params.title)) {
    params.title = "充值";
    }
    if (isNull(params.content)) {
    params.content = "充值";
    }
    if (!isNull(params.money)) {
    var m = parseFloat(params.money);
    params.money = m < 0.01 ? 0.01 : m;
    } else {
    params.money = 0.01;
    }
    params.no=noo;


    if (isNull(params.no)) {
    params.no = '201708021365';
    }
    var aliPay = api.require('aliPay');
    aliPay.config({
    partner : cfg.aliPayPid,
    seller : cfg.aliPayPid,
    rsaPriKey : cfg.aliPayPriKey,
    rsaPubKey : cfg.aliPayPubKey,
    notifyURL : cfg.aliPayNotifyURL
    }, function(ret, err) {
    if (ret.status) {
    aliPay.pay({
    subject : params.title,
    body : params.content,
    amount : params.money,
    tradeNO : params.no
    }, function(ret, err) {
    var code = ret.statusCode;
    if (code == 9000) {

    toast('支付成功');
    func('success');
    } else if (code == 6001) {

    toast('用户取消支付');
    func('cancel');
    } else {

    toast('支付出错,错误代码:'+code);
    //func('fail');
    }
    });
    }
    });
    }

    最后在body调用JS和方法即可

    调用微信

    oo=ret.order_id;
    var p = {


    };
    noo2=oo+'n';
    //


    wxPay(function(code){

    if(code == 'success'){
    dpost({
    act:'pay',
    sn:noo2,
    paymentType:'wxpay'
    },function(d){

    if(d == 'success'){

    tan('充值成功!');
    location.reload(true)
    }
    if(d == 'fail'){

    tan('充值失败');
    }
    });
    }
    if(code == 'fail'){

    tan('充值失败');
    }
    },p,cfg.payDebug);

    }

    });

    调用支付宝

    ooo=ret.order_id;
    var p = {


    };
    noo=ooo;
    //
    aliPay(function(code){

    if(code == 'success'){
    dpost({
    act:'pay',
    sn:noo,
    paymentType:'alipay'
    },function(d){
    if(d == 'success'){
    tan('充值成功!');
    location.reload(true)
    }
    if(d == 'fail'){

    tan('充值失败!');
    }
    });
    }
    if(code == 'fail'){

    tan('充值失败!');
    }
    },p,cfg.payDebug);

    });

    订单号可以从后台获取,再在前台获取充值金额即可。

  • 相关阅读:
    WINDOWSXP文件夹右键属性没有“安全”选项卡的解决
    无法为类型 CuteEditor.Editor 授予有效的许可证。
    DSO Framer _ WebForm 使用
    sql语句中日期时间格式化查询
    url 编码 中文|c# js url传参中文乱码解决方案
    UML中类图实例(转载)
    JavaSE重点——注解和反射
    JavaSE重点——内部类(转载)
    JavaSE重点——Java8新特性
    JavaSE重点——网络编程
  • 原文地址:https://www.cnblogs.com/jyc226/p/7504011.html
Copyright © 2020-2023  润新知