Braintree-国外支付对接(三) 之Customer UI

article/2025/8/22 9:40:28

前篇:Braintree-国外支付对接(二) 中的支付按钮的生成是braintree自带的样式和事件控制的,即drop-in,生成的界面我们不能过多的更改和控制。所以假如我们想要自己编写控件,自己控制样式,但又能正常点击触发支付等事件。那么就使用Customer UI.

使用Customer UI需要映入的官方JS有很大不同,但是我们的后端是不需要变的,因为提交的参数和返回的参数是一样的。下面给出的例子,没有过多的写的样式很漂亮华丽,简单点:

<div class="wrapper"><div class="checkout container"><header><h1>Hi,<br>Let's test a transaction</h1><p>Make a test payment with Braintree using PayPal or a card</p></header><form id="payment-form" method="post" action="/checkouts/Create"><section><label for="amount"><span class="input-label">Amount</span><div class="input-wrapper amount-wrapper"><input id="amount" name="amount" type="tel" min="0.01" placeholder="Amount" value="0.01"></div></label><fieldset><legend>Card</legend><div class="cardinfo-card-number"><label class="cardinfo-label" for="cc-number">Card Number</label><div class='input-wrapper' id="cc-number"></div><div id="card-image"></div></div><div class="cardinfo-card-number"><label class="cardinfo-label" for="cc-number">CVV</label><div class='input-wrapper' id="cc-cvv"></div></div><div class="cardinfo-card-number"><label class="cardinfo-label" for="cc-expiration-date">Expiration-Date</label><div class='input-wrapper' id="cc-expiration-date"></div></div>     <button style="margin-top: 10px; background-color: pink;" class="button" id="card-button" type="button"><span style="padding: 0.7em 1em">Done</span></button></fieldset><fieldset><legend>Paypal</legend><button style="margin-top: 10px; background-color: pink;" class="button" disabled id="paypal-button" type="button"><span>Paypal</span></button><span id="payer"></span></fieldset></section><input id="nonce" name="payment_method_nonce" type="hidden" /><button class="button" type="submit"><span>Test Transaction</span></button></form></div>
</div><style>.cardinfo-card-number {position: relative;}.cardinfo-label {display: block;font-size: 11px;margin-bottom: 0.5em;text-transform: uppercase;
}.input-wrapper {border-radius: 2px;background: rgba(255, 255, 255, 0.86);height: 2.75em;border: 1px solid #eee;-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06);box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06);padding: 5px 10px;margin-bottom: 1em;
}#card-image{position: absolute;top: 36px;right: 0px;width: 44px;height: 28px;background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/346994/card_sprite.png);background-size: 86px 458px;border-radius: 4px;background-position: -100px 0;background-repeat: no-repeat;margin-bottom: 1em;}#card-image.visa{background-position: 0 -398px;}#card-image.master-card{background-position: 0 -281px;}#card-image.american-express{background-position: 0 -370px;}#card-image.discover{background-position: 0 -163px;}#card-image.maestro{background-position: 0 -251px;}#card-image.jcb{background-position: 0 -221px;}#card-image.diners-club{background-position: 0 -133px;}
</style>
<script src="\App_Themes\javascript\vendor\jquery-2.1.4.min.js"></script><script src="https://js.braintreegateway.com/web/3.29.0/js/client.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.29.0/js/hosted-fields.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.29.0/js/paypal.js"></script>
<script src="https://js.braintreegateway.com/web/3.29.0/js/paypal-checkout.js"></script>
<script src="https://js.braintreegateway.com/web/3.29.0/js/three-d-secure.js"></script><script>$(function () {var client_token = '@ViewBag.ClientToken';var paypalButton = document.querySelector('#paypal-button');var cardButton = document.querySelector('#card-button');braintree.client.create({authorization: client_token  }, function (clientErr, clientInstance) {  //****if (clientErr) {//console.error('Error creating client:', clientErr);return;}//paypal支付braintree.paypal.create({client: clientInstance  //****}, function (paypalErr, paypalInstance) {if (paypalErr) {console.error('Error creating PayPal:', paypalErr);return;}paypalButton.removeAttribute('disabled');paypalButton.addEventListener('click', function (event) {// Because tokenization opens a popup, this has to be called as a result of// customer action, like clicking a button. You cannot call this at any time.paypalInstance.tokenize({flow: 'checkout',amount: document.querySelector('#amount').value,currency: 'USD'// For more tokenization options, see the full PayPal tokenization documentation// http://braintree.github.io/braintree-web/current/PayPal.html#tokenize}, function (tokenizeErr, payload) {if (tokenizeErr) {if (tokenizeErr.type !== 'CUSTOMER') {console.log('Error tokenizing:', tokenizeErr);}return;}// Tokenization succeededdocument.querySelector('#nonce').value = payload.nonce;$("#payer").html(payload.details.email);paypalButton.firstChild.innerHTML = "switch account";console.log('Paypal Got nonce:', payload.nonce);});}, false);});///信用卡支付// Create input fields and add text styles  braintree.hostedFields.create({client: clientInstance,  //****// Add information for individual fieldsfields: {number: {selector: '#cc-number',placeholder: '1111 1111 1111 1111'},cvv: {selector: '#cc-cvv',placeholder: '123'},expirationDate: {selector: '#cc-expiration-date',placeholder: '10 / 2019'}}}, function (err, hostedFieldsInstance) {if (err) {console.log(err);return;}hostedFieldsInstance.on('cardTypeChange', function (event) {// Change card bg depending on card typeif (event.cards.length === 1) {//$(form).removeClass().addClass(event.cards[0].type);$('#card-image').removeClass().addClass(event.cards[0].type);//$('header').addClass('header-slide');// Change the CVV length for AmericanExpress cardsif (event.cards[0].code.size === 4) {hostedFieldsInstance.setAttribute({field: 'cvv',attribute: 'placeholder',value: '1234'});}} else {hostedFieldsInstance.setAttribute({field: 'cvv',attribute: 'placeholder',value: '123'});}});cardButton.addEventListener('click', function (event) {event.preventDefault();hostedFieldsInstance.tokenize(function (err, payload) {if (err) {console.log(err);return;}var a = payload.details.cardType;var b = payload.details.lastFour;//document.querySelector('#nonce').value = payload.nonce;// This is where you would submit payload.nonce to your servervar my3DSContainer;braintree.threeDSecure.create({client: clientInstance}, function (threeDSecureErr, threeDSecure) {if (threeDSecureErr) {console.log("Error creating 3DSecure" + threeDSecureErr);return;}else{threeDSecure.verifyCard({nonce: payload.nonce,amount: document.querySelector('#amount').value,addFrame: function (err, iframe) {// Set up your UI and add the iframe.my3DSContainer = document.createElement('div');my3DSContainer.appendChild(iframe);document.body.appendChild(my3DSContainer);},removeFrame: function () {//Remove UI that you added in addFrame.document.body.removeChild(my3DSContainer);}}, function (err, thpayload) {if (err) {console.log(err);return;}//hostedFieldsInstance.clear('number');//hostedFieldsInstance.clear('cvv');//hostedFieldsInstance.clear('expirationDate');debugger;if (thpayload.liabilityShiftPossible) {if (thpayload.liabilityShifted) {//Liablity has shifted//submitNonceToServer(payload.nonce);document.querySelector('#nonce').value = thpayload.nonce;console.log('Card Got nonce:', thpayload.nonce);} else {console.log("Invalid Card!");}} else {document.querySelector('#nonce').value = thpayload.nonce;console.log('No3D nonce:', thpayload.nonce);}});}});});}, false);});});});
</script>

界面效果:

paypal和信用卡的初始化方法是不一样的,信用卡的稍微复杂点。它们所依赖的js也是不一样的。

这里要注意,所有引入的braintree官方的js的版本必须要一致,原因嘛,你去改下版本试下就知道了。版本不一致的话,paypal支付或者信用卡支付的时候官方会返回错误给我们,提示就是必须引入的client.min.js版本和paypal-checkout.js或者其他的js要一致。你用到哪种支付的时候,就会提示你要与哪种支付所依赖的js版本要一致。

  1. client.min.js

         这是第一个要引入的,客户端支付控件初始化,其他支付方式的初始化都是依赖于braintree.client.create()返回的clientInstance作为参数去初始化的。代码中注释了*的地方。

    2. hosted-fields.min.js

           这是将信用卡支付中的输入框控件(卡号,CVV等)标记为信用卡支付时校验的字段,标记了之后我们就拿不到其客户真正输入的值,且一切相应的判断(比如卡号,CVV的正确性,必填性)这些就都不需要我们自己来控制。其外最重要的,不可让商城拿到客户的卡信息这是为了保护客户的财产泄露,引发商家的承担不必要的责任。入正式的时候需要进行PCI安全验证,这点上就很能让你通过了。

    3. paypal-checkout.js

           这是paypal支付需要的,braintree.paypal.create()。这个很简单的。

    4.three-d-secure.js

           3D安全校验需要的,这个东西启用的主要原因就是 为支付减少风险。也为商家转移责任。


参考资料:

  https://developers.braintreepayments.com/guides/hosted-fields/examples/javascript/v3

以上纯属个人独自研究成果,仅供参考,转载请注明出处



http://chatgpt.dhexx.cn/article/jtWLxfon.shtml

相关文章

iOS接入国际支付Stripe和Braintree

黑客技术 点击右侧关注&#xff0c;了解黑客的世界&#xff01; Java开发进阶 点击右侧关注&#xff0c;掌握进阶之路&#xff01; Linux编程 点击右侧关注&#xff0c;免费入门到精通&#xff01; 前言 最近在开发国际版APP时需要用到支付&#xff0c;由于资料比较少&#xff…

braintree api调用记录

国外的支付集成接入。 只使用基础的卡支付&#xff0c;跟PayPal支付。 braintree 有沙盒环境可以申请测试&#xff0c;有php sdk包直接下载调用&#xff0c;非常简单。 1&#xff0c;声明配置信息 private $_debug false;private $_pay_method braintree;private …

Android集成Paypal支付Braintree

最新发现Paypal的官方SDK已经不再维护了&#xff0c;所以需要把项目的支付做一下升级。 文档链接&#xff1a;点击这里 根据文档来看Paypal支付的集成相比以前简单了许多&#xff0c;下面我们讲一下集成步骤&#xff1a; 1&#xff1a;在 build.gradle 中添加以下内容 compil…

:braintree_Laravel和Braintree:中间件和其他高级概念

:braintree This article was peer reviewed by Viraj Khatavkar. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be! 本文由Viraj Khatavkar进行了同行评审。 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态&…

braintree_Laravel和Braintree,坐在树上……

braintree This article was peer reviewed by Younes Rafie and Wern Ancheta. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be! 这篇文章由Younes Rafie和Wern Ancheta进行了同行评审。 感谢所有SitePoint的同行评审人员使S…

braintree使用_使用Braintree v.zero SDK购买时间

braintree使用 This article was sponsored by Braintree. Thank you for supporting the sponsors who make SitePoint possible! 本文由Braintree赞助。 感谢您支持使SitePoint成为可能的赞助商&#xff01; Braintree touts itself as offering “Simple, powerful payment…

简单聊聊PayPal与BrainTree选型经历

2019年9月30日&#xff0c;PayPal公司被批准通过对国付宝的股权收购正式进入中国。2019年12月19日晚间&#xff0c;PayPal公司正式宣布&#xff0c;已完成对国付宝信息科技有限公司&#xff08;Gopay&#xff09;70%的股权收购。交易完成后&#xff0c;PayPal成为第一家获准在中…

Braintree PayPal 支付网关开发(二)

开发准备在上篇文章已经介绍 >>看这里 << 这篇文章说下Demo示例。 1. 开发流程图这里再贴一下&#xff08;很重要&#xff09;&#xff1a; 2. 前端页面 2.1 代码 <div class"wrapper"><div class"checkout container"><…

braintree_Braintree的透明重定向

braintree The mere mention of “PCI Compliance” usually elicits a combination of confused looks and sweaty palms from business owners who accept credit card payments online. But what does it really mean? 仅仅提到“ PCI Compliance”通常会引起混淆的外观和来…

uniapp app端 对接 braintree

背景 项目客户端是uniapp&#xff08;app端&#xff09;&#xff0c;用户是海外微信支付宝肯定不行了&#xff0c;安卓苹果端都要&#xff0c;可选的支付方式有&#xff1a;1、苹果支付谷歌支付。2、paypal。3、Stripe 我比较看好paypal&#xff0c;最后领导决定用braintree。…

braintree支付开发整合paypal

braintree支付开发 braintree介绍 流程介绍 前端从服务端请求一个客户端令牌&#xff0c;并初始化客户端SDK。 服务端SDK生成客户端令牌并将其发送回客户端 客户提交付款信息&#xff0c;客户端SDK将该信息传递给Braintree&#xff0c;并返回付款方式随机数 前端付款方式随…

uniapp|vue 中的 braintree 支付

参考文档&#xff1a; Braintree-国外支付对接&#xff08;一&#xff09; Braintree-国外支付对接&#xff08;二&#xff09; Braintree-国外支付对接&#xff08;三&#xff09; 前面的两篇文章&#xff0c;有详细介绍了 Braintress 的账号创建&#xff1b;以及 SandBox 测…

Braintree PayPal 支付网关开发(一)

一般网上消费流程&#xff1a; 消费者 > 商户网站 > 消费者账户银行 > 支付网关 > 支付处理系统 > 商户收款银行 Braintree 就是一种支付方式。 Braintree 支付网关开发的准备&#xff1a; Braintree 支付网关开发流程&#xff1a; 第1步&#xff1a;前端请求自…

Braintree-国外支付对接(二)

在前文 国外支付对接&#xff1a;Braintree&#xff08;一&#xff09;的基础上 已经拿到了相关配置信息&#xff0c;接下来就是码代码了&#xff0c;这里完成的主要功能是支付与退款。 在此之前&#xff0c;先说一下Briantree的支付流程&#xff1a; 第一步先生成clientToke…

app接入 Paypal BrainTree

BrainTree 是什么 braintree 一开始是一个独立支付网关&#xff08;gateway&#xff09;&#xff0c;后来在2013年左右&#xff08;没记错的话&#xff09;被 Paypal收购。收购之后基本可以看作与paypal是一家。 paypal 收购 braintree 之后 sdk 也转向重点接入 braintree&…

多种方式99.9%解决从PDF复制文字后乱码问题

背景 需要从PDF复制文字出来做笔记&#xff0c;可是谁知道PDF通过adobe打开后复制出来后是乱码&#xff0c;如下图所示&#xff1a; &#xff08;再次感谢guide哥整理的文档&#xff09; 解决 尝试过安装字体&#xff0c;可惜没卵用。 方法1-CAJViewer打开 用该软件打开后…

java word转pdf 在linux转pdf乱码解决方法

word转pdf word转pdf,完美转换 引入依赖 (maven仓库是没有的&#xff0c;需要在项目中引用) 链接: 下载地址. 然后在pom里面引入下面这段&#xff0c;依赖我们就搭建好了 <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</a…

word转pdf公式乱码_MathType转换成pdf符号丢失或乱码怎么办

一般写论文的时候是在Word中编写&#xff0c;在Word中写公式时一般是使用MathType&#xff0c;MathType编辑出来的公式非常标准与美观&#xff0c;很多国际期刊杂志都有这种要求。但是在将编写好的论文进行投稿时需要将Word文档转换成PDF文档&#xff0c;这样论文公式才不会发生…

itextpdf生成pdf中文乱码 (乱码中挣扎的自述)

生成pdf文件的方法有很多&#xff0c;网上也有很多的介绍&#xff0c;本文主要主要是讲生成pdf乱码的问题&#xff0c;而且还十分诡异&#xff0c;具体生成pdf的步骤同学们可以自己百度&#xff0c;也可以参考如下链接&#xff1a; https://www.cnblogs.com/LUA123/p/5108007.…

pdf转换html乱码怎么办,pdf转word后乱码怎么办?

pdf转word后乱码怎么办&#xff1f;网络上面有一些PDF资料你可以对其内容复制&#xff0c;但是粘贴到word或者文本中就是一堆乱码&#xff0c;你用转换软件转换出来&#xff0c;有一些文件不会是乱码&#xff0c;但是还有一些文件依旧是乱码&#xff0c;怎么办呢&#xff1f;今…