Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

.net - In Braintree Net How do I generate a PaymentNonce

The company that I work for has a legacy WinForms application used by Sales and Customer Service to help customers create orders as opposed to the customer creating an order on the website. We are wanting to convert our CC Payment gateway to Braintree, but I cannot figure out how to create a Payment Nonce so that I can create a payment method with the CC information. Has anyone successfully integrated the Braintree SDK into a WinForms project?

I attempted to create a transaction request but without the PaymentNonce. I used just the customer Id which used the default payment method for that customer. That works great if

  • There is a customer
  • There is a default payment method

However, if I want to create a transaction for a customer that doesn't exist or the customer does not have a payment method for the credit card the user has provided, then I am stumped. I don't know how to provide the CC info and in what object to pass the card data (number, exp. date, CVV) the Braintree.CreditCard object is inaccessible due to its protection level, so that is not how to pass it. ???♂?

below is the transaction where I tried to pass the credit card information:

  transaction = new TransactionRequest()
            {
                Amount = paymentData.Amount,
                CustomerId = customerId,
                DiscountAmount = paymentData.CouponDiscount + paymentData.DiscAmount,
                ShippingAmount = paymentData.ShipCharge,
                LineItems = lineItems.ToArray(),
                TaxAmount = paymentData.Taxes,
                TaxExempt = paymentData.Taxes == 0 ? true : false,
                BillingAddress = new AddressRequest()
                {
                    Company = address.Company,
                    FirstName = paymentData.FirstName,
                    LastName = paymentData.LastName,
                    Locality = address.City,
                    Region = address.State,
                    PostalCode = address.Zip,
                    StreetAddress = address.Address1,
                    ExtendedAddress = address.Address2,
                    CountryCodeAlpha2 = address.CountryCode
                },
                Options = new TransactionOptionsRequest()
                {
                    SubmitForSettlement = paymentData.TransType == "A" ? false : true
                },
                Braintree.CreditCard = new Braintree.CreditCard()
                {
                    CardholderName = paymentData.AccountName,
                    Cvv = paymentData.CVV,
                    ExpirationMonth = paymentData.ExpMonth,
                    ExpirationYear = paymentData.ExpYear,
                    UniqueNumberIdentifier = paymentData.AccountNumber
                },
            };

            var customFields = new Dictionary<string, string>();
            customFields.Add("cartid", paymentData.IsCart ? paymentData.Id.ToString() : "0");
            customFields.Add("orderid", paymentData.IsCart ? "0" : paymentData.Id.ToString());
            customFields.Add("userid", paymentData.CustId);
            transaction.CustomFields = customFields;
  

            Result<Braintree.Transaction> result = GetGateway().Transaction.Sale(transaction);
question from:https://stackoverflow.com/questions/65851748/in-braintree-net-how-do-i-generate-a-paymentnonce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To create a credit card transaction using the Braintree SDK without using DropIn or hosted fields you need to create the credit card object then the transaction.

//create the credit card
                var creditCardRequest = new CreditCardRequest
                {
                    CustomerId = customerId,
                    Number = paymentData.AccountNumber,
                    CardholderName = paymentData.AccountName,
                    ExpirationMonth = paymentData.ExpMonth.ToString("d2"),
                    ExpirationYear = paymentData.ExpYear.ToString(),
                    CVV = paymentData.CVV,
                    Options = new CreditCardOptionsRequest()
                    {
                        FailOnDuplicatePaymentMethod = false,
                        VerifyCard = true
                    },
                };
                Result<Braintree.CreditCard> result = GetGateway().CreditCard.Create(creditCardRequest);
                if (result.IsSuccess())
                    creditCard = result.Target;




 transaction = new TransactionRequest()
                {
                    Amount = paymentData.Amount,
                    CustomerId = customerId,
                    PaymentMethodToken = creditCard.Token,
                    DiscountAmount = paymentData.CouponDiscount + paymentData.DiscAmount,
                    ShippingAmount = paymentData.ShipCharge,
                    LineItems = lineItems.ToArray(),
                    TaxAmount = paymentData.Taxes,
                    TaxExempt = paymentData.Taxes == 0 ? true : false,
                    BillingAddress = new AddressRequest()
                    {
                        Company = address.Company,
                        FirstName = paymentData.FirstName,
                        LastName = paymentData.LastName,
                        Locality = address.City,
                        Region = address.State,
                        PostalCode = address.Zip,
                        StreetAddress = address.Address1,
                        ExtendedAddress = address.Address2,
                        CountryCodeAlpha2 = address.CountryCode
                    },
                    Options = new TransactionOptionsRequest()
                    {
                        SubmitForSettlement = paymentData.TransType == "A" ? false : true
                    },
                    TransactionSource = "moto"
                };

                var customFields = new Dictionary<string, string>();
                customFields.Add("cartid", paymentData.IsCart ? paymentData.Id.ToString() : "0");
                customFields.Add("orderid", paymentData.IsCart ? "0" : paymentData.Id.ToString());
                customFields.Add("userid", paymentData.CustId);
                transaction.CustomFields = customFields;
      

                Result<Braintree.Transaction> result = GetGateway().Transaction.Sale(transaction);
                if (result.IsSuccess())
                { //do something here}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...