braintree支付开发整合paypal

article/2025/8/22 23:20:14

braintree支付开发

braintree介绍

流程介绍

braintree流程图

  1. 前端从服务端请求一个客户端令牌,并初始化客户端SDK。
  2. 服务端SDK生成客户端令牌并将其发送回客户端
  3. 客户提交付款信息,客户端SDK将该信息传递给Braintree,并返回付款方式随机数
  4. 前端付款方式随机数发送给服务端
  5. 服务端收到付款方式随机数后,使用SDK向Braintree发起交易请求。

环境配置

注册Braintree帐号

  • 前往braintree开发者文档注册沙盒帐号

  • 登录后点击齿轮⚙==>API==>Keys==>APIKeys==>PrivateKey==>View 备用

    BraintreeGateway gateway = new BraintreeGateway(Environment.SANDBOX,"f6tw2cm2c24yfbs7","ds8szsps6fc9tjky","bf4a3e7971a8e4be2dada2f7ca19573a"
    );
    

注册Paypal帐号

  • 前往paypal开发者文档注册沙盒帐号

  • 登录沙盒控制台后点击 My Apps & Credentials 选择 SandBox

  • 创建app ---------- REST API apps下点击Create app

    1. 输入 App name
    2. Sandbox Business Account 选择一个business.example.com帐号
  • 获取app参数备用--------点击进入app

    #### Sandbox account ####
    luozong@business.example.com
    #### Client ID #####
    ATN8f09gmB9Njm0iuoyApCV04GXbhbfltRzQPMHtjEL3i2oIZO0ShB31nIczY_hK1W0bVbYRUQaKIer6
    #### Secret ####
    EGQzXV9F46OEBFBZHNkk04CAIXRwZW1r4K6X40anp2RKL4dYa4q-11qLxcuW1fyHZ-DA01Y9Oa_xC86j
    

设置braintree链接到paypal

  • 前往braintree开发者文档登录braintree沙盒控制台

  • 登录后点击齿轮==>Processing==>Processing Options==>Payment Methods==>Paypal==>Link Sandbox

  • 进入到PayPal Sandbox Credentials ,填写以下信息

    ####PayPal Email#####
    luozong@business.example.com
    ####PayPal Client Id####
    ATN8f09gmB9Njm0iuoyApCV04GXbhbfltRzQPMHtjEL3i2oIZO0ShB31nIczY_hK1W0bVbYRUQaKIer6
    ####PayPal Client Secret####
    EGQzXV9F46OEBFBZHNkk04CAIXRwZW1r4K6X40anp2RKL4dYa4q
    

    点击 Link Paypal Sandbox 按钮

直接支付

直接支付

公司的流程和代码不符合,看看流程就好

直接支付流程图

前端代码

参考官方Set Up Your Client

该例子创建了 信用卡支付、paypal支付

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><!--jquery为了调用服务端获取token--><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.2.1/jquery.min.js" crossorigin="anonymous"></script><!--braintree出信用卡要加--><script src="https://js.braintreegateway.com/web/dropin/1.23.0/js/dropin.min.js"></script><!--出paypal按钮要加--><script src="https://www.paypal.com/sdk/js?client-id=sb"></script><!--沙盒client-id=sb,pro client-id取paypal的clientID--><script src="https://js.braintreegateway.com/web/3.64.2/js/client.min.js"></script><script src="https://js.braintreegateway.com/web/3.64.2/js/paypal-checkout.min.js"></script><script>$(function(){//获取tonkenvar token = null;$.ajax({url:'/braintree/getToken',type:'post',success:function(data){token = data;},async:false});//渲染信用卡交易按钮var button = document.querySelector('#submit-button');//信用卡输入显示braintree.dropin.create({authorization: token,container: '#dropin-container'}, function (createErr, instance) {button.addEventListener('click', function () {instance.requestPaymentMethod(function (err, payload) {// Submit payload.nonce to your server//发送payload.nonce以及订单信息到服务端});});});// 创建paypal按钮代码braintree.client.create({authorization: token}, function (clientErr, clientInstance) {// Stop if there was a problem creating the client.// This could happen if there is a network error or if the authorization// is invalid.if (clientErr) {console.error('Error creating client:', clientErr);return;}// Create a PayPal Checkout component.braintree.paypalCheckout.create({client: clientInstance}, function (paypalCheckoutErr, paypalCheckoutInstance) {if(paypalCheckoutErr){console.error('Error creating paypalCheckoutInstance:', paypalCheckoutErr);}paypalCheckoutInstance.loadPayPalSDK({currency: 'USD',intent: 'capture'}, function () {paypal.Buttons({fundingSource: paypal.FUNDING.PAYPAL,createOrder: function () {//此处可以去服务端下订单,获取订单具体信息在放到createPayment里面return paypalCheckoutInstance.createPayment({flow: 'checkout', // Requiredamount: 10.00, // Requiredcurrency: 'USD', // Required, must match the currency passed in with loadPayPalSDKintent: 'capture', // Must match the intent passed in with loadPayPalSDK//地址信息enableShippingAddress: true,shippingAddressEditable: false,shippingAddressOverride: {recipientName: 'Scruff McGruff',line1: '1234 Main St.',line2: 'Unit 1',city: 'Chicago',countryCode: 'US',postalCode: '60652',state: 'IL',phone: '123.456.7890'}});},onApprove: function (data, actions) {return paypalCheckoutInstance.tokenizePayment(data, function (err, payload) {// Submit `payload.nonce` to your server//发送payload.nonce以及订单信息到服务端请求进行一次付款交易(transaction)});},onCancel: function (data) {console.log('PayPal payment cancelled', JSON.stringify(data, 0, 2));},onError: function (err) {console.error('PayPal error', err);}}).render('#paypal-button').then(function () {// The PayPal button will be rendered in an html element with the ID// `paypal-button`. This function will be called when the PayPal button// is set up and ready to be used});});});});});</script></head><body><div id="dropin-container"></div><button id="submit-button">Request payment method</button><div id="paypal-button"></div></body>
</html>

后端代码

//简单的gatway配置,来自braintree帐号
public class GatewayFactory {public static BraintreeGateway getlisiGateway() {BraintreeGateway gateway = new BraintreeGateway(Environment.SANDBOX,"f6tw2cm2c24yfbs7", "ds8szsps6fc9tjky", "bf4a3e7971a8e4be2dada2f7ca19573a");return gateway;}
}
@RestController
@RequestMapping("/braintree")
public class BraintreeController {private static BraintreeGateway gateway = GatewayFactory.getlisiGateway();@PostMapping("/getToken")public String getToken(){//官方写new ClientTokenRequest().customerId("customerId")可以不要。ClientTokenRequest clientTokenRequest = new ClientTokenRequest();String clientToken = gateway.clientToken().generate(clientTokenRequest);return clientToken;}//从前端获取的交易信息不只有nonce,还有金额,设备等,orderId也可以传@PostMapping("/pay")public Msg pay(String nonce) {TransactionRequest request = new TransactionRequest().amount(new BigDecimal("10.00")).paymentMethodNonce(nonce).deviceData("h5").options().submitForSettlement(true).done();Result<Transaction> result = gateway.transaction().sale(request);if(result.isSuccess()){return Msg.ok(result.getTarget().getStatus().toString());}else{return Msg.fail(result.getMessage());}}
}

坑点

  • 官方写new ClientTokenRequest().customerId(“customerId”)可以不要。
  • Transaction状态会自动转,算是官方的审核时间,自动的。
    • 信用卡 submit_for_settlted 后续过30分钟自动转为 settlted
    • paypal settlting 后续过30分钟自动转为 settlted

订阅支付

订阅支付

公司的流程和代码不符合,看看流程就好

订阅支付

前端代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.2.1/jquery.min.js" crossorigin="anonymous"></script><!--信用卡支付--><script src="https://js.braintreegateway.com/web/dropin/1.23.0/js/dropin.min.js"></script><!--paypal支付--><script src="https://js.braintreegateway.com/web/3.64.2/js/client.min.js"></script><script src="https://js.braintreegateway.com/web/3.64.2/js/paypal-checkout.min.js"></script><script src="https://www.paypal.com/sdk/js?client-id=sb&vault=true"></script><script>$(function(){var token = null;$.ajax({url:'/braintree/getToken?'+new Date().getTime(),type:'post',success:function(data){token = data;},async:false});alert(token);var button = document.querySelector('#submit-button');braintree.dropin.create({authorization: token,container: '#dropin-container'}, function (createErr, instance) {button.addEventListener('click', function () {instance.requestPaymentMethod(function (err, payload) {// Submit payload.nonce to your serverif(confirm("是否继续支付0.01USD?")){$.ajax({url:'/braintree/pay?'+new Date().getTime(),type:'post',data:'nonce='+payload.nonce,success:function(data){alert(data.msg);}});}});});});// Create a client.braintree.client.create({authorization: token}, function (clientErr, clientInstance) {alert(clientInstance);// Stop if there was a problem creating the client.// This could happen if there is a network error or if the authorization// is invalid.if (clientErr) {console.error('Error creating client:', clientErr);return;}// Create a PayPal Checkout component.braintree.paypalCheckout.create({client: clientInstance}, function (paypalCheckoutErr, paypalCheckoutInstance) {if(paypalCheckoutErr){console.error('Error creating paypalCheckoutInstance:', paypalCheckoutErr);}paypalCheckoutInstance.loadPayPalSDK({currency: 'USD',intent: 'capture',vault: true}, function () {paypal.Buttons({fundingSource: paypal.FUNDING.PAYPAL,createBillingAgreement: function () {return paypalCheckoutInstance.createPayment({flow: 'vault' // Required});},onApprove: function (data, actions) {return paypalCheckoutInstance.tokenizePayment(data, function (err, payload) {// Submit `payload.nonce` to your serverdebugger;if(confirm("是否继续支付10.00USD?")){$.ajax({url:'/braintree/pay2?'+new Date().getTime(),type:'post',data:'nonce='+payload.nonce,success:function(data){alert(data.msg);}});}});},onCancel: function (data) {console.log('PayPal payment cancelled', JSON.stringify(data, 0, 2));},onError: function (err) {console.error('PayPal error', err);}}).render('#paypal-button').then(function () {// The PayPal button will be rendered in an html element with the ID// `paypal-button`. This function will be called when the PayPal button// is set up and ready to be used});});});});});</script></head><body><div id="dropin-container"></div><div id="paypal-button"></div><button id="submit-button">Request payment method</button></body>
</html>

后端代码

@RestController
@RequestMapping("/braintree")
public class BraintreeController {private static BraintreeGateway gateway = GatewayFactory.getlisiGateway();@PostMapping("/getToken")public String getToken(){ClientTokenRequest clientTokenRequest = new ClientTokenRequest();String clientToken = gateway.clientToken().generate(clientTokenRequest);return clientToken;}@PostMapping("/pay2")public Msg pay2(String nonce) {//保存用户CustomerRequest customerRequest = new CustomerRequest().id(UUID.randomUUID().toString()).paymentMethodNonce(nonce).firstName("Fred").lastName("Jones").creditCard().done();Result<Customer> customerResult = gateway.customer().create(customerRequest);Customer customer = gateway.customer().find(customerResult.getTarget().getId());String token = customer.getDefaultPaymentMethod().getToken();SubscriptionRequest subscriptionRequest = new SubscriptionRequest().planId("jhk2").paymentMethodToken(token);Result<Subscription> result = gateway.subscription().create(subscriptionRequest);if(result.isSuccess()){return Msg.ok(result.getTarget().getStatus().toString());}else{return Msg.fail(result.getMessage());}}
}

坑点

  • 订阅如果一直被declined 2046,表示帐号有问题,需要重新注册。我郁闷了好几天,最后重新注册就好了。


http://chatgpt.dhexx.cn/article/4IDDHiWR.shtml

相关文章

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;选中…

新版macbook,PPT导出PDF复制文字乱码问题的解决

参考资料&#xff1a; 福昕阅读器的文档&#xff1a;https://m.foxitsoftware.cn/company/product/964.html 微软支持(完全一模一样的问题) 先说一下&#xff0c;我的这个PDF复制文字乱码问题确实是被我解决了&#xff0c;但是我的这个PDF是有源文件的&#xff1a;一个PPT演示文…

解决从PDF复制文字后乱码问题

背景 需要从PDF复制文字出来做笔记&#xff0c;可是谁知道PDF通过adobe打开后复制出来后是乱码&#xff0c;如下图所示&#xff1a; 解决 尝试过安装字体&#xff0c;可惜没卵用。 方法1-CAJViewer打开 用该软件打开后复制&#xff0c;可以完美复制&#xff0c;但是有个小问题…

pdf复制乱码_教程如何将公众号文章导出为pdf

0.说明 最近有一些小伙伴在后台问我们文章有没有word版&#xff0c;因为可能对着手机小小的屏幕看这些公式什么的确实有点头疼&#xff0c;而且有的小伙伴还需要上课哈。 其实word文档的doc/docx并不是最佳的&#xff0c;在不同环境可能会出现公式乱码的情况&#xff0c;相比之…