Last active
June 7, 2019 19:11
-
-
Save valmont/f23cd23a491a63146be353e47cde7f76 to your computer and use it in GitHub Desktop.
This file contains 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
private CreditCardTransaction ProcessCustomerPaymentProfileTransaction(OrderContext context, transactionTypeEnum transactionType) { | |
//============================================ | |
//New Transaction Method by Paul -- 02/28/2018 | |
//============================================ | |
var creditCardInformation = (CreditCardInformation) context.BillingInformation; | |
//Create or update the AuthNet Customer Profile and Payment Profile | |
EnsurePaymentProfile(context, creditCardInformation); | |
//Authenticate with Authorize.net | |
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = | |
HttpContext.Current.Request.IsLocal ? Environment.SANDBOX : Environment.PRODUCTION; | |
//Define the merchant information (authentication / transaction id) | |
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType { | |
name = AuthNetLoginId, | |
ItemElementName = ItemChoiceType.transactionKey, | |
Item = AuthNetTransactionKey | |
}; | |
//Create a customer payment profile | |
var profileToCharge = | |
new customerProfilePaymentType { | |
customerProfileId = context.Customer.CIM_CustomerProfileID.ToString(), | |
paymentProfile = new paymentProfile { | |
paymentProfileId = context.Customer.CIM_CustomerPaymentProfileID.ToString() | |
}, | |
shippingProfileId = context.Customer.CIM_CustomerShippingAddressId.ToString() | |
}; | |
var tax = new extendedAmountType { | |
amount = Math.Round(context.SalesTax, 2, MidpointRounding.AwayFromZero), | |
name = "Sales Tax", | |
description = "" | |
}; | |
var shipping = new extendedAmountType { | |
amount = Math.Round(context.ShippingCost, 2, MidpointRounding.AwayFromZero), | |
name = "Shipping", | |
description = context.ShippingService.Name | |
}; | |
//Crate the transaction | |
var transactionRequest = new transactionRequestType { | |
transactionType = transactionType.ToString(), | |
amount = Math.Round(context.GrandTotal, 2, MidpointRounding.AwayFromZero), | |
profile = profileToCharge, | |
tax = tax, | |
shipping = shipping, | |
order = new orderExType { | |
invoiceNumber = context.OrderID.ToString() | |
}, | |
customerIP = context.ClientIp | |
}; | |
var request = new createTransactionRequest {transactionRequest = transactionRequest}; | |
// instantiate the collector that will call the service | |
var controller = new createTransactionController(request); | |
controller.Execute(); | |
//Use the old transaction result object so all | |
//calling code still behaves as expected | |
var result = new CreditCardTransaction(); | |
// get the response from the service (errors contained if any) | |
var response = controller.GetApiResponse(); | |
// validate response | |
if (response == null) { | |
var errorResponse = controller.GetErrorResponse(); | |
result.Successful = false; | |
result.ResponseText = "Unknown error processing transaction. Your card has not been charged. Please call 888.313.6313 to complete this transaction."; | |
result.ResponseCode = errorResponse.messages.message.Any() | |
? string.Join(",", errorResponse.messages.message.Select(x => x.code).ToArray()) | |
: "FS500"; | |
return result; | |
} | |
//Successful transaction | |
if (response.messages.resultCode == messageTypeEnum.Ok && response.transactionResponse.messages != null) { | |
result.Successful = true; | |
result.ExternalTransactionID = response.transactionResponse.transId; | |
result.AuthorizationCode = response.transactionResponse.authCode; | |
result.ResponseCode = response.transactionResponse.responseCode; | |
result.ResponseText = response.transactionResponse.messages[0].description; | |
return result; | |
} | |
//Unsuccessful transaction | |
result.Successful = false; | |
result.AuthorizationCode = ""; | |
result.ResponseCode = ""; | |
result.ResponseText = ""; | |
result.ExternalTransactionID = response.transactionResponse.transId ?? ""; | |
if (response.transactionResponse?.errors != null) { | |
foreach (var error in response.transactionResponse.errors) { | |
result.ResponseCode += $" > {error.errorCode}"; | |
result.ResponseText += $" > {error.errorText}"; | |
} | |
} | |
else { | |
foreach (var error in response.messages.message) { | |
result.ResponseCode += $" > {error.code}"; | |
result.ResponseText += $" > {error.text}"; | |
} | |
} | |
Logger.Log($"Authorize.net ERROR --- {result.ResponseCode} --- {result.ResponseText}"); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment