Braintree API在Sandbox中的CreateCard.Create上抛出Braintree.Exceptions.AuthorizationException

J.T*_*lor 4 .net c# braintree

我正在使用ASP.NET MVC 4,.NET Braintree Payments API和Braintree.js.

这是我为Braintree构建的一个简单的包装器:

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];
    }
}
Run Code Online (Sandbox Code Playgroud)

当我调用这个方法时,它可以工作:

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

随后,我可以在Braintree仪表板中查看已完成的测试事务.因此,我知道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"]
            );
Run Code Online (Sandbox Code Playgroud)

来自Braintree的支持:"您可以在沙箱中创建客户和信用卡,因为它的构建完全反映了生产环境的样子."

有没有人经历过这个?我指的是Braintree对这个问题的支持,但是如果SO上的任何人看到了这个问题并且知道解决方案或解决方法,我会很放心.

agf*_*agf 16

我在布伦特里工作.看起来我们已经回复了你的问题的答案,但我会在这里回答那些在将来遇到同样问题的人.

在这种情况下,问题是:

optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId;
Run Code Online (Sandbox Code Playgroud)

您的商家ID标识了您的付款网关帐户,而您的商家帐户ID标识了您要用于验证的银行帐户.我们支持中心的一篇文章解释了不同之处:

MerchantAccountId

使用Braintree,您可以通过同一个网关帐户处理多个商家帐户.这意味着您可以在一个帐户下拥有多个地点,多个商家,多种货币等所有设置和处理.这样可以通过统一的报告和访问轻松跟踪所有处理,甚至可以节省资金.

您可以按照以下步骤在网关帐户中找到所有商家帐户的ID:

  • 登录到您的帐户

  • 将鼠标悬停在您的帐户名称上,然后点击"处理"

  • 滚动到页面底部的"商家帐户"部分.

An AuthorizationExceptionHTTP状态码403禁止.这意味着服务器正在拒绝您的请求,因为您没有访问资源的权限(即使您可能已经过身份验证).

由于您的用户没有可以使用您指定的ID的商家帐户(因为它根本不是商家帐户ID),因此您可以获得AuthorizationException.

如果通常情况下,您的商家只有一个商家帐户,或者您想使用默认帐户,则无需指定VerificationMerchantAccountId.