main.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. function init_components_payment(input){
  2. input.each(function(i,input){
  3. input = $(input);
  4. if(!input.is(":visible")) return;
  5. var action = input.attr('data-action');
  6. if(!action) {
  7. console.log('PAYMENT COMPONENT: Need "data-action" to get process payment');
  8. return;
  9. }
  10. $.action({
  11. action: 'stripe_init_component',
  12. process: action
  13. }, function(r){
  14. if(!r.content) return;
  15. //On utilise un wrapper pour gérer les overflows "out of the box"
  16. input.addClass('row card-container');
  17. var content = $(r.content);
  18. input.append(content);
  19. init_creditcard_component();
  20. });
  21. });
  22. }
  23. //CHARGEMENT DE LA PAGE
  24. function init_plugin_stripe(){
  25. switch($.urlParam('page')){
  26. default:
  27. break;
  28. }
  29. }
  30. //Init page settings
  31. function init_setting_global_stripe(parameter){
  32. switch(parameter.section){
  33. case 'global.stripe':
  34. $.each($('code'), function(idx, val){
  35. hljs.highlightBlock($(val).get(0));
  36. });
  37. break;
  38. }
  39. }
  40. function stripe_setting_save(){
  41. $.action({
  42. action : 'stripe_setting_save',
  43. fields : $('#stripe-setting-form').toJson()
  44. },function(){
  45. $.message('success','Configuration enregistrée');
  46. });
  47. }
  48. function stripe_card_execute(options,complete){
  49. var activeBtn = $('#shop-stripe-button > span.button-active'),
  50. loadingBtn = $('#shop-stripe-button > span.button-loading');
  51. var o = $.extend({
  52. card: '4242424242424242',
  53. cvv: '123',
  54. month:8,
  55. year: 2019,
  56. owner: ''
  57. },options);
  58. try{
  59. Stripe.setPublishableKey(o.key);
  60. Stripe.card.createToken({
  61. number: o.card,
  62. cvc: o.cvv,
  63. exp_month:o.month,
  64. exp_year: o.year,
  65. name: o.owner
  66. }, function (status, response) {
  67. activeBtn.removeClass('hidden');
  68. loadingBtn.addClass('hidden');
  69. complete(response);
  70. });
  71. } catch(e){
  72. activeBtn.removeClass('hidden');
  73. loadingBtn.addClass('hidden');
  74. complete({error:e});
  75. }
  76. }
  77. function stripe_card(options,complete){
  78. var stripeLib = 'https://js.stripe.com/v2/';
  79. if($('script[src="'+stripeLib+'"]',document).length == 0){
  80. var s = document.createElement( 'script' );
  81. s.type = 'text/javascript';
  82. s.setAttribute( 'src', stripeLib );
  83. s.onload = function(){ stripe_card_execute(options,complete)};
  84. document.body.appendChild(s);
  85. }else{
  86. stripe_card_execute(options,complete);
  87. }
  88. }
  89. //Fonction pour lancer l'initialisation
  90. //du paiement par Stripe (récup des données)
  91. function stripe_process_payment(public_key, processAction=null){
  92. try{
  93. if(!processAction) throw 'Erreur sur l\'action pour procéder au paiement';
  94. var activeBtn = $('#shop-stripe-button > span.button-active'),
  95. loadingBtn = $('#shop-stripe-button .button-loading');
  96. loadingBtn.removeClass('hidden');
  97. activeBtn.addClass('hidden');
  98. var cardNumber = $('#stripe-card-number'),
  99. cardCvv = $('#stripe-card-cvv'),
  100. cardExpire = $('#stripe-card-expire'),
  101. cardOwner = $('#stripe-card-owner');
  102. var expParts = cardExpire.val().split(' / ');
  103. var month = expParts[0],
  104. year = expParts[1];
  105. if(cardNumber.val() == '') throw 'Numéro de carte invalide';
  106. if(cardCvv.val() == '') throw 'Numéro CVV invalide';
  107. if(month == '') throw 'Mois d\'expiration invalide';
  108. if(year == '') throw 'Année d\'expiration invalide';
  109. stripe_card({
  110. key : public_key,
  111. card: cardNumber.val().replace(/ /g,''),
  112. cvv: cardCvv.val(),
  113. month: month,
  114. year: year,
  115. owner: cardOwner.val()
  116. },function(response){
  117. if(response.error) return $.message('error',response.error.code+' : '+response.error.message);
  118. $.action({
  119. action : processAction,
  120. token : response.id
  121. },function(r){
  122. reset_inputs('.card-container');
  123. loadingBtn.addClass('hidden');
  124. activeBtn.removeClass('hidden');
  125. $.message('success','Paiement effectué');
  126. });
  127. });
  128. } catch(e){
  129. $.message('error',e);
  130. loadingBtn.addClass('hidden');
  131. activeBtn.removeClass('hidden');
  132. }
  133. }
  134. // 4: VISA, 51 -> 55: MasterCard, 36-38-39: DinersClub, 34-37: American Express, 65: Discover
  135. function init_creditcard_component(){
  136. var cards = [{
  137. name: "mastercard",
  138. color: "#0061A8",
  139. logo: "fab fa-cc-mastercard"
  140. }, {
  141. name: "visa",
  142. color: "#daa520",
  143. logo: "fab fa-cc-visa"
  144. }, {
  145. name: "dinersclub",
  146. color: "#888",
  147. logo: "fab fa-cc-diners-club"
  148. }, {
  149. name: "americanExpress",
  150. color: "#108168",
  151. logo: "fab fa-cc-amex"
  152. }, {
  153. name: "discover",
  154. color: "#7ea8bb",
  155. logo: "fab fa-cc-discover"
  156. }];
  157. var html = document.getElementsByTagName('html')[0];
  158. var number = "";
  159. var cardNumber = $(".card_number");
  160. //Gestion de l'UI (surbrillance des éléments)
  161. $(document).click(function(e){
  162. if(!$(e.target).is(".cvv") || !$(e.target).closest(".cvv").length){
  163. $(".card").css("transform", "rotatey(0deg)");
  164. $(".seccode").css("color", "var(--text-color)");
  165. }
  166. if(!$(e.target).is(".expire") || !$(e.target).closest(".expire").length)
  167. $(".date_value").css("color", "var(--text-color)");
  168. if(!$(e.target).is(".number") || !$(e.target).closest(".number").length)
  169. cardNumber.css("color", "var(--text-color)");
  170. if(!$(e.target).is(".inputname") || !$(e.target).closest(".inputname").length)
  171. $(".fullname").css("color", "var(--text-color)");
  172. });
  173. //Gestion du champ N° Carte
  174. var selected_card = -1;
  175. $(".number").keyup(function(event){
  176. cardNumber.text($(this).val());
  177. number = $(this).val();
  178. if(parseInt(number.substring(0, 2)) > 50 && parseInt(number.substring(0, 2)) < 56){
  179. selected_card = 0;
  180. } else if(parseInt(number.substring(0, 1)) == 4){
  181. selected_card = 1;
  182. } else if(parseInt(number.substring(0, 2)) == 36 || parseInt(number.substring(0, 2)) == 38 || parseInt(number.substring(0, 2)) == 39){
  183. selected_card = 2;
  184. } else if(parseInt(number.substring(0, 2)) == 34 || parseInt(number.substring(0, 2)) == 37){
  185. selected_card = 3;
  186. } else if(parseInt(number.substring(0, 2)) == 65){
  187. selected_card = 4;
  188. } else if(parseInt(number.substring(0, 4)) == 5019){
  189. selected_card = 5;
  190. } else {
  191. selected_card = -1;
  192. }
  193. if(selected_card != -1){
  194. html.setAttribute("style", "--card-color: " + cards[selected_card].color);
  195. $(".bankid").attr("class", "bankid").addClass(cards[selected_card].logo).removeClass('hidden');
  196. } else {
  197. html.setAttribute("style", "--card-color: #cecece");
  198. $(".bankid").attr("class", "bankid").addClass('hidden');
  199. }
  200. if(cardNumber.text().length === 0)
  201. cardNumber.html("&#x25CF;&#x25CF;&#x25CF;&#x25CF; &#x25CF;&#x25CF;&#x25CF;&#x25CF; &#x25CF;&#x25CF;&#x25CF;&#x25CF; &#x25CF;&#x25CF;&#x25CF;&#x25CF;");
  202. }).focus(function(){
  203. cardNumber.css("color", "white");
  204. }).on("keydown input", function(){
  205. cardNumber.text($(this).val());
  206. if(event.key >= 0 && event.key <= 9)
  207. if($(this).val().length === 4 || $(this).val().length === 9 || $(this).val().length === 14)
  208. $(this).val($(this).val() + " ");
  209. })
  210. //Gestion du champ Nom
  211. $(".inputname").keyup(function(){
  212. $(".fullname").text($(this).val());
  213. if($(".inputname").val().length === 0) $(".fullname").text("NOM");
  214. return event.charCode;
  215. }).focus(function(){
  216. $(".fullname").css("color", "white");
  217. });
  218. //Gestion du champ CVV
  219. $(".cvv").focus(function(){
  220. $(".card").css("transform", "rotatey(180deg)");
  221. $(".seccode").css("color", "white");
  222. }).keyup(function(){
  223. $(".seccode").text($(this).val());
  224. if($(this).val().length === 0) $(".seccode").html("&#x25CF;&#x25CF;&#x25CF;");
  225. }).focusout(function() {
  226. $(".card").css("transform", "rotatey(0deg)");
  227. $(".seccode").css("color", "var(--text-color)");
  228. });
  229. //Gestion champ Date d'expiration
  230. var month = 0;
  231. $(".expire").keypress(function(event){
  232. if(event.charCode >= 48 && event.charCode <= 57){
  233. if($(this).val().length === 1){
  234. if(($(this).val()==1 && event.key<=2) || $(this).val()==0)
  235. $(this).val($(this).val() + event.key + " / ");
  236. } else if($(this).val().length === 0){
  237. if(event.key == 1 || event.key == 0){
  238. month = event.key;
  239. return event.charCode;
  240. } else {
  241. $(this).val(0 + event.key + " / ");
  242. }
  243. } else if($(this).val().length > 2 && $(this).val().length < 9){
  244. return event.charCode;
  245. }
  246. }
  247. return false;
  248. }).keyup(function(event){
  249. $(".date_value").html($(this).val());
  250. if(event.keyCode == 8 && $(".expire").val().length == 4) $(this).val(month);
  251. if($(this).val().length === 0) $(".date_value").text("MM / YYYY");
  252. }).keydown(function(){
  253. $(".date_value").html($(this).val());
  254. }).focus(function(){
  255. $(".date_value").css("color", "white");
  256. });
  257. }