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];
}
}
このメソッドを呼び出すと、次のように機能します。
Result<Transaction> result = paymentBL.ChargeCardOnce(
2.34m
, formCollection["name"]
, formCollection["number"]
, formCollection["exp"]
, formCollection["cvv"]
);
その後、Braintreeダッシュボードで完了したテストトランザクションを表示できます。したがって、Braintree.jsからの暗号化されたフォーム値がコントローラーのアクションに正しく到達し、キーとマーチャントアカウントIDがすべて正しく設定されていることがわかります。
上記のChargeCardOnceの呼び出しを、以下のStoreCustomerの呼び出しに置き換えると、次の行で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のサポートから:「サンドボックスは本番環境がどのように見えるかを正確に反映するように構築されているため、サンドボックスに顧客とクレジットカードを作成できます。」
誰かがこれも経験したことがありますか?私はBraintreeのサポートをこの質問に言及していますが、SOの誰かがこれを見て、解決策または回避策を知っていれば、私は大いに安心します。