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);
        }
Example #2
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());
        }