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,表示帐号有问题,需要重新注册。我郁闷了好几天,最后重新注册就好了。

braintree支付开发整合paypal相关推荐

  1. SpringBoot整合微信支付开发在线教育视频网站(完整版)

    目录 ├─code.zip ├─第 1 章项目介绍和前期准备 │  ├─1-1 SpringBoot整合微信支付开发在线教育视频站点介绍.TS │  ├─1-2 中大型公司里面项目开发流程讲解.TS ...

  2. 国际支付 Superpay、Paypal、Braintree、Stripe

    2019年做了个海外的商城项目,年底接触到国际支付,分别接触到的是 Superpay.Paypal.Braintree.Stripe,说下我的认识.与后台交互的大致流程: Superpay 是将微信. ...

  3. php paypal源码,PHP整合PayPal支付

    简单整理一下PHP项目整合PayPal支付功能. 一.表单的构建: 二.IPN验证部分 class paypal { var $ipn_data = array(); // array contain ...

  4. braintree php 开发,PHP关于Braintree支付

    现国内关于Braintree支付的资料少之又少,所以我来分享下关于Braintree支付的相关代码,希望能对需要的人有所帮助. 一 :获取key以及MerchantID. 1.1:首先在https:/ ...

  5. uniapp|vue 中的 braintree 支付

    参考文档: Braintree-国外支付对接(一) Braintree-国外支付对接(二) Braintree-国外支付对接(三) 前面的两篇文章,有详细介绍了 Braintress 的账号创建:以及 ...

  6. php实现支付宝支付接口,PHP实现个人支付宝支付开发(二)

    在前面的文章<PHP实现个人支付宝支付开发(一)>中,我们为大家简单介绍了通过第三方接口平台码支付来实现个人支付宝支付功能的交易流程. 下面我们继续结合图文,给大家介绍通过码支付实现PHP ...

  7. 微信服务号 微信支付开发

    微信支付,须要通过微信支付验证 眼下.支付仅限服务号, 做微信支付开发,主要看开发文档 统一下单. 订单查询 退款等 1.发起支付.都是通过h5发起的,首先获取prepay_id 发起支付,须要统一下 ...

  8. 微信公众号支付开发全过程(java版)

    文章有不当之处,欢迎指正,如果喜欢微信阅读,你也可以关注我的微信公众号:好好学java,获取优质学习资源. 一.微信官方文档微信支付开发流程(公众号支付) 首先我们到微信支付的官方文档的开发步骤部分查 ...

  9. 微信支付通知 php,微信支付开发交易通知实例

    一.交易通知 用户在成功完成支付后,微信后台通知(POST)商户服务器(notify_url)支付结果.商户可以使用notify_url的通知结果进行个性化页面的展示. 对后台通知交互时,如果微信收到 ...

最新文章

  1. EM: 生而为菌,自强不息-嗜酸杆菌在重金属污染土壤中的生态适应性机制
  2. uni-app如何取消pages页面的默认返回按钮【autoBackButton属性】
  3. python not in range1002无标题_17个新手常见Python运行时错误
  4. Fragment 生命周期:
  5. FFmpeg 音视频处理总纲
  6. apache+php32位平台安装
  7. 输出国际象棋输出余弦曲线
  8. 修改PHP上传文件的大小限制
  9. 有哪些典型的「学生思维」?
  10. 自写保存字符串或文件为asp.net缓存的类
  11. cmi码型变换matlab程序_58 张图,手把手教会你 Simscape Multibody 物理建模与刚体变换!...
  12. 李彦宏开年内部信:2018营收破千亿,做出好产品的百度已归来
  13. MathType | 一款强大的公式编辑器
  14. 发布地图服务时导入已有的tpk切片包作为缓存
  15. CTFshow-萌新
  16. 全志A40i移植 RTL8188FTV/RTL8188FU USB-WiFi
  17. alert promt confirm js 用法
  18. 施工现场工地监管如何能够接入4G摄像头实现流媒体服务器视频监控?
  19. [Vijos]P1788 第k大
  20. 万字长文综述:文本增强技术的研究进展及应用实践

热门文章

  1. Windows11 上开发iOS是否可行呢?有什么解决方案呢
  2. E+H PH电极CPF81-NN11A3
  3. SLAM导航机器人零基础实战系列:(六)SLAM建图与自主避障导航——3.ros-navigation机器人自主避障导航...
  4. 比较、排序、数字溢出[5969] 摧毁小行星
  5. 实现语音对讲_BGS实现800兆对讲机和手持终端的语音通信融合
  6. 课程式学习 Curriclum Learning
  7. IT面试时注意的一些问题
  8. Ubuntu工具-2 OBS Studio
  9. 免费的网课API接口附加实例
  10. 传统企业转型互联网的十大死法