Run mercado pago javascript into java code -
i need implement mercado pago custom checkout receive payments in site. need run javascript code card_token_id, described in link: https://developers.mercadopago.com/documentation/custom-checkout?lang=en_us.
but website, business restrictions, need card_token_id running java code, class. need run javascript code java class, result, e use him in http post. tried use scriptengine, not works.
see below complete code works in html.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>pagar</title> <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="https://secure.mlstatic.com/org-img/checkout/custom/1.0/checkout.js"></script> </head> <body> <h1><a href="http://developers.mercadopago.com/documentation/custom-checkout-advanced?lang=pt_br">fluxo de pagamento personalizado (avançado)</a></h1> <form action="" method="post" id="form-pagar-mp"> <input id="amount" type="hidden" value="100"/> <p>número cartão: <input data-checkout="cardnumber" type="text" value="4509953566233704"/></p> <p>código de segurança: <input data-checkout="securitycode" type="text" value="123"/></p> <p>mês de vencimento: <input data-checkout="cardexpirationmonth" type="text" value="12"/></p> <p>ano de vencimento: <input data-checkout="cardexpirationyear" type="text" value="2020"/></p> <p>titular cartão: <input data-checkout="cardholdername" type="text" value="joao"/></p> <p>número documento: <input data-checkout="docnumber" type="text" value="19313777584"/></p> <input data-checkout="doctype" type="text" value="cpf"/> <p id="issuersfield">bancos: <select id="issuersoptions"></select> <p>parcelas: <select id="installmentsoption"></select> <p><input type="submit" value="concluir pagamento"></p> </form> <script type="text/javascript"> /* troque com sua public_key */ checkout.setpublishablekey("test-cba0f2d5-c989-4c97-8e61-847121530093"); $("input[data-checkout='cardnumber']").bind("keyup",function(){ var bin = $(this).val().replace(/ /g, '').replace(/-/g, '').replace(/\./g, ''); if (bin.length == 6){ checkout.getpaymentmethod(bin,setpaymentmethodinfo); } }); // estabeleça informação meio de pagamento obtido function setpaymentmethodinfo(status, result){ $.each(result, function(p, r){ $.each(r.labels, function(pos, label){ if (label == "recommended_method") { checkout.getinstallments(r.id ,parsefloat($("#amount").val()), setinstallmentinfo); checkout.getcardissuers(r.id,showissuers); return; } }); }); }; // mostre parcelas disponíveis no div 'installmentsoption' function setinstallmentinfo(status, installments){ var html_options = ""; for(i=0; installments && i<installments.length; i++){ html_options += "<option value='"+installments[i].installments+"'>"+installments[i].installments +" de "+installments[i].share_amount+" ("+installments[i].total_amount+")</option>"; }; $("#installmentsoption").html(html_options); }; function showissuers(status, issuers){ var i,options="<select data-checkout='cardissuerid'><option value='-1'>escolha...</option>"; for(i=0; issuers && i<issuers.length;i++){ options+="<option value='"+issuers[i].id+"'>"+issuers[i].name +" </option>"; } options+="</select>"; if(issuers.length>0){ $("#issuersoptions").html(options); }else{ $("#issuersoptions").html(""); $("#issuersfield").hide(); } }; $("#issuersoptions").change(function(){ var bin = $("input[data-checkout='cardnumber']").val().replace(/ /g, '').replace(/-/g, '').replace(/\./g, '').slice(0, 6); checkout.getinstallmentsbyissuerid(bin,this.value,parsefloat($("#amount").val()),setinstallmentinfo); }); $("#form-pagar-mp").submit(function( event ) { var $form = $(this); checkout.createtoken($form, mpresponsehandler); event.preventdefault(); return false; }); var mpresponsehandler = function(status, response) { var $form = $('#form-pagar-mp'); if (response.error) { alert("ocorreu um erro: "+json.stringify(response)); } else { var card_token_id = response.id; $form.append($('<input type="text" id="card_token_id" name="card_token_id"/>').val(card_token_id)); alert("card_token_id: "+card_token_id); $form.get(0).submit(); } } </script> </body> </html>
Comments
Post a Comment