Braintree PayPal 支付网关开发(二)

开发准备在上篇文章已经介绍 >>看这里 << 这篇文章说下Demo示例。
1. 开发流程图这里再贴一下(很重要):
Braintree PayPal 支付网关开发(二)
文章图片

2. 前端页面
2.1 代码

PayPal支付SandBox演示


Braintree PayPal 支付网关开发(二)
文章图片

ShippingAddress
FirstName: 
LastName:   
Email: 
City:        
Line1: 
Line2: 
PostalCode: 

Braintree PayPal 支付网关开发(二)
文章图片

Braintree PayPal 支付网关开发(二)
文章图片

2.2 选择PayPal支付后,JS提交支付信息给BratinTree服务器获取nonce及相关信息,页面如下:
Braintree PayPal 支付网关开发(二)
文章图片

2.3 JS中获取nonce及相关信息调试截图如下:Braintree PayPal 支付网关开发(二)
文章图片

2.4 这里确认信息后选择PayPal支付,会转到后端去进行支付操作。
3. 后端
3.1 PayPalController
public class PayPalController : Controller { PayPalAPI payPalAPI = new PayPalAPI(); public ActionResult Index() { return View(); }//获取TOKEN public 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; }//获取nonce var nonce = Request["payment_method_nonce"]; return payPalAPI.SendNonce(amount, nonce, shippingAddress); } }

3.2 PayPalAPI
public class PayPalAPI { #region Files private 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 TOKEN public 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 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 "支付完成!"; } }

【Braintree PayPal 支付网关开发(二)】4. 整体流程

5. Braintree sandbox中的交易记录:
Braintree PayPal 支付网关开发(二)
文章图片

6. 源码:https://github.com/wangqilong1225/BratinTree_PayPal_Demo

    推荐阅读