Braintree PayPal 支付网关开发(二)

article/2025/8/22 23:11:08

开发准备在上篇文章已经介绍 >>看这里 << 这篇文章说下Demo示例。


1. 开发流程图这里再贴一下(很重要):
在这里插入图片描述
2. 前端页面
    2.1 代码

<div class="wrapper"><div class="checkout container"><div style="text-align:center;"><span style="font-size:22px;">PayPal支付SandBox演示</span></div><br /><form id="payment-form" method="post" action="/PayPal/SendNonce" style="width: 500px; border: 2px solid black;margin-left: 31%; height: 350px;"><section style="text-align: center;" id="sectionID"><br /><br /><label for="amount"><span class="input-label" style="font-size: 16px;">Amount:</span><input id="amount" name="amount" type="tel" min="0.01" placeholder="Amount" value="0.01"></label></section><input id="nonce" name="payment_method_nonce" type="hidden" /><img style="margin-left: 33%; margin-top: 8%;" id="paypal-button" src="https://www.paypalobjects.com/digitalassets/c/website/marketing/apac/C2/logos-buttons/34_Grey_CheckOut_Pill_Button.png"><div id="shippingID"><br /><span class="shipClass" style="font-size: 18px;font-weight: bold; margin-left: 35%;">Shipping   Address</span><br /><hr /><span class="shipClass" style="font-size: 16px;">FirstName:</span>&nbsp; <input name="" id="payerFirstName" /><br /><span class="shipClass" style="font-size: 16px;">LastName:</span>&nbsp;&nbsp; <input name="LastName" id="payerLastName"><br /><span class="shipClass" style="font-size: 16px;">Email:</span>&nbsp; <input name="Email" id="payerEmail"><br /><span class="shipClass" style="font-size: 16px;">City: </span>&nbsp;&nbsp;&nbsp;&nbsp; <input name="City" id="payerCity"><br /><span class="shipClass" style="font-size: 16px;">Line1:</span>&nbsp; <input name="Line1" id="payerLine1"><br /><span class="shipClass" style="font-size: 16px;">Line2:</span>&nbsp; <input name="Line2" id="payerLine2"><br /><span class="shipClass" style="font-size: 16px;">PostalCode:</span>&nbsp; <input name="PostalCode" id="payerPostalCode"><br /><br /><img style="margin-left: 34%;" id="btnSubimit" src="https://www.paypalobjects.com/digitalassets/c/website/marketing/apac/C2/logos-buttons/34_Grey_CheckOut_Pill_Button.png"></div></form></div>
</div><script src="~/Content/jquery-2.1.1.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>$(document).ready(function () {$("#shippingID").hide();$("#btnSubimit").click(function () {$("#payment-form").submit();});//GET 请求去拿TOKEN$.get("/PayPal/GetPayPalClienToken", function (Data) {       $(function () {var client_token = Data;var paypalButton = document.querySelector('#paypal-button');var cardButton = document.querySelector('#card-button');braintree.client.create({authorization: client_token         //身份验证}, function (clientErr, clientInstance){  if (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) {    //支付按钮监听事件paypalInstance.tokenize({flow: 'checkout',amount: document.querySelector('#amount').value,currency: 'AUD'}, function (tokenizeErr, payload) {                     if (tokenizeErr) {if (tokenizeErr.type !== 'CUSTOMER') {console.log('Error tokenizing:', tokenizeErr);}return;}// Tokenization succeeded  document.querySelector('#nonce').value = payload.nonce;$("#paypal-button").hide();$("#sectionID").hide();$("#shippingID").show();$("#payerFirstName").val(payload.details.firstName);$("#payerLastName").val(payload.details.lastName);$("#payerLine1").val(payload.details.shippingAddress.line1);$("#payerLine2").val(payload.details.shippingAddress.line2);$("#payerEmail").val(payload.details.email);$("#payerCity").val(payload.details.shippingAddress.city);$("#payerPostalCode").val(payload.details.shippingAddress.postalCode);paypalButton.firstChild.innerHTML = "switch account";console.log('Paypal Got nonce:', payload.nonce);});}, false);});});});});});
</script>

在这里插入图片描述
      2.2 选择PayPal支付后,JS提交支付信息给BratinTree服务器获取nonce及相关信息,页面如下:
在这里插入图片描述
      2.3 JS中获取nonce及相关信息调试截图如下:在这里插入图片描述

      2.4 这里确认信息后选择PayPal支付,会转到后端去进行支付操作。

3. 后端
    3.1 PayPalController

public class PayPalController : Controller{PayPalAPI payPalAPI = new PayPalAPI();public ActionResult Index(){return View();}//获取TOKENpublic string GetPayPalClienToken(){ViewBag.ClientToken = payPalAPI.GetPayPalClientToken();return payPalAPI.GetPayPalClientToken();}public string SendNonce(){decimal amount=0.0m;//获取收货地址信息ShippingAddress shippingAddress = new ShippingAddress();try{amount =Convert.ToDecimal(Request["amount"]);shippingAddress = new ShippingAddress {FirstName = Request["FirstName"],LastName= Request["LastName"],Email= Request["Email"],City= Request["City"],Line1= Request["Line1"],Line2= Request["Line2"],PostalCode= Request["PostalCode"]};}catch (FormatException e){               return e.Message;}//获取noncevar nonce = Request["payment_method_nonce"];return payPalAPI.SendNonce(amount, nonce, shippingAddress);}}

        3.2 PayPalAPI

public class PayPalAPI{#region Filesprivate string Environment { get; set; }private string MerchantId { get; set; }private string PublicKey { get; set; }private string PrivateKey { get; set; }#endregion//Create Braintree Gateway Braintree网关初始化public BraintreeGateway CreateGateway(){Environment = System.Environment.GetEnvironmentVariable("BraintreeEnvironment");MerchantId = System.Environment.GetEnvironmentVariable("BraintreeMerchantId");PublicKey = System.Environment.GetEnvironmentVariable("BraintreePublicKey");PrivateKey = System.Environment.GetEnvironmentVariable("BraintreePrivateKey");if (MerchantId == null || PublicKey == null || PrivateKey == null){Environment = ConfigurationManager.AppSettings["BraintreeEnvironment"];MerchantId = ConfigurationManager.AppSettings["BraintreeMerchantId"];PublicKey = ConfigurationManager.AppSettings["BraintreePublicKey"];PrivateKey = ConfigurationManager.AppSettings["BraintreePrivateKey"];}return new BraintreeGateway(Environment, MerchantId, PublicKey, PrivateKey);}//Get PayPal TOKENpublic string GetPayPalClientToken(){BraintreeGateway gateway = CreateGateway();string ClientToken = gateway.ClientToken.Generate();return ClientToken;}//支付public string SendNonce(decimal amount, string nonce, ShippingAddress shippingAddress){BraintreeGateway gateway = CreateGateway();var request = new TransactionRequest{Amount = amount,PaymentMethodNonce = nonce,Options = new TransactionOptionsRequest{SubmitForSettlement = true},ShippingAddress = new AddressRequest{FirstName = shippingAddress.FirstName,LastName = shippingAddress.LastName,PostalCode= shippingAddress.PostalCode,StreetAddress= shippingAddress.Line1+ shippingAddress.Line2,}};//网关支付Result<Transaction> result = gateway.Transaction.Sale(request);//支付成功if (result.IsSuccess()){Transaction transaction = result.Target;}else if (result.Transaction != null){return "Transaction is null,Id=" + result.Transaction.Id;}else{string errorMessages = "";foreach (ValidationError error in result.Errors.DeepAll()){errorMessages += "Error: " + (int)error.Code + " - " + error.Message + "\n";}return errorMessages;}return "支付完成!";}}

4. 整体流程
在这里插入图片描述
5. Braintree sandbox中的交易记录:
在这里插入图片描述
6. 源码:https://github.com/wangqilong1225/BratinTree_PayPal_Demo


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

相关文章

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;今…

表格生成pdf 中字乱码

表格生成pdf及解决中字乱码 npm库表格生成pdf的超简洁小例子(用的是npm导入字体)两种解决乱码方法直接引入npm引入在项目中导入stsong-font在所需的页面上引用最后在生成pdf函数中使用(同上) npm库 两个必备包 jspdf npm i jspdfjspdf-autotable npm i jspdf-autotable在所需…

php生成pdf乱码_ierport 生成pdf出现乱码问题

iReport导出pdf中文乱码问题解决 使用iReport的过程经常遇到一些乱码的问题&#xff0c;最近用iReport导出pdf的时候就遇到中文不能显示的问题。 要使导出的pdf能够显示中文&#xff0c;需要用到iTextAsian.jar包。 1.将显示中文的地方Text属性设置成支持中文的字体。 Pdf font…

PDF文件复制文本为乱码

PDF文件可能会出现复制文本粘贴成乱码的现象。原因是PDF中所用的字体无法在电脑中找到&#xff0c;点击编辑器的”文件-属性“&#xff0c;查看字体&#xff0c;如果字体可以下载&#xff0c;可以在网上下载安装&#xff0c;就可以进行复制粘贴。 但有的PDF为保护内容&#xff…

pdf复制乱码_网站推荐 | 从未见过你这么不单纯的PDF转换器!

点击蓝字 关注我们 之前给大家分享过几期PDF转换器 但是仍然觉得部分线上转换器会出现 格式乱码、甚至字乱码的现象 (字乱码是由于外文网址识别误差) 这次挥挥找到了一个PDF转word 近乎完美保留原格式的在线转换网站&#xff01; 一起来看看叭 Today PDF转换器 http://www.pdfd…

Pdf 解密后复制文字乱码

1、安装cajviewer 这个工具 2、用CAJviewer打开pdf文档 3、选择图像4、点文字识别&#xff0c;这时候就弹窗一个框&#xff0c;里面是可复制的文本&#xff0c;而且准确率比较高 转载于:https://www.cnblogs.com/wangyuelang0526/p/3735398.html

使用latex撰写中文科技论文时,生成的PDF复制中文时乱码(不能查重),解决办法如下

在投稿中文期刊《控制理论与应用》时&#xff0c;期刊要求必须用latex&#xff0c;官方给的编辑器是WinEdt7.0&#xff0c;但是如果用常用的编译方式生成PDF的话&#xff0c;也就是第一个选项PDFTeXify 打开生成的PDF表面上看是没有任何问题的&#xff0c;可以正常阅读。但是如…

Tex导出PDF乱码问题

目录 工作环境错误pdf导出方式正确的pdf导出方式 工作环境 我用的是CTex TexStudio。CTex是为了方便中文字体。 错误pdf导出方式 一般我们都是点上方菜单栏的双箭头的按钮进行Tex文件编译&#xff0c;右边就会实时显示pdf内容。当然此时文件夹内也会产生相应的pdf文件。 我…

pdf复制乱码_SCI必备利器:选中即翻译,PDF文献也能一键翻译了。

来源丨软件通 ← 关注Ta 都是干货&#xff0c;无需引导 请仔细阅读全文。 熟悉小通的童鞋都知道&#xff0c;我们只推荐过一款sci论文翻译软件&#xff0c;那就是&#xff1a;CopyTranslator。复制即翻译&#xff0c;现在又更新了。新版本支持选中即翻译厉害吧&#xff01;选中…