pingpp-pc.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (function(){
  2. var
  3. version = "2.0.8",
  4. hasOwn = {}.hasOwnProperty,
  5. PingppSDK = function(){},
  6. cfg = {
  7. PINGPP_NOTIFY_URL: 'https://api.pingxx.com/notify/charges/',
  8. PINGPP_MOCK_URL: 'http://sissi.pingxx.com/mock.php',
  9. ALIPAY_PC_DIRECT_URL: 'https://mapi.alipay.com/gateway.do',
  10. UPACP_PC_URL: 'https://gateway.95516.com/gateway/api/frontTransReq.do',
  11. CP_B2B_URL: 'https://payment.chinapay.com/CTITS/service/rest/page/nref/000000000017/0/0/0/0/0'
  12. },
  13. channels = {
  14. alipay_pc_direct: 'alipay_pc_direct',
  15. upacp_pc: 'upacp_pc',
  16. cp_b2b: 'cp_b2b'
  17. };
  18. PingppSDK.prototype = {
  19. version: version,
  20. _resultCallback: undefined,
  21. _debug: false,
  22. createPayment: function(charge_json, callback, debug) {
  23. if (typeof callback == "function") {
  24. this._resultCallback = callback;
  25. }
  26. if (typeof debug == "boolean") {
  27. this._debug = debug;
  28. }
  29. var charge;
  30. if(typeof charge_json == "string"){
  31. try{
  32. charge = JSON.parse(charge_json);
  33. }catch(err){
  34. this._innerCallback("fail", this._error("json_decode_fail"));
  35. return;
  36. }
  37. }else{
  38. charge = charge_json;
  39. }
  40. if(typeof charge == "undefined"){
  41. this._innerCallback("fail", this._error("json_decode_fail"));
  42. return;
  43. }
  44. if(!hasOwn.call(charge, 'id')){
  45. this._innerCallback("fail", this._error("invalid_charge", "no_charge_id"));
  46. return;
  47. }
  48. if(!hasOwn.call(charge, 'channel')){
  49. this._innerCallback("fail", this._error("invalid_charge", "no_channel"));
  50. return;
  51. }
  52. var channel = charge['channel'];
  53. if(!hasOwn.call(charge, 'credential')){
  54. this._innerCallback("fail", this._error("invalid_charge", "no_credential"));
  55. return;
  56. }
  57. if (!charge['credential']) {
  58. this._innerCallback("fail", this._error("invalid_credential", "credential_is_undefined"));
  59. return;
  60. }
  61. if (!hasOwn.call(channels, channel)) {
  62. this._innerCallback("fail", this._error("invalid_charge", "no_such_channel:" + channel));
  63. return;
  64. }
  65. if (!hasOwn.call(charge['credential'], channel)) {
  66. this._innerCallback("fail", this._error("invalid_credential", "no_valid_channel_credential"));
  67. return;
  68. }
  69. if(!hasOwn.call(charge, 'livemode')){
  70. this._innerCallback("fail", this._error("invalid_charge", "no_livemode"));
  71. return;
  72. }
  73. if (charge['livemode'] == false) {
  74. this._testModeNotify(charge);
  75. return;
  76. }
  77. var credential = charge['credential'][channel];
  78. if (channel == channels.upacp_pc) {
  79. form_submit(cfg.UPACP_PC_URL, 'post', credential);
  80. } else if (channel == channels.alipay_pc_direct) {
  81. if (!hasOwn.call(credential, "_input_charset")) {
  82. credential["_input_charset"] = 'utf-8';
  83. }
  84. var query = stringify_data(credential, channel, true);
  85. window.location.href = cfg.ALIPAY_PC_DIRECT_URL + "?" + query;
  86. } else if (channel == channels.cp_b2b) {
  87. form_submit(cfg.CP_B2B_URL, 'post', credential);
  88. }
  89. },
  90. _error: function(msg, extra) {
  91. msg = (typeof msg == "undefined") ? "" : msg;
  92. extra = (typeof extra == "undefined") ? "" : extra;
  93. return {
  94. msg:msg,
  95. extra:extra
  96. };
  97. },
  98. _innerCallback: function(result, err) {
  99. if (typeof this._resultCallback == "function") {
  100. if (typeof err == "undefined") {
  101. err = this._error();
  102. }
  103. this._resultCallback(result, err);
  104. }
  105. },
  106. _testModeNotify: function(charge) {
  107. var params = {
  108. 'ch_id': charge['id'],
  109. 'scheme': 'http',
  110. 'channel': charge['channel']
  111. };
  112. if (hasOwn.call(charge, 'order_no')) {
  113. params['order_no'] = charge['order_no'];
  114. } else if (hasOwn.call(charge, 'orderNo')) {
  115. params['order_no'] = charge['orderNo'];
  116. }
  117. if (hasOwn.call(charge, 'time_expire')) {
  118. params['time_expire'] = charge['time_expire'];
  119. } else if (hasOwn.call(charge, 'timeExpire')) {
  120. params['time_expire'] = charge['timeExpire'];
  121. }
  122. if (hasOwn.call(charge, 'extra')) {
  123. params['extra'] = encodeURIComponent(JSON.stringify(charge['extra']));
  124. }
  125. location.href = cfg.PINGPP_MOCK_URL+'?'+stringify_data(params);
  126. }
  127. };
  128. function form_submit(url, method, params) {
  129. var form = document.createElement("form");
  130. form.setAttribute("method", method);
  131. form.setAttribute("action", url);
  132. for (var key in params) {
  133. if (hasOwn.call(params, key)) {
  134. var hiddenField = document.createElement("input");
  135. hiddenField.setAttribute("type", "hidden");
  136. hiddenField.setAttribute("name", key);
  137. hiddenField.setAttribute("value", params[key]);
  138. form.appendChild(hiddenField);
  139. }
  140. }
  141. document.body.appendChild(form);
  142. form.submit();
  143. }
  144. function stringify_data(data, channel, urlencode) {
  145. if (typeof urlencode == "undefined") {
  146. urlencode = false;
  147. }
  148. var output = [];
  149. for (var i in data) {
  150. if (channel == "bfb_wap" && i == "url") {
  151. continue;
  152. }
  153. if (channel == "yeepay_wap" && i == "mode") {
  154. continue;
  155. }
  156. output.push(i + '=' + (urlencode ? encodeURIComponent(data[i]) : data[i]));
  157. }
  158. return output.join('&');
  159. }
  160. PingppSDK.prototype.payment = PingppSDK.prototype.createPayment;
  161. window.pingppPc = new PingppSDK();
  162. })();