Exemple #1
0
        private static List <PaymentDefinition> GetPaymentDefinitions(bool trial, int trialLength, decimal trialPrice, string frequency)
        {
            var paymentDefinitions = new List <PaymentDefinition>();

            if (trial)
            {
                paymentDefinitions.Add(
                    new PaymentDefinition
                {
                    name      = "Trial",
                    type      = "TRIAL",
                    frequency = frequency
                }
                    );
            }

            var regularPayment = new PaymentDefinition
            {
                name      = "Standard Plan",
                type      = "REGULAR",
                frequency = frequency
            };

            paymentDefinitions.Add(regularPayment);

            return(paymentDefinitions);
        }
        public ActionResult Create(BillingPlan billingPlan)
        {
            if (ModelState.IsValid)
            {
                var apiContext = Common.GetApiContext();

                var plan = new Plan();
                plan.description = billingPlan.Description;
                plan.name        = billingPlan.Name;
                plan.type        = billingPlan.PlanType;

                plan.merchant_preferences = new MerchantPreferences
                {
                    initial_fail_amount_action = "CANCEL",
                    max_fail_attempts          = "3",
                    cancel_url = "http://localhost:50728/plans",
                    return_url = "http://localhost:50728/plans"
                };

                plan.payment_definitions = new List <PaymentDefinition>();

                var paymentDefinition = new PaymentDefinition();
                paymentDefinition.name   = "Standard Plan";
                paymentDefinition.amount = new Currency {
                    currency = "USD", value = billingPlan.Amount.ToString()
                };
                paymentDefinition.frequency          = billingPlan.Frequency.ToString();
                paymentDefinition.type               = "REGULAR";
                paymentDefinition.frequency_interval = "1";

                if (billingPlan.NumberOfCycles.HasValue)
                {
                    paymentDefinition.cycles = billingPlan.NumberOfCycles.Value.ToString();
                }

                plan.payment_definitions.Add(paymentDefinition);

                var created = plan.Create(apiContext);

                if (created.state == "CREATED")
                {
                    var patchRequest = new PatchRequest();
                    patchRequest.Add(new Patch {
                        path = "/", op = "replace", value = new Plan()
                        {
                            state = "ACTIVE"
                        }
                    });
                    created.Update(apiContext, patchRequest);
                }

                TempData["success"] = "Billing plan created.";
                return(RedirectToAction("Index"));
            }

            AddDropdowns();
            return(View(billingPlan));
        }
Exemple #3
0
        private static List <PaymentDefinition> GetPaymentDefinitions(bool trial, int trialLength, decimal trialPrice,
                                                                      string frequency, int frequencyInterval, decimal planPrice, decimal shippingAmount, decimal taxPercentage)
        {
            var paymentDefinitions = new List <PaymentDefinition>();

            if (trial)
            {
                // Define a trial plan that will charge 'trialPrice' for 'trialLength'
                // After that, the standard plan will take over.
                paymentDefinitions.Add(
                    new PaymentDefinition()
                {
                    name               = "Trial",
                    type               = "TRIAL",
                    frequency          = frequency,
                    frequency_interval = frequencyInterval.ToString(),
                    amount             = GetCurrency(trialPrice.ToString()),
                    cycles             = trialLength.ToString(),
                    charge_models      = GetChargeModels(trialPrice, shippingAmount, taxPercentage)
                });
            }

            // Define the standard payment plan. It will represent a 'frequency' (monthly, etc)
            // plan for 'planPrice' that charges 'planPrice' (once a month) for #cycles.
            var regularPayment = new PaymentDefinition
            {
                name               = "Standard Plan",
                type               = "REGULAR",
                frequency          = frequency,
                frequency_interval = frequencyInterval.ToString(),
                amount             = GetCurrency(planPrice.ToString()),
                // > NOTE: For `IFNINITE` type plans, `cycles` should be 0 for a `REGULAR` `PaymentDefinition` object.
                cycles        = "11",
                charge_models = GetChargeModels(trialPrice, shippingAmount, taxPercentage)
            };

            paymentDefinitions.Add(regularPayment);

            return(paymentDefinitions);
        }
        public static Plan CreatePlanObject(HttpContext httpContext)
        {
            // Both the trial and standard plans will use the same shipping
            // charge for this example, so for simplicity we'll create a
            // single object to use with both payment definitions.
            var shippingChargeModel = new ChargeModel()
            {
                type   = "SHIPPING",
                amount = GetCurrency("9.99")
            };

            // Define a trial plan that will only charge $9.99 for the first
            // month. After that, the standard plan will take over for the
            // remaining 11 months of the year.
            var trialPlanPaymentDefinition = new PaymentDefinition()
            {
                name               = "Trial Plan",
                type               = "TRIAL",
                frequency          = "MONTH",
                frequency_interval = "1",
                amount             = GetCurrency("9.99"),
                cycles             = "1"
            };

            trialPlanPaymentDefinition.charge_models = new List <ChargeModel>();
            trialPlanPaymentDefinition.charge_models.Add(new ChargeModel()
            {
                type   = "TAX",
                amount = GetCurrency("1.65")
            });
            trialPlanPaymentDefinition.charge_models.Add(shippingChargeModel);

            // Define the standard payment plan. It will represent a monthly
            // plan for $19.99 USD that charges once month for 11 months.
            var regularPlanPaymentDefinition = new PaymentDefinition()
            {
                name               = "Standard Plan",
                type               = "REGULAR",
                frequency          = "MONTH",
                frequency_interval = "1",
                amount             = GetCurrency("19.99"),
                cycles             = "11"
            };

            regularPlanPaymentDefinition.charge_models = new List <ChargeModel>();
            regularPlanPaymentDefinition.charge_models.Add(new ChargeModel()
            {
                type   = "TAX",
                amount = GetCurrency("2.47")
            });
            regularPlanPaymentDefinition.charge_models.Add(shippingChargeModel);

            // Define the merchant preferences.
            // More Information: https://developer.paypal.com/webapps/developer/docs/api/#merchantpreferences-object
            var merchantPreferences = new MerchantPreferences()
            {
                setup_fee                  = GetCurrency("1"),
                return_url                 = httpContext.Request.Url.ToString(),
                cancel_url                 = httpContext.Request.Url.ToString() + "?cancel",
                auto_bill_amount           = "YES",
                initial_fail_amount_action = "CONTINUE",
                max_fail_attempts          = "0"
            };

            // Define the plan and attach the payment definitions and merchant preferences.
            // More Information: https://developer.paypal.com/webapps/developer/docs/api/#create-a-plan
            var plan = new Plan()
            {
                name                 = "T-Shirt of the Month Club Plan",
                description          = "Monthly plan for getting the t-shirt of the month.",
                type                 = "fixed",
                merchant_preferences = merchantPreferences
            };

            plan.payment_definitions = new List <PaymentDefinition>();
            plan.payment_definitions.Add(trialPlanPaymentDefinition);
            plan.payment_definitions.Add(regularPlanPaymentDefinition);
            return(plan);
        }
        public int CreatePlan(PlanDto planDto)
        {
            var currency = new Currency()
            {
                value    = planDto.PaymentAmount,
                currency = planDto.PaymentCurrency
            };

            var shippingChargeModel = new ChargeModel()
            {
                type   = "SHIPPING",
                amount = new Currency()
                {
                    value    = "1",
                    currency = planDto.PaymentCurrency
                }
            };

            var paymentDefinition = new PaymentDefinition()
            {
                name               = planDto.PaymentName,
                type               = planDto.PaymentType,
                frequency          = planDto.PaymentFrequency,
                frequency_interval = planDto.PaymentFrequencyInterval,
                amount             = currency,
                cycles             = planDto.PaymentCycles,
                charge_models      = new List <ChargeModel>
                {
                    new ChargeModel()
                    {
                        type   = "TAX",
                        amount = new Currency {
                            value    = "1",
                            currency = planDto.PaymentCurrency
                        }
                    },
                    shippingChargeModel
                }
            };

            var plan = new PayPal.Api.Plan
            {
                name        = planDto.Name,
                description = planDto.Description,
                type        = "fixed",

                payment_definitions = new List <PaymentDefinition>
                {
                    paymentDefinition
                },

                merchant_preferences = new MerchantPreferences()
                {
                    setup_fee = new Currency()
                    {
                        value    = "0",
                        currency = planDto.PaymentCurrency
                    },
                    return_url                 = "https://localhost:5001/api/values",
                    cancel_url                 = "https://localhost:5001/api/houses",
                    auto_bill_amount           = "YES",
                    initial_fail_amount_action = "CONTINUE",
                    max_fail_attempts          = "0"
                }
            };

            var createdPlan = plan.Create(_apiContext);
            var state       = new
            {
                state = "ACTIVE"
            };
            var patchRequest = new PatchRequest()
            {
                new Patch()
                {
                    op    = "replace",
                    path  = "/",
                    value = state
                }
            };

            createdPlan.Update(_apiContext, patchRequest);

            //save plan in db
            var planToSave = new Models.Plan
            {
                Name    = createdPlan.name,
                PlanId  = createdPlan.id,
                OwnerId = planDto.OwnerId
            };

            var planInsert = _planRepository.Insert(planToSave);

            return(planInsert.Id);
        }
Exemple #6
0
        public ActionResult TestRecurring()
        {
            string sPricePlan = Helpers.Tools.PriceFormat(priceItem);
            string sTaxAmt    = Helpers.Tools.PriceFormat(priceItem * tax);
            //string sSubtotal = Helpers.Tools.PriceFormat(decimal.Parse(sPriceItem) - decimal.Parse(sTaxAmt));

            APIContext apiContext = clsPayPal.PayPalConfig.GetAPIContext();

            Session["accesstoken"] = apiContext.AccessToken;

            ChargeModel cModel = new ChargeModel()
            {
                amount = new Currency()
                {
                    currency = currency,
                    value    = sTaxAmt
                },
                type = "TAX"
            };

            //cModel = new ChargeModel()
            //{
            //    amount = new Currency()
            //    {
            //        currency = currency,
            //        value = sTaxAmt
            //    },
            //    type = "SHIPPING"
            //};

            List <ChargeModel> lstModels = new List <ChargeModel>();

            lstModels.Add(cModel);

            PaymentDefinition pDef = new PaymentDefinition()
            {
                amount = new Currency()
                {
                    currency = currency,
                    value    = sPricePlan
                },
                cycles             = "11",
                charge_models      = lstModels,
                frequency          = "MONTH",
                frequency_interval = "1",
                type = "REGULAR",
                name = "pmt recurring",
            };
            List <PaymentDefinition> lstPayDef = new List <PaymentDefinition>();

            lstPayDef.Add(pDef);

            MerchantPreferences merPref = new MerchantPreferences()
            {
                setup_fee = new Currency()
                {
                    currency = currency,
                    value    = "1"
                },
                auto_bill_amount           = "YES",
                max_fail_attempts          = "0",
                initial_fail_amount_action = "CONTINUE",
                return_url = returnURL,
                cancel_url = cancelURL
            };

            Plan createdPlan = Plan.Get(apiContext, "P-4NL72850M8850524J7ZREBTY");

            if (createdPlan == null)
            {
                Plan myPlan = new Plan();
                myPlan.name                 = "Plan One";
                myPlan.description          = "Plan One Description";
                myPlan.type                 = "fixed";
                myPlan.payment_definitions  = lstPayDef;
                myPlan.merchant_preferences = merPref;
                var guid = Convert.ToString((new Random()).Next(100000));
                myPlan.merchant_preferences.return_url = Request.Url.ToString() + "?guid=" + guid;
                createdPlan = myPlan.Create(apiContext);
            }

            // Activate the plan
            var patchRequest = new PatchRequest()
            {
                new Patch()
                {
                    op    = "replace",
                    path  = "/",
                    value = new Plan()
                    {
                        state = "ACTIVE"
                    }
                }
            };

            createdPlan.Update(apiContext, patchRequest);

            ShippingAddress shippAddr = new ShippingAddress()
            {
                line1        = "111 First Street",
                city         = "Toronto",
                state        = "ON",
                postal_code  = "95070",
                country_code = "CA"
            };

            //Agreement newSub =clsPayPal.CreateBillingAgreement(hPlan.id, shippAddr, "some name", "some description", DateTime.Now);

            var agreement = new Agreement()
            {
                name        = "some name",
                description = "some description",
                start_date  = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "Z",
                payer       = new Payer()
                {
                    payment_method = "paypal",
                },
                plan = new Plan()
                {
                    id = createdPlan.id
                },
                shipping_address = shippAddr,
            };


            var createdAgreement = agreement.Create(apiContext);

            //return createdAgreement;



            /////

            //Details dtl = new Details()
            //{
            //    tax = sTaxAmt,
            //    subtotal = sSubtotal,
            //    shipping = "0",
            //};

            ////contact infor
            //Address addr = new Address()
            //{
            //    city = sCity,
            //    country_code = sCountryCode,
            //    line1 = sAddressLine1,
            //    line2 = sAddressLine2,
            //    phone = sPhone,
            //    postal_code = sPostalCode,
            //    state = sProvince,
            //};

            //PayerInfo payInfo = new PayerInfo()
            //{
            //    //billing_address = addr,
            //    //country_code= sCountryCode,
            //    first_name = sFirstName,
            //    last_name = sLastName,
            //    //middle_name ="",
            //    email = sEmail,
            //    //phone =sPhone,
            //};

            //Amount amnt = new Amount()
            //{
            //    currency = currency,
            //    total = sPricePlan,
            //    details = dtl,
            //};

            //List<Transaction> transactionList = new List<Transaction>();
            //Transaction tran = new Transaction()
            //{
            //    description = sItemName,
            //    custom = sItemName + " some additional information",
            //    amount = amnt
            //};
            //transactionList.Add(tran);

            //Payer payr = new Payer();
            //payr.payment_method = "paypal";
            //payr.payer_info = payInfo;

            //RedirectUrls redirUrls = new RedirectUrls();
            //redirUrls.cancel_url = "https://devtools-paypal.com/guide/pay_paypal/dotnet?cancel=true";
            ////redirUrls.return_url = "https://devtools-paypal.com/guide/pay_paypal/dotnet?success=true";
            //redirUrls.return_url = "https://localhost:44320/PayPal/ReturnTestRecurring";

            //Payment pymnt = new Payment();
            //pymnt.intent = "sale";
            //pymnt.payer = payr;
            //pymnt.transactions = transactionList;
            //pymnt.redirect_urls = redirUrls;

            //Payment createdPayment = pymnt.Create(apiContext);
            //string ApprovalURL = createdPayment.GetApprovalUrl();

            //Response.Redirect(ApprovalURL);

            return(View());
        }