Exemplo n.º 1
0
        /// <summary>
        /// Process recurring payment
        /// </summary>
        /// <param name="paymentInfo">Payment info required for an order processing</param>
        /// <param name="customer">Customer</param>
        /// <param name="orderGuid">Unique order identifier</param>
        /// <param name="processPaymentResult">Process payment result</param>
        public void ProcessRecurringPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult)
        {
            InitSettings();
            MerchantAuthenticationType authentication = PopulateMerchantAuthentication();

            if (!paymentInfo.IsRecurringPayment)
            {
                ARBSubscriptionType subscription = new ARBSubscriptionType();
                NopSolutions.NopCommerce.Payment.Methods.AuthorizeNet.net.authorize.api.CreditCardType creditCard = new NopSolutions.NopCommerce.Payment.Methods.AuthorizeNet.net.authorize.api.CreditCardType();

                subscription.name = orderGuid.ToString();

                creditCard.cardNumber     = paymentInfo.CreditCardNumber;
                creditCard.expirationDate = paymentInfo.CreditCardExpireYear + "-" + paymentInfo.CreditCardExpireMonth; // required format for API is YYYY-MM
                creditCard.cardCode       = paymentInfo.CreditCardCvv2;

                subscription.payment      = new PaymentType();
                subscription.payment.Item = creditCard;

                subscription.billTo           = new NameAndAddressType();
                subscription.billTo.firstName = paymentInfo.BillingAddress.FirstName;
                subscription.billTo.lastName  = paymentInfo.BillingAddress.LastName;
                subscription.billTo.address   = paymentInfo.BillingAddress.Address1 + " " + paymentInfo.BillingAddress.Address2;
                subscription.billTo.city      = paymentInfo.BillingAddress.City;
                if (paymentInfo.BillingAddress.StateProvince != null)
                {
                    subscription.billTo.state = paymentInfo.BillingAddress.StateProvince.Abbreviation;
                }
                subscription.billTo.zip = paymentInfo.BillingAddress.ZipPostalCode;

                if (paymentInfo.ShippingAddress != null)
                {
                    subscription.shipTo           = new NameAndAddressType();
                    subscription.shipTo.firstName = paymentInfo.ShippingAddress.FirstName;
                    subscription.shipTo.lastName  = paymentInfo.ShippingAddress.LastName;
                    subscription.shipTo.address   = paymentInfo.ShippingAddress.Address1 + " " + paymentInfo.ShippingAddress.Address2;
                    subscription.shipTo.city      = paymentInfo.ShippingAddress.City;
                    if (paymentInfo.ShippingAddress.StateProvince != null)
                    {
                        subscription.shipTo.state = paymentInfo.ShippingAddress.StateProvince.Abbreviation;
                    }
                    subscription.shipTo.zip = paymentInfo.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 = string.Format("{0} {1}", IoC.Resolve <ISettingManager>().StoreName, "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(paymentInfo.RecurringTotalCycles);
                subscription.paymentSchedule.totalOccurrencesSpecified = true;

                subscription.amount          = paymentInfo.OrderTotal;
                subscription.amountSpecified = true;

                // Interval can't be updated once a subscription is created.
                subscription.paymentSchedule.interval = new PaymentScheduleTypeInterval();
                switch (paymentInfo.RecurringCyclePeriod)
                {
                case (int)RecurringProductCyclePeriodEnum.Days:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(paymentInfo.RecurringCycleLength);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.days;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Weeks:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(paymentInfo.RecurringCycleLength * 7);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.days;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Months:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(paymentInfo.RecurringCycleLength);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.months;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Years:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(paymentInfo.RecurringCycleLength * 12);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.months;
                    break;

                default:
                    throw new NopException("Not supported cycle period");
                }


                ARBCreateSubscriptionResponseType response = webService.ARBCreateSubscription(authentication, subscription);

                if (response.resultCode == MessageTypeEnum.Ok)
                {
                    processPaymentResult.SubscriptionTransactionId      = response.subscriptionId.ToString();
                    processPaymentResult.AuthorizationTransactionCode   = response.resultCode.ToString();
                    processPaymentResult.AuthorizationTransactionResult = string.Format("Approved ({0}: {1})", response.resultCode.ToString(), response.subscriptionId.ToString());
                }
                else
                {
                    processPaymentResult.Error     = string.Format("Error processing recurring payment. {0}", GetErrors(response));
                    processPaymentResult.FullError = string.Format("Error processing recurring payment. {0}", GetErrors(response));
                }
            }
        }
        /// <summary>
        /// Process recurring payment
        /// </summary>
        /// <param name="paymentInfo">Payment info required for an betting processing</param>
        /// <param name="bettingGuid">Unique order identifier</param>
        /// <param name="processPaymentResult">Process payment result</param>
        public void ProcessRecurringPayment(TransactionPayment transactionPayment, Guid bettingGuid, ref ProcessPaymentResult processPaymentResult)
        {
            InitSettings();
            MerchantAuthenticationType authentication = PopulateMerchantAuthentication();

            if (!transactionPayment.IsRecurringPayment)
            {
                ARBSubscriptionType subscription = new ARBSubscriptionType();
                AuthorizeNet.net.authorize.api.CreditCardType creditCard = new AuthorizeNet.net.authorize.api.CreditCardType();

                subscription.name         = bettingGuid.ToString();
                creditCard.cardNumber     = transactionPayment.PaymentMenthod.CreditCardNumber;
                creditCard.expirationDate = transactionPayment.PaymentMenthod.CardExpirationYear + "-" + transactionPayment.PaymentMenthod.CardExpirationMonth; // required format for API is YYYY-MM
                creditCard.cardCode       = transactionPayment.PaymentMenthod.CardCvv2;

                subscription.payment              = new PaymentType();
                subscription.payment.Item         = creditCard;
                subscription.billTo               = new NameAndAddressType();
                subscription.billTo.firstName     = transactionPayment.Customer.FirstName;
                subscription.billTo.lastName      = transactionPayment.Customer.LastName;
                subscription.billTo.address       = transactionPayment.Customer.Address;
                subscription.billTo.city          = transactionPayment.Customer.City;
                subscription.billTo.zip           = transactionPayment.Customer.PostalCode;
                subscription.customer             = new CustomerType();
                subscription.customer.email       = transactionPayment.Customer.Email1;
                subscription.customer.phoneNumber = transactionPayment.Customer.Telephone;
                subscription.order             = new OrderType();
                subscription.order.description = Constant.Payment.STORENAME + " " + "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(transactionPayment.RecurringTotalCycles);
                subscription.paymentSchedule.totalOccurrencesSpecified = true;

                subscription.amount          = transactionPayment.TransactionPaymentTotal;
                subscription.amountSpecified = true;

                // Interval can't be updated once a subscription is created.
                subscription.paymentSchedule.interval = new PaymentScheduleTypeInterval();
                switch (transactionPayment.RecurringCyclePeriod)
                {
                case (int)RecurringProductCyclePeriodEnum.Days:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(transactionPayment.RecurringCycleLength);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.days;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Weeks:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(transactionPayment.RecurringCycleLength * 7);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.days;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Months:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(transactionPayment.RecurringCycleLength);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.months;
                    break;

                case (int)RecurringProductCyclePeriodEnum.Years:
                    subscription.paymentSchedule.interval.length = Convert.ToInt16(transactionPayment.RecurringCycleLength * 12);
                    subscription.paymentSchedule.interval.unit   = ARBSubscriptionUnitEnum.months;
                    break;

                default:
                    throw new Exception("Not supported cycle period");
                }


                ARBCreateSubscriptionResponseType response = webService.ARBCreateSubscription(authentication, subscription);

                if (response.resultCode == MessageTypeEnum.Ok)
                {
                    processPaymentResult.SubscriptionTransactionId      = response.subscriptionId.ToString();
                    processPaymentResult.AuthorizationTransactionCode   = response.resultCode.ToString();
                    processPaymentResult.AuthorizationTransactionResult = string.Format("Approved ({0}: {1})", response.resultCode.ToString(), response.subscriptionId.ToString());
                }
                else
                {
                    processPaymentResult.Error     = string.Format("Error processing recurring payment. {0}", GetErrors(response));
                    processPaymentResult.FullError = string.Format("Error processing recurring payment. {0}", GetErrors(response));
                }
            }
        }