protected static Cybersource.TransactionProcessorClient GetCybersourceService(string serviceEndpoint, string merchantId, string transactionKey) { var binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential; var address = new EndpointAddress(serviceEndpoint); var Service = new Cybersource.TransactionProcessorClient(binding, address); Service.ChannelFactory.Credentials.UserName.UserName = merchantId; Service.ChannelFactory.Credentials.UserName.Password = transactionKey; return Service; }
public override void ProcessTransaction(Transaction t) { bool result = false; try { string url = LiveUrl; if (Settings.TestMode) { url = TestUrl; } // Create Request Cybersource.RequestMessage req = new Cybersource.RequestMessage(); // Set MerchantId and Ref Code req.merchantID = Settings.MerchantId; if (t.Id.ToString().Length < 0) { req.merchantReferenceCode = Text.TrimToLength(System.Guid.NewGuid().ToString(), 50); t.AdditionalSettings.Add("CybersourceRefCode", req.merchantReferenceCode); } else { req.merchantReferenceCode = Text.TrimToLength(t.Id.ToString(), 50); } // for debugging on Cybersource side req.clientLibrary = ".NET WCF"; req.clientLibraryVersion = Environment.Version.ToString(); req.clientEnvironment = Environment.OSVersion.Platform + Environment.OSVersion.Version.ToString(); req.clientApplication = "MerchantTribe"; req.clientApplicationVersion = "1.3.0"; // Currency Code req.purchaseTotals = new Cybersource.PurchaseTotals(); req.purchaseTotals.currency = Settings.CurrencyCode; if (req.purchaseTotals.currency.Trim().Length < 3) { req.purchaseTotals.currency = "USD"; } bool includeBillTo = false; bool includeCardData = false; bool includeMerchantExtraData = false; // Set Action switch (t.Action) { case ActionType.CreditCardCharge: req.ccAuthService = new Cybersource.CCAuthService(); req.ccAuthService.run = "true"; req.ccCaptureService = new Cybersource.CCCaptureService(); req.ccCaptureService.run = "true"; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardHold: // Authorize req.ccAuthService = new Cybersource.CCAuthService(); req.ccAuthService.run = "true"; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardCapture: // Capture, Post Authorize req.ccCaptureService = new Cybersource.CCCaptureService(); req.ccCaptureService.run = "true"; req.ccCaptureService.authRequestID = t.PreviousTransactionNumber; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardVoid: // Void req.voidService = new Cybersource.VoidService(); req.voidService.run = "true"; req.voidService.voidRequestID = t.PreviousTransactionNumber; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardRefund: // Refund, Credit req.ccCreditService = new Cybersource.CCCreditService(); req.ccCreditService.run = "true"; if (t.PreviousTransactionNumber.Length > 0) { // Previous Transaction Credit req.ccCreditService.captureRequestID = t.PreviousTransactionNumber; } else { // Stand Alone Credit includeCardData = true; } includeBillTo = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; } if (includeBillTo) { // Bill To Address req.billTo = new Cybersource.BillTo(); req.billTo.city = Text.TrimToLength(t.Customer.City, 50); req.billTo.company = Text.TrimToLength(t.Customer.Company, 40); var country = MerchantTribe.Web.Geography.Country.FindByName(t.Customer.Country); if (country != null) { req.billTo.country = country.IsoCode; } else { req.billTo.country = "US"; } req.billTo.customerID = Text.TrimToLength(t.Customer.Email, 50); req.billTo.email = Text.TrimToLength(t.Customer.Email, 255); req.billTo.firstName = Text.TrimToLength(t.Customer.FirstName, 60); req.billTo.lastName = Text.TrimToLength(t.Customer.LastName, 60); req.billTo.ipAddress = t.Customer.IpAddress; req.billTo.phoneNumber = Text.TrimToLength(t.Customer.Phone, 15); req.billTo.postalCode = Text.TrimToLength(t.Customer.PostalCode, 10); req.billTo.state = t.Customer.Region; req.billTo.street1 = Text.TrimToLength(t.Customer.Street, 60); } if (includeCardData) { req.card = new Cybersource.Card(); req.card.accountNumber = t.Card.CardNumber; switch (MerchantTribe.Payment.CardValidator.GetCardTypeFromNumber(t.Card.CardNumber)) { case CardType.Visa: req.card.cardType = "001"; break; case CardType.MasterCard: req.card.cardType = "002"; break; case CardType.Amex: req.card.cardType = "003"; break; case CardType.Discover: req.card.cardType = "004"; break; case CardType.DinersClub: req.card.cardType = "005"; break; case CardType.JCB: req.card.cardType = "007"; break; default: req.card.cardType = ""; break; } req.card.expirationMonth = t.Card.ExpirationMonthPadded; req.card.expirationYear = t.Card.ExpirationYear.ToString(); if (t.Card.SecurityCode.Length > 0) { req.card.cvNumber = t.Card.SecurityCode; } } if (includeMerchantExtraData) { req.merchantDefinedData = new Cybersource.MerchantDefinedData(); req.merchantDefinedData.field1 = t.MerchantInvoiceNumber; } var proc = new Cybersource.TransactionProcessorClient(); proc.ChannelFactory.Credentials.UserName.UserName = req.merchantID; proc.ChannelFactory.Credentials.UserName.Password = Settings.TransactionKey; var reply = proc.runTransaction(req); t.Result.ReferenceNumber = reply.requestID; t.Result.ResponseCode = reply.reasonCode; t.Result.ResponseCodeDescription = ParseReasonCode(reply.reasonCode); switch (reply.decision.ToUpperInvariant()) { case "ACCEPT": t.Result.Succeeded = true; result = true; break; case "ERROR": t.Result.Succeeded = false; result = false; break; case "REJECT": t.Result.Succeeded = false; result = false; break; } if (reply.ccAuthReply != null) { t.Result.AvsCode = ParseAvsCode(reply.ccAuthReply.avsCode ?? string.Empty); t.Result.CvvCode = ParseSecurityCode(reply.ccAuthReply.cvCode ?? string.Empty); } } catch (Exception ex) { result = false; t.Result.Messages.Add(new Message("Unknown Payment Error: " + ex.Message, "BVP_CYBERSOURCE_1001", MessageType.Error)); t.Result.Messages.Add(new Message("Stack Trace " + ex.StackTrace, "STACKTRACE", MessageType.Error)); } t.Result.Succeeded = result; }
public override void ProcessTransaction(Transaction t) { bool result = false; try { string url = LiveUrl; if (Settings.TestMode) { url = TestUrl; } // Create Request Cybersource.RequestMessage req = new Cybersource.RequestMessage(); // Set MerchantId and Ref Code req.merchantID = Settings.MerchantId; if (t.Id.ToString().Length < 0) { req.merchantReferenceCode = Text.TrimToLength(System.Guid.NewGuid().ToString(), 50); t.AdditionalSettings.Add("CybersourceRefCode", req.merchantReferenceCode); } else { req.merchantReferenceCode = Text.TrimToLength(t.Id.ToString(), 50); } // for debugging on Cybersource side req.clientLibrary = ".NET WCF"; req.clientLibraryVersion = Environment.Version.ToString(); req.clientEnvironment = Environment.OSVersion.Platform + Environment.OSVersion.Version.ToString(); req.clientApplication = "MerchantTribe"; req.clientApplicationVersion = "1.3.0"; // Currency Code req.purchaseTotals = new Cybersource.PurchaseTotals(); req.purchaseTotals.currency = Settings.CurrencyCode; if (req.purchaseTotals.currency.Trim().Length < 3) { req.purchaseTotals.currency = "USD"; } bool includeBillTo = false; bool includeCardData = false; bool includeMerchantExtraData = false; // Set Action switch (t.Action) { case ActionType.CreditCardCharge: req.ccAuthService = new Cybersource.CCAuthService(); req.ccAuthService.run = "true"; req.ccCaptureService = new Cybersource.CCCaptureService(); req.ccCaptureService.run = "true"; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardHold: // Authorize req.ccAuthService = new Cybersource.CCAuthService(); req.ccAuthService.run = "true"; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardCapture: // Capture, Post Authorize req.ccCaptureService = new Cybersource.CCCaptureService(); req.ccCaptureService.run = "true"; req.ccCaptureService.authRequestID = t.PreviousTransactionNumber; includeBillTo = true; includeCardData = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardVoid: // Void req.voidService = new Cybersource.VoidService(); req.voidService.run = "true"; req.voidService.voidRequestID = t.PreviousTransactionNumber; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; case ActionType.CreditCardRefund: // Refund, Credit req.ccCreditService = new Cybersource.CCCreditService(); req.ccCreditService.run = "true"; if (t.PreviousTransactionNumber.Length > 0) { // Previous Transaction Credit req.ccCreditService.captureRequestID = t.PreviousTransactionNumber; } else { // Stand Alone Credit includeCardData = true; } includeBillTo = true; includeMerchantExtraData = true; req.purchaseTotals.grandTotalAmount = t.Amount.ToString(); break; } if (includeBillTo) { // Bill To Address req.billTo = new Cybersource.BillTo(); req.billTo.city = Text.TrimToLength(t.Customer.City, 50); req.billTo.company = Text.TrimToLength(t.Customer.Company, 40); var country = MerchantTribe.Web.Geography.Country.FindByName(t.Customer.Country); if (country != null) { req.billTo.country = country.IsoCode; } else { req.billTo.country = "US"; } req.billTo.customerID = Text.TrimToLength(t.Customer.Email, 50); req.billTo.email = Text.TrimToLength(t.Customer.Email, 255); req.billTo.firstName = Text.TrimToLength(t.Customer.FirstName, 60); req.billTo.lastName = Text.TrimToLength(t.Customer.LastName, 60); req.billTo.ipAddress = t.Customer.IpAddress; req.billTo.phoneNumber = Text.TrimToLength(t.Customer.Phone, 15); req.billTo.postalCode = Text.TrimToLength(t.Customer.PostalCode, 10); req.billTo.state = t.Customer.Region; req.billTo.street1 = Text.TrimToLength(t.Customer.Street, 60); } if (includeCardData) { req.card = new Cybersource.Card(); req.card.accountNumber = t.Card.CardNumber; switch (MerchantTribe.Payment.CardValidator.GetCardTypeFromNumber(t.Card.CardNumber)) { case CardType.Visa: req.card.cardType = "001"; break; case CardType.MasterCard: req.card.cardType = "002"; break; case CardType.Amex: req.card.cardType = "003"; break; case CardType.Discover: req.card.cardType = "004"; break; case CardType.DinersClub: req.card.cardType = "005"; break; case CardType.JCB: req.card.cardType = "007"; break; default: req.card.cardType = ""; break; } req.card.expirationMonth = t.Card.ExpirationMonthPadded; req.card.expirationYear = t.Card.ExpirationYear.ToString(); if (t.Card.SecurityCode.Length > 0) { req.card.cvNumber = t.Card.SecurityCode; } } if (includeMerchantExtraData) { req.merchantDefinedData = new Cybersource.MerchantDefinedData(); req.merchantDefinedData.field1 = t.MerchantInvoiceNumber; } var proc = new Cybersource.TransactionProcessorClient(); proc.ChannelFactory.Credentials.UserName.UserName = req.merchantID; proc.ChannelFactory.Credentials.UserName.Password = Settings.TransactionKey; var reply = proc.runTransaction(req); t.Result.ReferenceNumber = reply.requestID; t.Result.ResponseCode = reply.reasonCode; t.Result.ResponseCodeDescription = ParseReasonCode(reply.reasonCode); switch (reply.decision.ToUpperInvariant()) { case "ACCEPT": t.Result.Succeeded = true; result = true; break; case "ERROR": t.Result.Succeeded = false; result = false; break; case "REJECT": t.Result.Succeeded = false; result = false; break; } if (reply.ccAuthReply != null) { t.Result.AvsCode = ParseAvsCode(reply.ccAuthReply.avsCode ?? string.Empty); t.Result.CvvCode = ParseSecurityCode(reply.ccAuthReply.cvCode ?? string.Empty); } } catch (Exception ex) { result = false; t.Result.Messages.Add(new Message("Unknown Payment Error: " + ex.Message, "BVP_CYBERSOURCE_1001", MessageType.Error)); t.Result.Messages.Add(new Message("Stack Trace " + ex.StackTrace, "STACKTRACE", MessageType.Error)); } t.Result.Succeeded = result; }