2013-03-27 82 views
3

我正在使用ASP.NET MVC 4,.NET Braintree Payments API和Braintree.js。Braintree API在Sandbox的CreateCard.Create中拋出Braintree.Exceptions.AuthorizationException

下面是一個簡單的包裝我建立布倫特裏:

public class PaymentBL 
{ 
    private static BraintreeGateway _braintreeGateway = new BraintreeGateway 
     { 
      Environment = Braintree.Environment.SANDBOX, 
      MerchantId = "xxxxxxx", 
      PublicKey = "xxxxxxxxxxxx", 
      PrivateKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
     }; 

    public Result<Transaction> ChargeCardOnce(decimal amount, string cardholderName, string cardNumber, string expiration, 
     string cvv) 
    { 
     TransactionCreditCardRequest creditCardRequest = new TransactionCreditCardRequest(); 
     creditCardRequest.CardholderName = cardholderName; 
     creditCardRequest.Number = cardNumber; 
     creditCardRequest.ExpirationDate = expiration; 
     creditCardRequest.CVV = cvv; 

     TransactionOptionsRequest optionsRequest = new TransactionOptionsRequest(); 
     optionsRequest.SubmitForSettlement = true; 

     TransactionRequest transactionRequest = new TransactionRequest(); 
     transactionRequest.Amount = amount; 
     transactionRequest.CreditCard = creditCardRequest; 
     transactionRequest.Options = optionsRequest; 

     return _braintreeGateway.Transaction.Sale(transactionRequest); 
    } 

    /// <summary> 
    /// Stores a credit card in the Braintree vault. In some cases, will put a $1 temporary charge 
    /// on the credit card that will come off a few days later. 
    /// 
    /// From BrainTree: Regardless of card type, any instance where a $1 authorization returns a successful result, 
    /// we immediately follow-up with an automatic void request to ensure that the transaction will fall off 
    /// of the cardholder's statement as soon as possible. 
    /// </summary> 
    public Result<CreditCard> StoreCustomer(int customerId, string cardholderName, string cardNumber, string expiration, string cvv) 
    { 
     //CreditCardAddressRequest addressRequest = new CreditCardAddressRequest(); 
     //addressRequest.PostalCode = postalCode; 

     CreditCardOptionsRequest optionsRequest = new CreditCardOptionsRequest(); 
     optionsRequest.VerifyCard = true; 
     optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId; 

     CreditCardRequest creditCard = new CreditCardRequest(); 
     creditCard.CustomerId = customerId.ToString(); 
     creditCard.Token = customerId.ToString(); // Use same token to ensure overwrite 
     creditCard.CardholderName = cardholderName; 
     creditCard.Number = cardNumber; 
     creditCard.ExpirationDate = expiration; 
     creditCard.CVV = cvv; 
     creditCard.Options = optionsRequest; 

     return _braintreeGateway.CreditCard.Create(creditCard); 
    } 

    /// <summary> 
    /// Search BrainTree vault to retrieve credit card 
    /// </summary> 
    /// <param name="customerId"></param> 
    public CreditCard GetCreditCardOnFile(int customerId) 
    { 
     Customer customer = null; 

     try 
     { 
      customer = _braintreeGateway.Customer.Find(customerId.ToString()); 
     } 
     catch (Braintree.Exceptions.NotFoundException) 
     { 
      return null; 
     } 

     if (customer.CreditCards == null || customer.CreditCards.Length == 0) 
     { 
      return null; 
     } 

     if (customer.CreditCards.Length >= 2) 
     { 
      throw new Exception(string.Format("Customer {0} has {1} credit cards", 
       customerId, customer.CreditCards.Length)); 
     } 

     return customer.CreditCards[0]; 
    } 
} 

當我調用這個方法,它的工作原理:

  Result<Transaction> result = paymentBL.ChargeCardOnce(
       2.34m 
       , formCollection["name"] 
       , formCollection["number"] 
       , formCollection["exp"] 
       , formCollection["cvv"] 
      ); 

隨後,我可以在布倫特裏查看已完成測試交易儀表板。因此,我知道Braintree.js的加密表單值正確地到達我的控制器操作,並且我的密鑰和商家帳戶ID都已正確設置。

當我更換上面的調用與以下調用StoreCustomer到ChargeCardOnce,我收到一個Braintree.Exceptions.AuthorizationException在該行return _braintreeGateway.CreditCard.Create(creditCard);

  Result<CreditCard> result = paymentBL.StoreCustomer(
       systemHost.Customer.CustomerId 
       , formCollection["name"] 
       , formCollection["number"] 
       , formCollection["exp"] 
       , formCollection["cvv"] 
      ); 

從布倫特裏支持:「你能創建一個客戶以及沙盒中的信用卡,因爲它的製作完全反映了生產環境的外觀。「

有沒有人體驗過這個呢?我指的是Braintree對這個問題的支持,但是如果SO上的任何人已經看到了這一點,並且知道解決方案或解決方法,我會放心的。

回答

13

我在布倫特裏工作。看起來我們已經回答了你的問題的答案,但我會在這裏回答任何有未來相同問題的人。

在這種情況下,問題是:

optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId; 

商家ID標識您的支付網關帳戶,而您的商家帳戶ID標識要用於驗證的銀行帳戶。 An article in our support center explains the difference

MerchantAccountId

隨着布倫特裏,你可以有多個商戶 通過同一個網關帳戶佔所有處理。這 意味着你可以有多個地點,多個業務,多種貨幣等所有設置和處理在一個單一的 帳戶。通過統一報告和訪問,您可以輕鬆跟蹤所有的處理過程,甚至可以爲您節省開支。

您可以通過以下步驟找到所有商家帳戶的ID在你的網關 帳戶:在您的帳戶名

  • 登錄到您的帳戶

  • 懸停,然後單擊「處理」

  • 滾動至頁面底部至 標爲「商家帳戶」的部分。

AuthorizationExceptionHTTP Status Code 403 Forbidden。這意味着服務器正在拒絕您的請求,因爲您無權訪問資源(即使您可能已通過身份驗證)。

由於沒有商戶帳戶可供您的用戶使用您指定的ID(因爲它根本不是商家帳戶ID),您可以獲得AuthorizationException

如果您的商家經常只有一個商家帳戶,或者您想使用默認帳戶,則無需指定VerificationMerchantAccountId

+0

感謝您的回答和澄清,它已經解決了這個問題。 – 2013-03-29 16:48:44

相關問題