加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Braintree API在Sandbox中的CreateCard.Create上抛出Brain

发布时间:2020-12-16 00:22:38 所属栏目:百科 来源:网络整理
导读:我正在使用ASP.NET MVC 4,.NET Braintree Payments API和Braintree.js. 这是我为Braintree构建的一个简单的包装器: public class PaymentBL{ private static BraintreeGateway _braintreeGateway = new BraintreeGateway { Environment = Braintree.Environ
我正在使用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 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仪表板中查看已完成的测试事务.因此,我知道Braintree.js的加密表单值正确地到达我的控制器操作,并且我的密钥和商家帐户ID都已正确设置.

当我使用以下对StoreCustomer的调用将上述调用替换为ChargeCardOnce时,我在返回_braintreeGateway.CreditCard.Create(creditCard)行返回Braintree.Exceptions.AuthorizationException;

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

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

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

解决方法

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

在这种情况下,问题是:

optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId;

您的商家ID标识了您的付款网关帐户,而您的商家帐户ID标识了您要用于验证的银行帐户. An article in our support center explains the difference:

MerchantAccountId

With Braintree,you can have multiple merchant
accounts all processing via the same gateway account. This
means that you can have multiple locations,multiple businesses,
multiple currencies,etc. all setup and processing under a single
account. This makes it easy to keep track of all of your processing
via unified reporting and access and can even save you money.

You can find the IDs for all merchant accounts in your gateway
account by following these steps:

  • login to your account

  • hover over your account name
    and click “Processing”

  • scroll to the bottom of the page to the
    section labeled “Merchant Accounts”.

AuthorizationException是HTTP Status Code 403 Forbidden.这意味着服务器正在拒绝您的请求,因为您没有访问资源的权限(即使您可能已经过身份验证).

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读