sg.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* ================================================================================
  2. * sg.js v1.0
  3. * http://git.shemic.com/sg/script
  4. * ================================================================================
  5. * Copyright 2018-2020 Dever(dever.cc)
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ================================================================================
  19. */
  20. var host = 'http://sg.5dev.cn/';
  21. document.write('<link rel="stylesheet" href="'+host+'assets/layui/css/layui.css">');
  22. document.write('<script src="'+host+'assets/layui/layui.js"></script>');
  23. var SG =
  24. {
  25. id : '#pay',
  26. layui : '',
  27. product_name : '#product_name',
  28. amount : '#amount',
  29. prefix : 'Pay with ',
  30. submit : 'pay',
  31. url : host + 'payment/?l=api.get&json=1&callback=?',
  32. method :
  33. {
  34. 'credit' :
  35. {
  36. 'id' : 21
  37. ,'name' : 'credit cards'
  38. ,'type' : 'page2'
  39. }
  40. ,'sadad' :
  41. {
  42. 'id' : 4
  43. ,'name' : 'sadad'
  44. ,'type' : 'redirection'
  45. }
  46. ,'naps' :
  47. {
  48. 'id' : 3
  49. ,'name' : 'naps'
  50. ,'type' : 'redirection'
  51. }
  52. /*
  53. ,'installments_page' :
  54. {
  55. 'id' : 12
  56. ,'name' : 'installments page'
  57. ,'type' : 'page'
  58. }
  59. */
  60. ,'installments' :
  61. {
  62. 'id' : 2
  63. ,'name' : 'installments'
  64. ,'type' : 'redirection'
  65. }
  66. },
  67. Html : function()
  68. {
  69. var self = this;
  70. var html = '<select id="sg-method" name="sg-method" lay-verify="required">';
  71. for (var i in self.method) {
  72. html += '<option value="'+i+'">'+self.prefix+self.method[i]['name']+'</option>';
  73. }
  74. html += '</select>';
  75. html = self.CreateFormItem(html, 'Pay Method');
  76. html += self.CreateFormItem('<a class="layui-btn" id="sg-submit">'+self.submit+'</a>');
  77. if (self.layui.$(self.id).length) {
  78. self.layui.$(self.id).html(html);
  79. self.Init('#sg-submit', '#sg-method');
  80. self.layui.form.render('select');
  81. }
  82. }
  83. ,Notify : function(res)
  84. {
  85. alert(222);
  86. }
  87. ,Init : function(submit, value)
  88. {
  89. var self = this;
  90. self.layui.$(submit).bind('click', function() {
  91. var key = self.layui.$(value).val();
  92. var method = self.method[key];
  93. if (method.type == 'page2') {
  94. self.ShowPage(key, method);
  95. } else {
  96. self.Submit(key, method);
  97. }
  98. });
  99. }
  100. ,ShowPage : function(key, method)
  101. {
  102. var self = this;
  103. var html = self.CreatePage();
  104. self.layui.layer.alert(html, {
  105. area: ['auto', '600px'],
  106. title : 'notice'
  107. //,skin: 'layui-layer-lan' //样式类名
  108. ,closeBtn: 0
  109. ,btn: [self.submit, 'close']
  110. ,success: function(layero, index){
  111. self.layui.form.render('select');
  112. }
  113. }, function() {
  114. var state = self.CheckPageParam(key, method).validateCcForm();
  115. if (state) {
  116. self.Submit(key, method);
  117. }
  118. });
  119. }
  120. ,CreatePage : function()
  121. {
  122. var self = this;
  123. var html = '<form class="layui-form" action="">';
  124. html += self.CreateFormItemInput('card_holder_name', 'Card Holder\'s Name', 'Card Name');
  125. html += self.CreateFormItemInput('card_number', 'Debit/Credit Card Number', 'Card Number');
  126. var option =
  127. {
  128. 'Jan (01)' : '01',
  129. 'Feb (02)' : '02',
  130. 'Mar (03)' : '03',
  131. 'Apr (04)' : '04',
  132. 'May (05)' : '05',
  133. 'June (06)' : '06',
  134. 'July (07)' : '07',
  135. 'Aug (08)' : '08',
  136. 'Sep (09)' : '09',
  137. 'Oct (10)' : '10',
  138. 'Nov (11)' : '11',
  139. 'Dec (12)' : '12',
  140. };
  141. html += self.CreateFormItemSelect('expiry_month', 'Expiration Month', 'Expiration Month', option);
  142. var date = new Date;
  143. var year = date.getFullYear();
  144. var total = 10;
  145. var option = {};
  146. for (var i = 0; i < total; i++) {
  147. option[year + i] = year + i - 2000;
  148. }
  149. html += self.CreateFormItemSelect('expiry_year', 'Expiration Year', 'Expiration Year', option);
  150. html += self.CreateFormItemInput('cvv', 'Security Code', 'Card CVV');
  151. html += '</form>';
  152. return html;
  153. }
  154. ,Submit : function(key, method)
  155. {
  156. if (location.href.indexOf('?status=') != -1) {
  157. var refer = location.href.split('?status=');
  158. var refer = refer[0];
  159. } else if (location.href.indexOf('&status=') != -1) {
  160. var refer = location.href.split('&status=');
  161. var refer = refer[0];
  162. } else {
  163. var refer = location.href;
  164. }
  165. var self = this;
  166. var param = {};
  167. param.type = method.id;
  168. param.refer = refer;
  169. param.product_name = self.layui.$(self.product_name).val();
  170. param.amount = parseInt(self.layui.$(self.amount).val());
  171. self.layui.$.ajax({
  172. url: self.url,
  173. type: 'post',
  174. dataType: 'jsonp',
  175. data: param,
  176. success: function (response) {
  177. if (response.status == 1 && response.data && response.data.form) {
  178. self.layui.$(self.id).append(response.data.form);
  179. if (method.type == 'page2') {
  180. var expDate = self.layui.$('#expiry_year').val()+''+self.layui.$('#expiry_month').val();
  181. var params = {};
  182. params.card_holder_name = self.layui.$('#card_holder_name').val();
  183. params.card_number = self.layui.$('#card_number').val();
  184. params.expiry_date = expDate;
  185. params.card_security_code = self.layui.$('#cvv').val();
  186. self.layui.$.each(params, function(k, v){
  187. self.layui.$('<input>').attr({
  188. type: 'hidden',
  189. id: k,
  190. name: k,
  191. value: v
  192. }).appendTo('#sg-form');
  193. });
  194. self.Click();
  195. } else if (method.type == 'page') {
  196. } else {
  197. self.Click();
  198. }
  199. /*
  200. if(response.paymentMethod == 'cc_merchantpage' || response.paymentMethod == 'installments_merchantpage') {
  201. showMerchantPage(response.url);
  202. }
  203. */
  204. }
  205. }
  206. });
  207. }
  208. ,Click : function()
  209. {
  210. var self = this;
  211. self.layui.$('#sg-form input[type=submit]').click();
  212. }
  213. ,CheckPageParam : function(key, method)
  214. {
  215. var self = this;
  216. var payfortFort = self.CheckPayFort();
  217. return {
  218. validateCcForm: function () {
  219. this.hideError();
  220. var isValid = payfortFort.validateCardHolderName(self.layui.$('#card_holder_name'));
  221. if(!isValid) {
  222. this.showError('Invalid Card Holder Name', '#card_holder_name');
  223. return false;
  224. }
  225. isValid = payfortFort.validateCreditCard(self.layui.$('#card_number'));
  226. if(!isValid) {
  227. this.showError('Invalid Credit Card Number', '#card_number');
  228. return false;
  229. }
  230. isValid = payfortFort.validateCvc(self.layui.$('#cvv'));
  231. if(!isValid) {
  232. this.showError('Invalid Card CVV', '#cvv');
  233. return false;
  234. }
  235. return true;
  236. },
  237. showError: function(msg, id) {
  238. self.layui.layer.tips(msg, id, {
  239. tips: [2, '#3595CC'],
  240. time: 3000
  241. });
  242. },
  243. hideError: function() {
  244. return;
  245. }
  246. };
  247. }
  248. ,CheckPayFort : function()
  249. {
  250. var self = this;
  251. return {
  252. validateCreditCard: function(element) {
  253. var isValid = false;
  254. var eleVal = self.layui.$(element).val();
  255. eleVal = this.trimString(element.val());
  256. eleVal = eleVal.replace(/\s+/g, '');
  257. self.layui.$(element).val(eleVal);
  258. isValid = self.ValidateCreditCard(eleVal);
  259. return isValid;
  260. },
  261. validateCardHolderName: function(element) {
  262. self.layui.$(element).val(this.trimString(element.val()));
  263. var cardHolderName = self.layui.$(element).val();
  264. if(!cardHolderName || cardHolderName.length > 50) {
  265. return false;
  266. }
  267. return true;
  268. },
  269. validateCvc: function(element) {
  270. self.layui.$(element).val(this.trimString(element.val()));
  271. var cvc = self.layui.$(element).val();
  272. if(!cvc || cvc.length > 4 || cvc.length == 0) {
  273. return false;
  274. }
  275. if(!this.isPosInteger(cvc)) {
  276. return false;
  277. }
  278. return true;
  279. },
  280. isDefined: function(variable) {
  281. if (typeof (variable) === 'undefined' || typeof (variable) === null) {
  282. return false;
  283. }
  284. return true;
  285. },
  286. trimString: function(str){
  287. return str.trim();
  288. },
  289. isPosInteger: function(data) {
  290. var objRegExp = /(^\d*$)/;
  291. return objRegExp.test( data );
  292. }
  293. };
  294. }
  295. ,CreateFormItemSelect : function(id, info, title, option)
  296. {
  297. var html = '<select id="'+id+'" name="'+id+'" lay-verify="required">';
  298. for (var i in option) {
  299. html += '<option value="'+option[i]+'">'+i+'</option>';
  300. }
  301. html += '</select>';
  302. var style = 'float: left;padding-left: 0px;padding-top: 5px;';
  303. return this.CreateFormItem(html, title, style);
  304. }
  305. ,CreateFormItemInput : function(id, info, title)
  306. {
  307. var html = '<input type="text" name="'+id+'" id="'+id+'" required="" lay-verify="required" placeholder="'+info+'" autocomplete="off" class="layui-input">';
  308. var style = 'float: left;padding-left: 0px;padding-top: 5px;';
  309. return this.CreateFormItem(html, title, style);
  310. }
  311. ,CreateFormItem : function(content, title, style)
  312. {
  313. if (style) {
  314. var attr = 'style="'+style+'"';
  315. } else {
  316. var attr = 'class="layui-form-label"';
  317. }
  318. if (title) {
  319. title = '<label '+attr+'>'+title+'</label>';
  320. } else {
  321. title = '';
  322. }
  323. var html = '<div class="layui-form-item">'+title+'<div class="layui-input-block">' + content + '</div></div>';
  324. return html;
  325. }
  326. ,getUrlParameter : function(sParam)
  327. {
  328. var sPageURL = decodeURIComponent(window.location.search.substring(1)),
  329. sURLVariables = sPageURL.split('&'),
  330. sParameterName,
  331. i;
  332. for (i = 0; i < sURLVariables.length; i++) {
  333. sParameterName = sURLVariables[i].split('=');
  334. if (sParameterName[0] === sParam) {
  335. return sParameterName[1] === undefined ? true : sParameterName[1];
  336. }
  337. }
  338. }
  339. ,ValidateCreditCard : function(card)
  340. {
  341. var self = this;
  342. var card_types = [
  343. {
  344. name: 'amex',
  345. pattern: /^3[47]/,
  346. valid_length: [15]
  347. }, {
  348. name: 'diners_club_carte_blanche',
  349. pattern: /^30[0-5]/,
  350. valid_length: [14]
  351. }, {
  352. name: 'diners_club_international',
  353. pattern: /^36/,
  354. valid_length: [14]
  355. }, {
  356. name: 'jcb',
  357. pattern: /^35(2[89]|[3-8][0-9])/,
  358. valid_length: [16]
  359. }, {
  360. name: 'laser',
  361. pattern: /^(6304|670[69]|6771)/,
  362. valid_length: [16, 17, 18, 19]
  363. }, {
  364. name: 'visa_electron',
  365. pattern: /^(4026|417500|4508|4844|491(3|7))/,
  366. valid_length: [16]
  367. }, {
  368. name: 'visa',
  369. pattern: /^4/,
  370. valid_length: [16]
  371. }, {
  372. name: 'mastercard',
  373. pattern: /^5[1-5]/,
  374. valid_length: [16]
  375. }, {
  376. name: 'maestro',
  377. pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
  378. valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
  379. }, {
  380. name: 'discover',
  381. pattern: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
  382. valid_length: [16]
  383. }
  384. ];
  385. for (var i in card_types) {
  386. state = self.CheckCardMatch(card, card_types[i]);
  387. if (state) {
  388. state = self.CheckCardLength(card, card_types[i]);
  389. if (state) {
  390. return true;
  391. }
  392. }
  393. }
  394. return false;
  395. }
  396. ,CheckCardMatch : function(card, item)
  397. {
  398. if (card.match(item.pattern)) {
  399. return true;
  400. } else {
  401. return false;
  402. }
  403. }
  404. ,CheckCardLength : function(card, item)
  405. {
  406. var length = card.length;
  407. for (var i in item.valid_length) {
  408. if (item.valid_length[i] == length) {
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. }