Last active
January 20, 2017 09:15
-
-
Save sjwaight/d537c05c39a898afede9cdb0c82fa710 to your computer and use it in GitHub Desktop.
Azure Function main entry point showing how to do payments using Braintree's v.zero API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Braintree; | |
| public static void Run(string input, TraceWriter log) | |
| { | |
| var braintreeEnvironment = "sandbox"; // alternative = production | |
| // These three config items should be stored in Azure KeyVault | |
| var merchantId = "5xxxxxxxxxxxxxx7"; | |
| var merchPubKey = "tyyyyyyyyyyyyyyh"; | |
| var merchPrivateKey = "4oooooooooooooooooooooooooooooo4"; | |
| // This is a payment nonce or customer ID as supplied by | |
| // the Braintree. | |
| var paymentMethodToken = "9aaaaa"; | |
| var gateway = new BraintreeGateway(braintreeEnvironment, | |
| merchantId, | |
| merchPubKey, | |
| merchPrivateKey); | |
| // Note submitting same amount for same payment token | |
| // within 1 minute will result in rejected payment | |
| // unless you turn off duplicate detection in Braintree | |
| var btRequest = new TransactionRequest() | |
| { | |
| Amount = 10M, | |
| PaymentMethodToken = paymentMethodToken, | |
| Options = new TransactionOptionsRequest | |
| { | |
| SubmitForSettlement = true | |
| } | |
| }; | |
| Transaction transaction = null; | |
| var btResponse = gateway.Transaction.Sale(btRequest); | |
| if (btResponse.IsSuccess()) | |
| { | |
| // SUCCESS | |
| transaction = btResponse.Target; | |
| log.Info($"Success!"); | |
| log.Info($"Auth code: {transaction.ProcessorAuthorizationCode}"); | |
| // Do some processing, save somewhere | |
| } | |
| else | |
| { | |
| // FAILURE | |
| log.Info($"Failure!"); | |
| log.Info(btResponse.Message); | |
| // btResponse at this point has no 'Target' property | |
| transaction = btResponse.Transaction; | |
| // Do some processing, save somewhere | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment