白罂粟
白罂粟
  • 发布:2018-01-12 15:34
  • 更新:2018-01-12 15:34
  • 阅读:1928

支付方法的封装

分类:Native.js
**最下面有源码**  
这个是我封装的payment方法 封装的有点深  
入过的坑   
1.微信一直返回-1代码错误 可能是因为微信的配置出来问题还有可能需要用正式签名打包  
2.集成支付宝失败 提示是系统繁忙,请稍后再试( ALI64) 这个我记得好像是也是支付的配置出了问题 具体的可能忘了 可以联系支付宝客服解决  
3. 注意layerOpen这个是我找的模仿ios的一个提示窗口 需要的找我要  

在其他页面调用之前需要导入  
<script type="text/javascript" src="utilsJs/paymentUtils.js"></script>  
var paymentUtils = null;  
function plusReady() {  
    paymentUtils = new PaymentUtils();  
    paymentUtils.paymentWayAlert({  
                order_id: order._id,  
                onSuccess: function () {  
                  //支付成功  
                },  
                onFailed: function () {  
                  //支付失败  
                }  
              });  
  }  
if (window.plus) {  
    plusReady();  
  } else {  
    document.addEventListener("plusready", plusReady, false);  
  }  

这个是 PaymentUtils文件的代码  
var PaymentUtils = function () {  
  this.reportLocation = null;  
};  

/**  
 *检查是否安装了支付软件  
 * @param options  
 */  
PaymentUtils.prototype.checkPaymentWay = function (options) {  
  options = options || {};  
  plus.payment.getChannels(function (paymentWay) {  
    if (paymentWay) {  
      options.onSuccess && options.onSuccess(paymentWay);  
    } else {  
      options.onFailed && options.onFailed('获取支付通道列表失败');  
    }  
  }, function (error) {  
    options.onFailed && options.onFailed('获取支付通道列表失败' + error.message);  
  });  
};  

/**  
 *调用支付并选择支付方式  
 * @param options  
 */  
PaymentUtils.prototype.paymentWayAlert = function (options) {  
  var _this = this;  
  options = options || {};  
  var installPayment = function (content, channel) {  
    layerOpen({  
      "title": "是否安装支付支持服务",  
      "content": content,  
      "btn": ["取消", "确定"],  
      "shadeClose": true,  
      "event": [null, function () {  
        channel.installService();  
      }]  
    });  
  };  
  this.checkPaymentWay({  
    onSuccess: function (paymentWay) {  
      var bts = [];  
      var bids = [];  
      var content = '';  
      for (var index in paymentWay) {  
        var channel = paymentWay[index];  
        bids.push(channel);  
        switch (channel.id) {  
          case 'alipay':  
            bts.push({title: '支付宝支付'});  
            break;  
          case 'wxpay':  
            bts.push({title: '微信支付'});  
            break;  
          case 'appleiap':  
            break;  
          case 'qhpay':  
            break;  
          default:  
            break;  
        }  
      }  
      plus.nativeUI.actionSheet({  
          cancel: "取消",  
          buttons: bts  
        },  
        function (e) {  
          var i = e.index;  
          if (i > 0) {  
            var channel = bids[i - 1];  
            switch (channel.id) {  
              case 'alipay':  
                if (!channel.serviceReady) {  
                  content = '检测到系统未安装“支付宝快捷支付服务”,无法完成支付操作,是否立即安装?';  
                  installPayment(content, channel);  
                  return;  
                }  
                break;  
              case 'wxpay':  
                if (!channel.serviceReady) {  
                  content = '系统未安装“' + channel.description + '”服务,无法完成支付,是否立即安装?';  
                  installPayment(content, channel);  
                  return;  
                }  
                break;  
              case 'appleiap':  
                break;  
              case 'qhpay':  
                break;  
              default:  
                break;  
            }  
            _this.paymentHandle({  
              order_id: options.order_id,  
              channel: channel,  
              onSuccess: function (result) {  
                options.onSuccess && options.onSuccess(result);  
              },  
              onFailed: function (error) {  
                options.onFailed && options.onFailed(error);  
              },  
              onCompleted: function () {  
                options.onCompleted && options.onCompleted();  
              }  
            });  
          }  
        }  
      );  
    },  
    onFailed: function (error) {  
      options.onFailed && options.onFailed(error);  
    }  
  });  
};  

/**  
 *向服务器发起请求  
 * @param options  
 */  
PaymentUtils.prototype.paramsHandle = function (options) {  
  var params = '';  
  options = options || {};  
  var order_id = options.order_id;  
  var isAilPay = options.isAilPay;  
  //判断是否是支付宝还是微信  
  if (isAilPay) {  
    //请求服务器获取支付接口  
    http.xxxx({  
      order_id: order_id,  
      onSuccess: function (responseJson) {  
        options.onSuccess && options.onSuccess(params);  
      },  
      onFailed: function (errorJson) {  
        options.onFailed && options.onFailed(errorJson);  
      },  
      onCompleted: function () {  
        options.onCompleted && options.onCompleted();  
      }  
    });  
  } else {  
   //请求服务器获取支付接口  
    http.xxxx({  
      order_id: order_id,  
      onSuccess: function (responseJson) {  
        options.onSuccess && options.onSuccess(params);  
      },  
      onFailed: function (errorJson) {  
        options.onFailed && options.onFailed(errorJson);  
      },  
      onCompleted: function () {  
        options.onCompleted && options.onCompleted();  
      }  
    });  
  }  
};  

/**  
 *调起payment方法  
 * @param options  
 */  
PaymentUtils.prototype.paymentHandle = function (options) {  
  var _this = this;  
  var isAilPay;  
  options = options || {};  
  var order_id = options.order_id;  
  var channel = options.channel;  
  switch (channel.id) {  
    case 'alipay':  
      isAilPay = true;  
      break;  
    case 'wxpay':  
      isAilPay = false;  
      break;  
    default:  
      break;  
  }  
  if (isAilPay == undefined) {  
    return;  
  }  
  this.paramsHandle({  
    order_id: order_id,  
    isAilPay: isAilPay,  
    onSuccess: function (order_params) {  
    //官方调起支付的api  
      plus.payment.request(channel, order_params, function (result) {  
        //支付成功后 逻辑  
      }, function (error) {  
        ApiConfig.staticShowToast('支付失败:' + error.code);  
        options.onFailed && options.onFailed(error);  
      });  
    },  
    onFailed: function (error) {  
      options.onFailed && options.onFailed(error);  
    },  
    onCompleted: function () {  
      options.onCompleted && options.onCompleted();  
    }  
  });  
};  

/**  
 * 查询支付成功后的订单状态  
 * @param options  
 */  
PaymentUtils.prototype.selectPaymentStatus = function (options) {  

}  

/**  
 * 获取订单详情  
 * @param options  
 */  
PaymentUtils.prototype.getOrderSuccess = function (options) {  

}
2 关注 分享
gmmx133 rysnone

要回复文章请先登录注册