BrainTree是一個國外集成信用卡支付的卡包。
沙盒登陸地址:
https://sandbox.braintreegateway.com/login
登陸沙盒得到商戶ID、公鑰、私鑰。

1.配置web.config
<BraintreeSection>
<BraintreeAuthentication
Environment="SANDBOX" //生產環境為PRODUCT
MerchantId="bnnvkfgf3crhvbcs"
PublicKey="rp28gffvwtj7gygz"
PrivateKey="906d2077d76e81f90b51c3b409682815">
</BraintreeAuthentication>
</BraintreeSection>
2.引入Braintree.dll
3.引入Braintree.js
4.配置Braintree證書
var gateway = new BraintreeGateway { Environment = Braintree.Environment.SANDBOX; //沙盒 MerchantId = config.MerchantId, PublicKey = config.PublicKey, PrivateKey = config.PrivateKey };
5. 作為客戶將客戶Id及信息保存在Braintree中。
private string getCustomerId() { string customerId = "customer_" + CurrentUserId; try { var customer = getGateway().Customer.Find(customerId); //查找該客戶的所有信息 if (customer != null) { return customer.Id; } } catch { //沒找到則添加 var model = UserDataService.GetUserInfo(CurrentUserId); var request = new CustomerRequest { Id = "customer_" + CurrentUserId, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Phone = model.CellPhone, Company = model.BusinessName }; Result<Customer> result = getGateway().Customer.Create(request); //創建客戶 if (result.IsSuccess()) { return result.Target.Id; } } return null; }
6. 獲得該客戶的所有信用卡信息。
var customer = getGateway().Customer.Find(customerId); if (customer.CreditCards != null)//信用卡部分 { return Json(new { result = true, data = customer.CreditCards.Select(c => new { Token = c.Token, MaskedNumber = c.MaskedNumber, CardType = c.CardType.ToString(), IsDefault = c.IsDefault, ImageUrl = c.ImageUrl }) }); //token支付用的憑證,MaskedNumber卡號,CardType 卡類型,IsDefault 默認卡,ImageUrl 圖片路徑 }
1.2信用卡驗證
1.Braintree介入方式(1。Dropin 2。custom) 前者使用現成的框架,后者自定義框架(意味着卡的驗證也要自己)
2.Dropin顯示方法:
<form id="CreditCards-form" method="post" action="/Braintree/AddPaymentMenthod"> <div id="payment-form"></div> <div id="show" style="display:none"> <input type="checkbox" id="default" name="default" checked="checked" value="true" /> Set As Default <div style="text-align:center"> <input type='submit' id="btnAddCreditCard" value='Add Credit Cards'/> </div> </div> </form>
默認只有卡號,年月日輸入框,自動根據卡號驗證卡的類型,CVV AVS 需要在控制面板開啟,設為默認是自己后加的。
PayPal按鈕需要線上關閉,沙盒無法關閉。
設置方法:



一些其他支付方式的支持,卡類型的支持均在此處設置。
調用braintree封裝好的方法顯示該UI。首先需要取得客戶端Token:
var clientToken = getGateway().ClientToken.generate();
得到客戶端令牌后就可以調用js方法展示該UI了
braintree.setup(data, "dropin", { container: "payment-form", form: "CreditCards-form", onReady: function () { $("#ts").hide(); $("#show").show(); } });
1.3:添加一張信用卡:
創建的ui自動會生成一個付款方式隨機數,該隨機數包含信用卡的卡號、卡類型、有效期以及我們設置的CVV。
payment_method_nonce
為客戶添加信用卡
var request = new PaymentMethodRequest { CustomerId = customerId, PaymentMethodNonce = payment_method_nonce, Options = new PaymentMethodOptionsRequest { VerifyCard = true, //卡驗證 FailOnDuplicatePaymentMethod = true //拒絕重復添加 } }; Result<PaymentMethod> result = getGateway().PaymentMethod.Create(request); if (result.IsSuccess()) { }
IsSuccess為成功狀態。添加信用卡時可以設置對卡片進行有效性驗證,以及不可以重復添加等。
1.4:刪除一張信用卡:
var result = getGateway().PaymentMethod.Delete(token); //token為信用卡支付以及刪除的唯一標識。
刪除信用卡后,自動將下一張設為默認卡。
1.5:設為默認:
var updateRequest = new PaymentMethodRequest { Options = new PaymentMethodOptionsRequest { MakeDefault = true, //默認 } }; Result<PaymentMethod> result = getGateway().PaymentMethod.Update(token, updateRequest); if (result.IsSuccess()) { return Json(new { result = true }); }
只需要設置MakeDefault為true即可。
2.1:支付方式新增信用卡支付:

調用Braintree的消費方法
var request = new TransactionRequest { Amount = amount, PaymentMethodToken = token, Options = new TransactionOptionsRequest { SubmitForSettlement = true } }; Result<Transaction> result = getGateway().Transaction.Sale(request); //支付 var payResult = PayStatus.WaitingForPayment; var message = ""; if (result.Target != null) { message = result.Target.ProcessorResponseText;//錯誤 Message } else if (result.Message != null) { message = result.Message; } if (result.IsSuccess()) { UpdatePaymentMenthod(token); payResult = PayStatus.PaymentSuccess; } else { payResult = PayStatus.PaymentFailed; }
如果支付成功,則修改當前方式為默認付款方式。
如果PayEnd異常則立即取消這筆信用卡消費,調用transaction的void方法。 稍后說明void方法與refund方法區別
Void方法調用時也需要Begin 中間void 結束 End 證明一筆交易。
Result<Transaction> result = getGateway().Transaction.Void(transactionId);
關於退款:
部分退款時調用refund方法,但是需要檢測信用卡狀態,如果信用卡狀態為:SUBMITTED_FOR_SETTLEMENT或AUTHORIZED只可以使用void方法取消整筆訂單,如果是SETTLED或SETTLING 則使用refund方法進行部分退款。
getGateway().Transaction.Refund(transactionId, amount); 如果不傳錢 則全部退款
一、BrainTree后台操作說明
3.1:控制面板管理Dashboard:
介紹了兩種集成方式,以及近期收入的統計。
3.2:交易信息管理Transactions:
按檢索條件檢索數據

Sale為消費,Submitted For Settlement 為剛提交的交易,Voided為已取消的交易。Credit為已退款的交易,Settled 為已完結的交易。
點擊交易號ID 可以對交易進行操作:Submitted For Settlement的則可以Void 取消交易操作,Settled則可以refund部分退款操作。
3.3:信用卡驗證管理Verifications:


添加信用卡的時候對信用卡的有效進行驗證,花1美金,看支付狀態。點擊可以查看該驗證的詳細信息。
3.4:保險庫管理Vault:



所有保存在Braintree中的客戶信息,以及該客戶的所有信用卡,所有的交易等。
3.5:訂閱服務管理Subscription:
訂閱服務
3.6:創建交易New Transaction:
創建一筆新的交易,可以指定某個客戶或 輸入卡進行付款。
3.7:交易統計 Transaction Summary:
對卡的類型進行消費退款等統計。
3.7: 支出匯總 Disbursement summary:
按月份對每種卡進行支出匯總
3.8: 爭議 Disputes:
已完結匯總
3.9: 創建新的客戶 New Customer:
創建新的客戶
3.10: 即將到期的卡 Expiring Cards:
即將過期的信用卡。
3.11: 導出客戶信息 Export Customers:
導出客戶資料,以及信用卡信息等。
3.12: 計划 Plans:
定期每月使用某卡付款的功能,我們沒開發這個功能。
3.13: 附加/折扣 add-ons / discounts:
附加費或打折設置,每次交易時的附加費,或者享受的折扣。
3.14: 郵件通知 Email Notifications:
定期郵件通知
四: 設置 Settings:
4.1: 處理選項 Processing:
這些設置可能無法立即生效,可能需要幾分鍾。
Duplicate Transaction Checking 重復交易檢查
Accept Paypal 使用PayPal
Accept Venmo 使用Venmo
Accept Apple Pay 使用Apple Pay
Accept Android Pay 使用Android Pay
Venmo Touch 讓你的賬戶支持其他商家中的信用卡
AVS 開啟AVS驗證
cvv 開啟CVV驗證
Risk ThreSholds 風險控制
Advanced Credit Fraud Tools 信用卡詐騙工具
Card Verification 信用卡驗證 開啟后會對信用卡進行有效性驗證,支付0~1之間的美金,如果通過則會存儲在保險庫中,否則不會保存。
Card Verification – Retry All Failed $0 重試所有驗證失敗的
Display Additional Processor Response 當處理器拒絕時顯示拒絕原因, 無論是否設置總是顯示拒絕原因。
Dispute Notifications 爭議通知 產生爭議時會發送郵件到一個郵箱,一批通知每天一次
Transaction lssue Notifications 交易問題的通知郵箱
Custom Fields 自定義字段
Email Receipts Email收據
Recurring Billing 周期性結算
4.2: 安全選項 Security Options:
限制ip訪問
4.3: 用戶權限 User and Roles:
可以添加郵箱 權限
