'Is it possible to make 3D Secure payments through a Braintree sandbox environment with C#?
I have written code in .NET Standard 2.0 to make payments through Braintree. The code uses the Braintree 5.2.0 NuGet package. I intend to exclusively make 3D Secure payments when the code is used against a Braintree production account. I have written a integration test that creates a customer, creates a payment method for that customer, then makes a payment against that payment method using the Token that was generated.
The code to create a customer is:
public async Task<string> SeedCustomer(IBraintreeConfiguration braintreeConfiguration)
{
BraintreeGateway _braintreeGateway = new BraintreeGateway(
braintreeConfiguration.Environment,
braintreeConfiguration.MerchantId,
braintreeConfiguration.PublicKey,
braintreeConfiguration.PrivateKey
);
CustomerRequest request = new CustomerRequest
{
Email = BraintreeTestConstants.Email,
CustomFields = new Dictionary<string, string>
{
{"account_id", BraintreeTestConstants.AccountId}
}
};
string braintreeCustomerId =
(await _braintreeGateway.Customer.CreateAsync(request)).Target.Id;
return braintreeCustomerId;
}
The code to create a payment method is:
PaymentMethodRequest paymentMethodRequest = new PaymentMethodRequest
{
PaymentMethodNonce = nonce,
CustomerId = customerId,
BillingAddress = new PaymentMethodAddressRequest
{
FirstName = BraintreeTestConstants.BillingName,
Locality = BraintreeTestConstants.City,
Company = BraintreeTestConstants.CompanyName,
CountryCodeAlpha2 = BraintreeTestConstants.Country,
ExtendedAddress = BraintreeTestConstants.ExtendedAddress,
Region = BraintreeTestConstants.State,
StreetAddress = BraintreeTestConstants.StreetAddress,
PostalCode = BraintreeTestConstants.Zip,
},
Options = new PaymentMethodOptionsRequest
{
VerifyCard = true
}
};
Result<PaymentMethod> result =
await _braintreeGateway.PaymentMethod.CreateAsync(paymentMethodRequest);
return _mapper.Map<AddPaymentMethodResultModel>((CreditCard)result.Target);
CreditCard.Token is mapped to AddPaymentMethodResultModel.CardId.
The code to make a payment is:
bool useThreeDSecure = true;
TransactionRequest transactionRequest = new TransactionRequest
{
Amount = Amount,
PaymentMethodToken = CardId,
MerchantAccountId = string.IsNullOrWhiteSpace(MerchantAccountId) ? null : MerchantAccountId,
TransactionSource = string.IsNullOrWhiteSpace(TransactionSource) ? null : TransactionSource,
Options = new TransactionOptionsRequest
{
SubmitForSettlement = true,
ThreeDSecure = new TransactionOptionsThreeDSecureRequest
{
Required = useThreeDSecure
}
}
};
if (Address != null)
{
transactionRequest.BillingAddress = new AddressRequest
{
Company = Address.CompanyName,
FirstName = Address.BillingName,
StreetAddress = Address.StreetAddress,
ExtendedAddress = Address.ExtendedAddress,
Locality = Address.City,
Region = Address.State,
PostalCode = Address.Zip,
CountryCodeAlpha2 = Address.Country
};
}
Result<Transaction> result =
await _braintreeGateway.Transaction.SaleAsync(transactionRequest);
When I execute the test against a Braintree sandbox environment with TransactionRequest.Options.ThreeDSecure.Required set to false, a payment is successful.
When I execute the test against a Braintree sandbox environment TransactionRequest.Options.ThreeDSecure.Required set to true, a payment fails with a Result.Message of Gateway Rejected: three_d_secure.
I was wondering whether it is possible to make a successful payment through a Braintree sandbox environment with TransactionRequest.Options.ThreeDSecure.Required set to true. I have tried unsuccessfully using a payment method created through the NuGet package with a PaymentMethodNonce of fake-three-d-secure-visa-full-authentication-nonce. I have also tried unsuccessfully using a payment method created through the Braintree sandbox dashboard using details of a test Visa card from https://support.bluesnap.com/docs/test-credit-card-numbers.
I would like to prove that a 3D Secure payment can be made through a Braintree sandbox environment to have confidence that the code will work against a Braintree production environment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
