예제 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var CurrContext  = HttpContext.Current;
            var profile      = new WebProfile();
            var patchRequest = new PatchRequest();

            try
            {
                var apiContext = Configuration.GetAPIContext();

                // Setup the profile we want to create
                profile.name                             = Guid.NewGuid().ToString();
                profile.presentation                     = new Presentation();
                profile.presentation.brand_name          = "Sample brand";
                profile.presentation.locale_code         = "US";
                profile.presentation.logo_image          = "https://www.paypal.com/";
                profile.input_fields                     = new InputFields();
                profile.input_fields.address_override    = 1;
                profile.input_fields.allow_note          = true;
                profile.input_fields.no_shipping         = 0;
                profile.flow_config                      = new FlowConfig();
                profile.flow_config.bank_txn_pending_url = "https://www.paypal.com/";
                profile.flow_config.landing_page_type    = "billing";

                // Create the profile
                var response = profile.Create(apiContext);

                // Create a patch and add it to the PatchRequest object used to
                // perform the partial update on the profile.
                var patch1 = new Patch();
                patch1.op    = "add";
                patch1.path  = "/presentation/brand_name";
                patch1.value = "New brand name";
                patchRequest.Add(patch1);

                var patch2 = new Patch();
                patch2.op   = "remove";
                patch2.path = "/flow_config/landing_page_type";
                patchRequest.Add(patch1);

                // Get the profile object and partially update the profile.
                var retrievedProfile = WebProfile.Get(apiContext, response.id);
                retrievedProfile.PartialUpdate(apiContext, patchRequest);

                CurrContext.Items.Add("ResponseJson", "Experience profile successfully updated.");

                // Delete the newly-created profile
                retrievedProfile.Delete(apiContext);
            }
            catch (Exception ex)
            {
                CurrContext.Items.Add("Error", ex.Message);
            }

            CurrContext.Items.Add("RequestJson", patchRequest.ConvertToJson());

            Server.Transfer("~/Response.aspx");
        }
        public void AgreementUpdateTest()
        {
            // Get the agreement to be used for verifying the update functionality
            var apiContext  = TestingUtil.GetApiContext();
            var agreementId = "I-HP4H4YJFCN07";
            var agreement   = Agreement.Get(apiContext, agreementId);

            // Create an update for the agreement
            var updatedDescription = Guid.NewGuid().ToString();
            var patch = new Patch();

            patch.op    = "replace";
            patch.path  = "/";
            patch.value = new Agreement()
            {
                description = updatedDescription
            };
            var patchRequest = new PatchRequest();

            patchRequest.Add(patch);

            // Update the agreement
            agreement.Update(apiContext, patchRequest);

            // Verify the agreement was successfully updated
            var updatedAgreement = Agreement.Get(apiContext, agreementId);

            Assert.AreEqual(agreementId, updatedAgreement.id);
            Assert.AreEqual(updatedDescription, updatedAgreement.description);
        }
예제 #3
0
        public void PlanUpdateTest()
        {
            var apiContext = TestingUtil.GetApiContext();

            // Get a test plan for updating purposes.
            var plan        = GetPlan();
            var createdPlan = plan.Create(TestingUtil.GetApiContext());
            var planId      = createdPlan.id;

            // Create the patch request and update the description to a random value.
            var updatedDescription = Guid.NewGuid().ToString();
            var patch = new Patch();

            patch.op    = "replace";
            patch.path  = "/";
            patch.value = new Plan()
            {
                description = updatedDescription
            };
            var patchRequest = new PatchRequest();

            patchRequest.Add(patch);

            // Update the plan.
            createdPlan.Update(apiContext, patchRequest);

            // Verify the plan was updated successfully.
            var updatedPlan = Plan.Get(apiContext, planId);

            Assert.AreEqual(planId, updatedPlan.id);
            Assert.AreEqual(updatedDescription, updatedPlan.description);
        }
예제 #4
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));
        }
예제 #5
0
        public void WebProfilePartialUpdateTest()
        {
            try
            {
                // Create a new profile
                var profileName = Guid.NewGuid().ToString();
                var profile     = WebProfileTest.GetWebProfile();
                profile.name = profileName;
                var createdProfile = profile.Create(TestingUtil.GetApiContext());

                // Get the profile object for the new profile
                profile = WebProfile.Get(TestingUtil.GetApiContext(), createdProfile.id);

                // Partially update the profile
                var newName = "New " + profileName;
                var patch1  = new Patch();
                patch1.op    = "add";
                patch1.path  = "/presentation/brand_name";
                patch1.value = newName;

                var patch2 = new Patch();
                patch2.op   = "remove";
                patch2.path = "/flow_config/landing_page_type";

                var patchRequest = new PatchRequest();
                patchRequest.Add(patch1);
                patchRequest.Add(patch2);

                profile.PartialUpdate(TestingUtil.GetApiContext(), patchRequest);

                // Get the profile again and verify it was successfully updated via the patch commands.
                var retrievedProfile = WebProfile.Get(TestingUtil.GetApiContext(), profile.id);
                Assert.AreEqual(newName, retrievedProfile.presentation.brand_name);
                Assert.IsTrue(string.IsNullOrEmpty(retrievedProfile.flow_config.landing_page_type));

                // Delete the profile
                profile.Delete(TestingUtil.GetApiContext());
            }
            catch (ConnectionException ex)
            {
                TestingUtil.WriteConnectionExceptionDetails(ex);
                throw;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // In order to update the plan, you must define one or more
                // patches to be applied to the plan. The patches will be
                // applied in the order in which they're specified.
                //
                // The 'value' of each Patch object will need to be a Plan object
                // that contains the fields that will be modified.
                // More Information: https://developer.paypal.com/webapps/developer/docs/api/#patchrequest-object
                var tempPlan = new Plan();
                tempPlan.description = "Some updated description (" + Guid.NewGuid().ToString() + ").";

                // NOTE: Only the 'replace' operation is supported when updating
                //       billing plans.
                var patch = new Patch()
                {
                    op    = "replace",
                    path  = "/",
                    value = tempPlan
                };
                var patchRequest = new PatchRequest();
                patchRequest.Add(patch);

                HttpContext.Current.Items.Add("RequestJson", Common.FormatJsonString(patchRequest.ConvertToJson()));

                // Get the plan we want to update.
                var apiContext = Configuration.GetAPIContext();
                var planId     = "P-23P27073KJ353233VHEXQM4Y";
                var plan       = Plan.Get(apiContext, planId);

                // Update the plan.
                plan.Update(apiContext, patchRequest);

                // After it's been updated, get it again to make sure it was updated properly (and so we can see what it looks like afterwards).
                var updatedPlan = Plan.Get(apiContext, planId);

                HttpContext.Current.Items.Add("ResponseTitle", "Updated Billing Plan Details");
                HttpContext.Current.Items.Add("ResponseJson", Common.FormatJsonString(updatedPlan.ConvertToJson()));
            }
            catch (Exception ex)
            {
                HttpContext.Current.Items.Add("Error", ex.Message);
            }

            Server.Transfer("~/Response.aspx");
        }
예제 #7
0
        public ActionResult Activate(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var apiContext = Common.GetApiContext();
                var plan = Plan.Get(apiContext, id);
                if (plan != null && plan.state == "CREATED")
                {
                    var patchRequest = new PatchRequest();
                    var tempPlan = new Plan();
                    tempPlan.state = "ACTIVE";
                    patchRequest.Add(new Patch { path = "/", op = "replace", value = tempPlan });
                    plan.Update(apiContext, patchRequest);

                    TempData["success"] = "Plan activated";
                }
            }

            return RedirectToAction("Index");
        }
예제 #8
0
        public ActionResult Activate(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var apiContext = Common.GetApiContext();
                var plan       = Plan.Get(apiContext, id);
                if (plan != null && plan.state == "CREATED")
                {
                    var patchRequest = new PatchRequest();
                    var tempPlan     = new Plan();
                    tempPlan.state = "ACTIVE";
                    patchRequest.Add(new Patch {
                        path = "/", op = "replace", value = tempPlan
                    });
                    plan.Update(apiContext, patchRequest);

                    TempData["success"] = "Plan activated";
                }
            }

            return(RedirectToAction("Index"));
        }
예제 #9
0
        public void AgreementUpdateTest()
        {
            // Get the agreement to be used for verifying the update functionality
            var apiContext = TestingUtil.GetApiContext();
            var agreementId = "I-HP4H4YJFCN07";
            var agreement = Agreement.Get(apiContext, agreementId);

            // Create an update for the agreement
            var updatedDescription = Guid.NewGuid().ToString();
            var patch = new Patch();
            patch.op = "replace";
            patch.path = "/";
            patch.value = new Agreement() { description = updatedDescription };
            var patchRequest = new PatchRequest();
            patchRequest.Add(patch);

            // Update the agreement
            agreement.Update(apiContext, patchRequest);

            // Verify the agreement was successfully updated
            var updatedAgreement = Agreement.Get(apiContext, agreementId);
            Assert.AreEqual(agreementId, updatedAgreement.id);
            Assert.AreEqual(updatedDescription, updatedAgreement.description);
        }
예제 #10
0
        public ActionResult Edit(string id, CreditCard creditCard)
        {
            if (ModelState.IsValid)
            {
                var apiContext = Common.GetApiContext();

                creditCard.type = Common.GetCardType(creditCard.number);
                var existing = CreditCard.Get(apiContext, id);
                if (existing != null)
                {
                    var patchRequest = new PatchRequest();

                    // determine what's changed between the existing card
                    // and the posted values
                    if (creditCard.expire_month != existing.expire_month)
                    {
                        patchRequest.Add(new Patch {
                            op = "replace", path = "/expire_month", value = creditCard.expire_month
                        });
                    }

                    if (creditCard.expire_year != existing.expire_year)
                    {
                        patchRequest.Add(new Patch {
                            op = "replace", path = "/expire_year", value = creditCard.expire_year
                        });
                    }

                    if (!string.IsNullOrEmpty(creditCard.first_name) && creditCard.first_name != existing.first_name)
                    {
                        patchRequest.Add(new Patch {
                            op = "replace", path = "/first_name", value = creditCard.first_name
                        });
                    }

                    if (!string.IsNullOrEmpty(creditCard.last_name) && creditCard.last_name != existing.last_name)
                    {
                        patchRequest.Add(new Patch {
                            op = "replace", path = "/last_name", value = creditCard.last_name
                        });
                    }

                    if (patchRequest.Count > 0)
                    {
                        existing.Update(apiContext, patchRequest);

                        TempData["success"] = "Stored Card updated.";

                        return(RedirectToAction("Details", new { id }));
                    }
                    else
                    {
                        TempData["info"] = "Nothing to update";
                    }
                }
            }
            else
            {
                TempData["info"] = "ModelState is invalid";
            }

            AddPaymentDropdowns();
            return(View(creditCard));
        }
        /// <summary>
        ///
        /// </summary>
        private void CreateBillingAgreement()
        {
            var apiContext = Configuration.GetAPIContext();

            // Before we can setup the billing agreement, we must first create a
            // billing plan that includes a redirect URL back to this test server.
            var plan = BillingPlanCreate.CreatePlanObject(HttpContext.Current);
            var guid = Convert.ToString((new Random()).Next(100000));

            plan.merchant_preferences.return_url = Request.Url.ToString() + "?guid=" + guid;
            var createdPlan = plan.Create(apiContext);

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

            patchRequest.Add(patch);
            createdPlan.Update(apiContext, patchRequest);

            // With the plan created and activated, we can now create the
            // billing agreement.
            var payer = new Payer()
            {
                payment_method = "paypal"
            };
            var shippingAddress = new ShippingAddress()
            {
                line1        = "111 First Street",
                city         = "Saratoga",
                state        = "CA",
                postal_code  = "95070",
                country_code = "US"
            };

            var agreement = new Agreement()
            {
                name        = "T-Shirt of the Month Club",
                description = "Agreement for T-Shirt of the Month Club",
                start_date  = "2015-02-19T00:37:04Z",
                payer       = payer,
                plan        = new Plan()
                {
                    id = createdPlan.id
                },
                shipping_address = shippingAddress
            };

            HttpContext.Current.Items.Add("RequestJson", Common.FormatJsonString(agreement.ConvertToJson()));

            // Create the billing agreement.
            var createdAgreement = agreement.Create(apiContext);

            HttpContext.Current.Items.Add("ResponseJson", Common.FormatJsonString(createdAgreement.ConvertToJson()));

            // Get the redirect URL to allow the user to be redirected to PayPal to accept the agreement.
            var links = createdAgreement.links.GetEnumerator();

            while (links.MoveNext())
            {
                Links lnk = links.Current;
                if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                {
                    HttpContext.Current.Items.Add("RedirectURLText", "Redirect to PayPal to approve billing agreement...");
                    HttpContext.Current.Items.Add("RedirectURL", lnk.href);
                }
            }
            Session.Add(guid, createdAgreement.token);
        }
예제 #12
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);
        }
예제 #13
0
        public ActionResult Edit(string id, CreditCard creditCard)
        {
            if (ModelState.IsValid)
            {
                var apiContext = Common.GetApiContext();

                creditCard.type = Common.GetCardType(creditCard.number);
                var existing = CreditCard.Get(apiContext, id);
                if (existing != null)
                {
                    var patchRequest = new PatchRequest();

                    // determine what's changed between the existing card
                    // and the posted values
                    if (creditCard.expire_month != existing.expire_month)
                    {
                        patchRequest.Add(new Patch { op = "replace", path = "/expire_month", value = creditCard.expire_month });
                    }

                    if (creditCard.expire_year != existing.expire_year)
                    {
                        patchRequest.Add(new Patch { op = "replace", path = "/expire_year", value = creditCard.expire_year });
                    }

                    if (!string.IsNullOrEmpty(creditCard.first_name) && creditCard.first_name != existing.first_name)
                    {
                        patchRequest.Add(new Patch { op = "replace", path = "/first_name", value = creditCard.first_name });
                    }

                    if (!string.IsNullOrEmpty(creditCard.last_name) && creditCard.last_name != existing.last_name)
                    {
                        patchRequest.Add(new Patch { op = "replace", path = "/last_name", value = creditCard.last_name });
                    }

                    if (patchRequest.Count > 0)
                    {
                        existing.Update(apiContext, patchRequest);

                        TempData["success"] = "Stored Card updated.";

                        return RedirectToAction("Details", new { id });
                    }
                    else
                    {
                        TempData["info"] = "Nothing to update";
                    }
                }
            }
            else
            {
                TempData["info"] = "ModelState is invalid";
            }

            AddPaymentDropdowns();
            return View(creditCard);
        }