/// <summary> /// Process recurring payment /// </summary> /// <param name="processPaymentRequest">Payment info required for an order processing</param> /// <returns>Process payment result</returns> public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest) { var result = new ProcessPaymentResult(); if (!processPaymentRequest.IsRecurringPayment) { var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId); var subscription = new ARBSubscriptionType(); var creditCard = new net.authorize.api.CreditCardType(); subscription.name = processPaymentRequest.OrderGuid.ToString(); creditCard.cardNumber = processPaymentRequest.CreditCardNumber; creditCard.expirationDate = processPaymentRequest.CreditCardExpireYear + "-" + processPaymentRequest.CreditCardExpireMonth; // required format for API is YYYY-MM creditCard.cardCode = processPaymentRequest.CreditCardCvv2; subscription.payment = new PaymentType(); subscription.payment.Item = creditCard; subscription.billTo = new NameAndAddressType(); subscription.billTo.firstName = customer.BillingAddress.FirstName; subscription.billTo.lastName = customer.BillingAddress.LastName; subscription.billTo.address = customer.BillingAddress.Address1; //subscription.billTo.address = customer.BillingAddress.Address1 + " " + customer.BillingAddress.Address2; subscription.billTo.city = customer.BillingAddress.City; if (customer.BillingAddress.StateProvince != null) { subscription.billTo.state = customer.BillingAddress.StateProvince.Abbreviation; } subscription.billTo.zip = customer.BillingAddress.ZipPostalCode; if (customer.ShippingAddress != null) { subscription.shipTo = new NameAndAddressType(); subscription.shipTo.firstName = customer.ShippingAddress.FirstName; subscription.shipTo.lastName = customer.ShippingAddress.LastName; subscription.shipTo.address = customer.ShippingAddress.Address1; //subscription.shipTo.address = customer.ShippingAddress.Address1 + " " + customer.ShippingAddress.Address2; subscription.shipTo.city = customer.ShippingAddress.City; if (customer.ShippingAddress.StateProvince != null) { subscription.shipTo.state = customer.ShippingAddress.StateProvince.Abbreviation; } subscription.shipTo.zip = customer.ShippingAddress.ZipPostalCode; } subscription.customer = new CustomerType(); subscription.customer.email = customer.BillingAddress.Email; subscription.customer.phoneNumber = customer.BillingAddress.PhoneNumber; subscription.order = new OrderType(); subscription.order.description = "Recurring payment"; // Create a subscription that is leng of specified occurrences and interval is amount of days ad runs subscription.paymentSchedule = new PaymentScheduleType(); DateTime dtNow = DateTime.UtcNow; subscription.paymentSchedule.startDate = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day); subscription.paymentSchedule.startDateSpecified = true; subscription.paymentSchedule.totalOccurrences = Convert.ToInt16(processPaymentRequest.RecurringTotalCycles); subscription.paymentSchedule.totalOccurrencesSpecified = true; var orderTotal = Math.Round(processPaymentRequest.OrderTotal, 2); subscription.amount = orderTotal; subscription.amountSpecified = true; // Interval can't be updated once a subscription is created. subscription.paymentSchedule.interval = new PaymentScheduleTypeInterval(); switch (processPaymentRequest.RecurringCyclePeriod) { case RecurringProductCyclePeriod.Days: subscription.paymentSchedule.interval.length = Convert.ToInt16(processPaymentRequest.RecurringCycleLength); subscription.paymentSchedule.interval.unit = ARBSubscriptionUnitEnum.days; break; case RecurringProductCyclePeriod.Weeks: subscription.paymentSchedule.interval.length = Convert.ToInt16(processPaymentRequest.RecurringCycleLength * 7); subscription.paymentSchedule.interval.unit = ARBSubscriptionUnitEnum.days; break; case RecurringProductCyclePeriod.Months: subscription.paymentSchedule.interval.length = Convert.ToInt16(processPaymentRequest.RecurringCycleLength); subscription.paymentSchedule.interval.unit = ARBSubscriptionUnitEnum.months; break; case RecurringProductCyclePeriod.Years: subscription.paymentSchedule.interval.length = Convert.ToInt16(processPaymentRequest.RecurringCycleLength * 12); subscription.paymentSchedule.interval.unit = ARBSubscriptionUnitEnum.months; break; default: throw new NopException("Not supported cycle period"); } using (var webService = new net.authorize.api.Service()) { if (_authorizeNetPaymentSettings.UseSandbox) webService.Url = "https://apitest.authorize.net/soap/v1/Service.asmx"; else webService.Url = "https://api.authorize.net/soap/v1/Service.asmx"; var authentication = PopulateMerchantAuthentication(); var response = webService.ARBCreateSubscription(authentication, subscription); if (response.resultCode == MessageTypeEnum.Ok) { result.SubscriptionTransactionId = response.subscriptionId.ToString(); result.AuthorizationTransactionCode = response.resultCode.ToString(); result.AuthorizationTransactionResult = string.Format("Approved ({0}: {1})", response.resultCode.ToString(), response.subscriptionId.ToString()); if (_authorizeNetPaymentSettings.TransactMode == TransactMode.Authorize) { result.NewPaymentStatus = PaymentStatus.Authorized; } else { result.NewPaymentStatus = PaymentStatus.Paid; } } else { result.AddError(string.Format("Error processing recurring payment. {0}", GetErrors(response))); } } } return result; }
/// <summary> /// Cancels a recurring payment /// </summary> /// <param name="cancelPaymentRequest">Request</param> /// <returns>Result</returns> public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest) { var result = new CancelRecurringPaymentResult(); long subscriptionId; long.TryParse(cancelPaymentRequest.Order.SubscriptionTransactionId, out subscriptionId); using (var webService = new net.authorize.api.Service()) { if (_authorizeNetPaymentSettings.UseSandbox) webService.Url = "https://apitest.authorize.net/soap/v1/Service.asmx"; else webService.Url = "https://api.authorize.net/soap/v1/Service.asmx"; var authentication = PopulateMerchantAuthentication(); var response = webService.ARBCancelSubscription(authentication, subscriptionId); if (response.resultCode == MessageTypeEnum.Ok) { //ok } else { result.AddError("Error cancelling subscription, please contact customer support. " + GetErrors(response)); } } return result; }