Пример #1
0
        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);
        }
        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);
        }
        protected override void RunSample()
        {
            // ### Api Context
            // Pass in a `APIContext` object to authenticate 
            // the call and to send a unique request id 
            // (that ensures idempotency). The SDK generates
            // a request id if you do not pass one explicitly. 
            // See [Configuration.cs](/Source/Configuration.html) to know more about APIContext.
            var apiContext = Configuration.GetAPIContext();

            // ### Create a Billing Plan
            // For demonstration purposes, we'll first create a new billing 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 = new MerchantPreferences
                {
                    setup_fee = new Currency { value = "1.00", currency = "USD" },
                    return_url = "https://www.paypal.com",
                    cancel_url = "https://www.paypal.com",
                    auto_bill_amount = "YES",
                    initial_fail_amount_action = "CONTINUE",
                    max_fail_attempts = "0"
                },
                payment_definitions = new List<PaymentDefinition>
                {
                    new PaymentDefinition()
                    {
                        name = "Standard Plan",
                        type = "REGULAR",
                        frequency = "MONTH",
                        frequency_interval = "1",
                        amount = new Currency() { value = "19.99", currency = "USD" },
                        cycles = "11",
                        charge_models = new List<ChargeModel>
                        {
                            new ChargeModel
                            {
                                type = "TAX",
                                amount = new Currency() { value = "2.47", currency = "USD" }
                            },
                            new ChargeModel()
                            {
                                type = "SHIPPING",
                                amount = new Currency() { value = "9.99", currency = "USD" }
                            }
                        }
                    }
                }
            };

            #region Track Workflow
            //--------------------
            this.flow.AddNewRequest("Create the billing plan", plan);
            //--------------------
            #endregion

            // Create the billing plan.
            var createdPlan = plan.Create(apiContext);

            #region Track Workflow
            //--------------------
            this.flow.RecordResponse(createdPlan);
            //--------------------
            #endregion

            // ### Delete the Billing Plan
            // Deleting the plan is done by applying an update to the plan with its `state` set to `DELETED`.
            // > NOTE: Only the 'replace' operation is supported when updating billing plans.
            // For demonstration purposes, we'll create the `patchRequest` object here to show you how the plan is being deleted.
            var patchRequest = new PatchRequest
            {
                new Patch
                {
                    op = "replace",
                    path = "/",
                    value = new Plan { state = "DELETED" }
                }
            };

            #region Track Workflow
            this.flow.AddNewRequest("Delete the billing plan", patchRequest);
            #endregion

            // To make it easier for developers, this functionality has been built into the `Plan.Delete()` method.
            createdPlan.Delete(apiContext);

            #region Track Workflow
            this.flow.RecordActionSuccess("Billing plan deleted successfully");
            #endregion

            // For more information, please visit [PayPal Developer REST API Reference](https://developer.paypal.com/docs/api/).
        }
Пример #4
0
 /// <summary>
 /// Create a new billing plan by passing the details for the plan, including the plan name, description, and type, to the request URI.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <returns>Plan</returns>
 public Plan Create(APIContext apiContext)
 {
     return(Plan.Create(apiContext, this));
 }