beecloud.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var beecloud = {};
  2. var channels = null;
  3. var w = null;
  4. beecloud.payReq = function(data, cbsuccess, cberror) {
  5. doPay(data, cbsuccess, cberror);
  6. };
  7. beecloud.genBillNo = function() {
  8. var d = new Date();
  9. var vYear = d.getFullYear();
  10. var vMon = d.getMonth() + 1;
  11. var vDay = d.getDate();
  12. var h = d.getHours();
  13. var m = d.getMinutes();
  14. var se = d.getSeconds();
  15. var ms = d.getMilliseconds();
  16. billno = "" + vYear + (vMon < 10 ? "0" + vMon : vMon) + (vDay < 10 ? "0" + vDay : vDay) + (h < 10 ? "0" + h : h) + (m < 10 ? "0" + m : m) + (se < 10 ? "0" + se : se) + ms;
  17. return billno;
  18. };
  19. mui.plusReady(function() {
  20. //配置业务支持的支付通道,支付需要服务端支持,在BeeCloud上支持支付宝支付和微信支付;
  21. var support_channel = ['alipay'];
  22. if(!mui.os.stream){//流应用下暂不支持微信SDK支付
  23. support_channel.push('wxpay');
  24. }
  25. plus.payment.getChannels(function(s) {
  26. var oauthArea = document.querySelector('.oauth-area');
  27. for (var i = 0; i < s.length; i++) {
  28. if(s[i].serviceReady){
  29. if(~support_channel.indexOf(s[i].id)){
  30. var btn = document.createElement('div');
  31. btn.setAttribute('id', s[i].id);
  32. btn.className = 'mui-btn mui-btn-blue mui-btn-block pay';
  33. btn.innerText = s[i].description+'支付'
  34. oauthArea.appendChild(btn);
  35. }
  36. }
  37. }
  38. channels = s;
  39. }, function(e) {
  40. console.log("获取支付渠道信权限失败:" + e.message);
  41. });
  42. });
  43. function getRandomHost() {
  44. var hosts = ['https://apibj.beecloud.cn',
  45. 'https://apihz.beecloud.cn',
  46. 'https://apisz.beecloud.cn',
  47. 'https://apiqd.beecloud.cn'
  48. ];
  49. return "" + hosts[parseInt(3 * Math.random())] + "/2/rest/app/bill";
  50. }
  51. /**
  52. * 获取支付通道
  53. *
  54. */
  55. function getPayChannel(bc_channel) {
  56. var dc_channel_id = '';
  57. switch (bc_channel) {
  58. case 'ALI_APP':
  59. dc_channel_id = 'alipay';
  60. break;
  61. case 'WX_APP':
  62. dc_channel_id = 'wxpay';
  63. break;
  64. default:
  65. break;
  66. }
  67. for (var i in channels) {
  68. if (channels[i].id == dc_channel_id) {
  69. return channels[i];
  70. }
  71. }
  72. return null;
  73. }
  74. function doPay(payData, cbsuccess, cberror) {
  75. if (w) return;
  76. w = plus.nativeUI.showWaiting();
  77. mui.ajax(getRandomHost(), {
  78. data: JSON.stringify(payData),
  79. type: 'post',
  80. dataType: 'json',
  81. contentType: "application/json",
  82. success: function(data) {
  83. w.close();
  84. w = null;
  85. var paySrc = '';
  86. if (data.result_code == 0) {
  87. var payChannel = getPayChannel(payData.channel);
  88. if (payChannel) {
  89. if (payChannel.id === 'alipay') {
  90. paySrc = data.order_string;
  91. } else if (payChannel.id === 'wxpay') {
  92. var statement = {};
  93. statement.appid = data.app_id;
  94. statement.noncestr = data.nonce_str;
  95. statement.package = data.package;
  96. statement.partnerid = data.partner_id;
  97. statement.prepayid = data.prepay_id;
  98. statement.timestamp = parseInt(data.timestamp);
  99. statement.sign = data.pay_sign;
  100. paySrc = JSON.stringify(statement);
  101. }
  102. plus.payment.request(payChannel, paySrc, cbsuccess, cberror);
  103. } else if (payData.channel == 'UN_WEB') {
  104. //银联在线支付
  105. var web = plus.webview.create('', "beecloudPay",{
  106. statusbar:{
  107. background: "#f7f7f7"
  108. }
  109. });
  110. //注入JS,解决银联界面返回的问题
  111. web.setJsFile('_www/js/95516.js');
  112. web.addEventListener('loaded', function() {
  113. if (!web.isVisible()) {
  114. web.show();
  115. }
  116. });
  117. web.loadData(data.html);
  118. }
  119. } else {
  120. var bcError = {};
  121. bcError.code = data.result_code;
  122. bcError.message = data.result_msg + ":" + data.err_detail;
  123. cberror(bcError);
  124. }
  125. },
  126. error: function(xhr, errorType, error) {
  127. w.close();
  128. w = null;
  129. cberror(error);
  130. }
  131. });
  132. }