public override string RecurringBillingCreateSubscription(String SubscriptionDescription, Customer ThisCustomer, Address UseBillingAddress, Address UseShippingAddress, Decimal RecurringAmount, DateTime StartDate, int RecurringInterval, DateIntervalTypeEnum RecurringIntervalType, int OriginalRecurringOrderNumber, string XID, IDictionary <string, string> TransactionContext, out String RecurringSubscriptionID, out String RecurringSubscriptionCommand, out String RecurringSubscriptionResult) { string result = string.Empty; try { //Re-Use the Internal Gateway Recurring Billing logic for calculating how much of the order is recurring ShoppingCart recurringCart = new ShoppingCart(ThisCustomer.SkinID, ThisCustomer, CartTypeEnum.RecurringCart, OriginalRecurringOrderNumber, false); CreditCardDetailsType creditCard = new CreditCardDetailsType(); if (UseBillingAddress.CardNumber != null && UseBillingAddress.CardNumber.Length > 0) { creditCard.CreditCardNumber = UseBillingAddress.CardNumber; creditCard.ExpMonth = Localization.ParseUSInt(UseBillingAddress.CardExpirationMonth); creditCard.ExpYear = Localization.ParseUSInt(UseBillingAddress.CardExpirationYear); creditCard.ExpMonthSpecified = true; creditCard.ExpYearSpecified = true; creditCard.CVV2 = XID; if (UseBillingAddress.CardType == "AmericanExpress") { creditCard.CreditCardType = (CreditCardTypeType)Enum.Parse(typeof(CreditCardTypeType), "Amex", true); } else { creditCard.CreditCardType = (CreditCardTypeType)Enum.Parse(typeof(CreditCardTypeType), UseBillingAddress.CardType, true); } creditCard.CreditCardTypeSpecified = true; } else { creditCard.CreditCardTypeSpecified = false; } BasicAmountType recurringAmount = new BasicAmountType(); recurringAmount.currencyID = (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), AppLogic.AppConfig("Localization.StoreCurrency"), true); recurringAmount.Value = RecurringAmount.ToString(); DateIntervalTypeEnum recurringIntervalType = recurringCart.CartItems[0].RecurringIntervalType; //We currently only support 1 interval per recurring order, so grabbing the first as a default should be safe int recurringInterval = recurringCart.CartItems[0].RecurringInterval; BillingPeriodDetailsType billingPeriodDetails = PayPalController.GetECRecurringPeriodDetails(recurringIntervalType, recurringInterval); billingPeriodDetails.Amount = recurringAmount; billingPeriodDetails.TotalBillingCyclesSpecified = false; ScheduleDetailsType scheduleDetails = new ScheduleDetailsType(); scheduleDetails.Description = string.Format("Recurring order created on {0} from {1}", System.DateTime.Now.ToShortDateString(), AppLogic.AppConfig("StoreName")); scheduleDetails.MaxFailedPayments = 0; scheduleDetails.MaxFailedPaymentsSpecified = true; scheduleDetails.AutoBillOutstandingAmount = AutoBillType.NoAutoBill; scheduleDetails.AutoBillOutstandingAmountSpecified = true; scheduleDetails.PaymentPeriod = billingPeriodDetails; RecurringPaymentsProfileDetailsType profileDetails = new RecurringPaymentsProfileDetailsType(); profileDetails.SubscriberName = ThisCustomer.FirstName + " " + ThisCustomer.LastName; profileDetails.BillingStartDate = StartDate; CreateRecurringPaymentsProfileRequestDetailsType profileRequestDetails = new CreateRecurringPaymentsProfileRequestDetailsType(); profileRequestDetails.ScheduleDetails = scheduleDetails; profileRequestDetails.RecurringPaymentsProfileDetails = profileDetails; profileRequestDetails.CreditCard = creditCard; if (!(UseBillingAddress.CardNumber != null && UseBillingAddress.CardNumber.Length > 0)) { profileRequestDetails.Token = XID; } if (recurringCart.IsAllDownloadComponents()) { PaymentDetailsItemType paymentDetailsItem = new PaymentDetailsItemType(); paymentDetailsItem.ItemCategory = ItemCategoryType.Digital; paymentDetailsItem.ItemCategorySpecified = true; List <PaymentDetailsItemType> paymentDetailsList = new List <PaymentDetailsItemType>(); paymentDetailsList.Add(paymentDetailsItem); profileRequestDetails.PaymentDetailsItem = paymentDetailsList.ToArray(); } CreateRecurringPaymentsProfileRequestType profileRequest = new CreateRecurringPaymentsProfileRequestType(); profileRequest.Version = API_VER; profileRequest.CreateRecurringPaymentsProfileRequestDetails = profileRequestDetails; CreateRecurringPaymentsProfileReq request = new CreateRecurringPaymentsProfileReq(); request.CreateRecurringPaymentsProfileRequest = profileRequest; CreateRecurringPaymentsProfileResponseType profileResponse = new CreateRecurringPaymentsProfileResponseType(); profileResponse = IPayPal.CreateRecurringPaymentsProfile(request); if (profileResponse != null && profileResponse.Ack.ToString().StartsWith("success", StringComparison.InvariantCultureIgnoreCase)) { result = AppLogic.ro_OK; } else { if (profileResponse.Errors != null) { bool first = true; for (int ix = 0; ix < profileResponse.Errors.Length; ix++) { if (!first) { result += ", "; } result += profileResponse.Errors[ix].LongMessage; first = false; } } } RecurringSubscriptionID = (profileResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID == null ? "No ProfileID provided" : profileResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID); RecurringSubscriptionCommand = string.Empty; RecurringSubscriptionResult = (profileResponse.CreateRecurringPaymentsProfileResponseDetails.DCCProcessorResponse == null ? "No response provided" : profileResponse.CreateRecurringPaymentsProfileResponseDetails.DCCProcessorResponse); //Log the transaction OrderTransactionCollection ecRecurringOrderTransaction = new OrderTransactionCollection(OriginalRecurringOrderNumber); ecRecurringOrderTransaction.AddTransaction("PayPal Express Checkout Recurring Profile Creation", request.ToString(), result, string.Empty, (profileResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID == null ? "No ProfileID provided" : profileResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID), AppLogic.ro_PMPayPalExpress, null, RecurringAmount); } catch { result = "Recurring Profile Creation Failed."; RecurringSubscriptionID = string.Empty; RecurringSubscriptionCommand = string.Empty; RecurringSubscriptionResult = result; } return(result); }
public static String MakeECRecurringProfile(ShoppingCart cart, int orderNumber, String payPalToken, String payerID, DateTime nextRecurringShipDate) { PayPalAPISoapBinding IPayPalRefund; PayPalAPIAASoapBinding IPayPal; PayPalController.GetPaypalRequirements(out IPayPalRefund, out IPayPal); String result = String.Empty; CreateRecurringPaymentsProfileReq ECRecurringRequest = new CreateRecurringPaymentsProfileReq(); CreateRecurringPaymentsProfileRequestType varECRequest = new CreateRecurringPaymentsProfileRequestType(); CreateRecurringPaymentsProfileRequestDetailsType varECRequestDetails = new CreateRecurringPaymentsProfileRequestDetailsType(); CreateRecurringPaymentsProfileResponseType ECRecurringResponse = new CreateRecurringPaymentsProfileResponseType(); //Re-Use the Internal Gateway Recurring Billing logic for calculating how much of the order is recurring ShoppingCart cartRecur = new ShoppingCart(cart.ThisCustomer.SkinID, cart.ThisCustomer, CartTypeEnum.RecurringCart, orderNumber, false); Decimal CartTotalRecur = Decimal.Round(cartRecur.Total(true), 2, MidpointRounding.AwayFromZero); Decimal RecurringAmount = CartTotalRecur - CommonLogic.IIF(cartRecur.Coupon.CouponType == CouponTypeEnum.GiftCard, CommonLogic.IIF(CartTotalRecur < cartRecur.Coupon.DiscountAmount, CartTotalRecur, cartRecur.Coupon.DiscountAmount), 0); DateIntervalTypeEnum ecRecurringIntervalType = cartRecur.CartItems[0].RecurringIntervalType; //We currently only support 1 interval per recurring order, so grabbing the first as a default should be safe int ecRecurringInterval = cartRecur.CartItems[0].RecurringInterval; BasicAmountType ecRecurringAmount = new BasicAmountType(); ecRecurringAmount.currencyID = (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), AppLogic.AppConfig("Localization.StoreCurrency"), true); ecRecurringAmount.Value = RecurringAmount.ToString(); BillingPeriodDetailsType varECSchedulePaymentDetails = GetECRecurringPeriodDetails(ecRecurringIntervalType, ecRecurringInterval); varECSchedulePaymentDetails.Amount = ecRecurringAmount; varECSchedulePaymentDetails.TotalBillingCyclesSpecified = false; ScheduleDetailsType varECSchedule = new ScheduleDetailsType(); //Need a better description, but it must match the one sent in StartEC varECSchedule.Description = "Recurring order created on " + System.DateTime.Now.ToShortDateString() + " from " + AppLogic.AppConfig("StoreName"); varECSchedule.MaxFailedPayments = 0; //Cancel the order if a recurrence fails varECSchedule.MaxFailedPaymentsSpecified = true; varECSchedule.AutoBillOutstandingAmount = AutoBillType.NoAutoBill; varECSchedule.AutoBillOutstandingAmountSpecified = true; varECSchedule.PaymentPeriod = varECSchedulePaymentDetails; RecurringPaymentsProfileDetailsType varECProfileDetails = new RecurringPaymentsProfileDetailsType(); varECProfileDetails.SubscriberName = cart.ThisCustomer.FirstName + " " + cart.ThisCustomer.LastName; varECProfileDetails.BillingStartDate = nextRecurringShipDate; varECRequestDetails.ScheduleDetails = varECSchedule; varECRequestDetails.Token = payPalToken; varECRequestDetails.RecurringPaymentsProfileDetails = varECProfileDetails; if (cart.IsAllDownloadComponents()) { PaymentDetailsItemType varECPaymentDetails = new PaymentDetailsItemType(); varECPaymentDetails.ItemCategory = ItemCategoryType.Digital; varECPaymentDetails.ItemCategorySpecified = true; List <PaymentDetailsItemType> ECPaymentDetailsList = new List <PaymentDetailsItemType>(); ECPaymentDetailsList.Add(varECPaymentDetails); varECRequestDetails.PaymentDetailsItem = ECPaymentDetailsList.ToArray(); } varECRequest.Version = API_VER; varECRequest.CreateRecurringPaymentsProfileRequestDetails = varECRequestDetails; ECRecurringRequest.CreateRecurringPaymentsProfileRequest = varECRequest; ECRecurringResponse = IPayPal.CreateRecurringPaymentsProfile(ECRecurringRequest); if (ECRecurringResponse.Ack.ToString().StartsWith("success", StringComparison.InvariantCultureIgnoreCase)) { result = AppLogic.ro_OK; } else { if (ECRecurringResponse.Errors != null) { bool first = true; for (int ix = 0; ix < ECRecurringResponse.Errors.Length; ix++) { if (!first) { result += ", "; } result += ECRecurringResponse.Errors[ix].LongMessage; first = false; } } } //Log the transaction OrderTransactionCollection ecRecurringOrderTransaction = new OrderTransactionCollection(orderNumber); ecRecurringOrderTransaction.AddTransaction("PayPal Express Checkout Recurring Profile Creation", ECRecurringRequest.ToString(), result, payerID, //PNREF = payerID (ECRecurringResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID == null ? "No ProfileID provided" : ECRecurringResponse.CreateRecurringPaymentsProfileResponseDetails.ProfileID), //Code = ProfileID AppLogic.ro_PMPayPalExpress, null, RecurringAmount); return(result); }