Exemplo n.º 1
11
        public ActionResult CreatePurchase(PurchaseInfo purchaseInfo)
        {
            try
            {
                string action = "Index";

                var payer = new Payer
                {
                    payment_method = "credit_card",
                    funding_instruments = new List<FundingInstrument>(),
                    payer_info = new PayerInfo
                    {
                        email = "*****@*****.**"
                    }
                };

                var creditCard = new CreditCard();

                if (!string.IsNullOrEmpty(purchaseInfo.CreditCardId))
                {
                    payer.funding_instruments.Add(new FundingInstrument()
                    {
                        credit_card_token = new CreditCardToken()
                        {
                            credit_card_id = purchaseInfo.CreditCardId
                        }
                    });
                }
                else
                {
                    creditCard = new CreditCard()
                    {
                        billing_address = new Address()
                        {
                            city = "Orlando",
                            country_code = "US",
                            line1 = "123 Test Way",
                            postal_code = "32803",
                            state = "FL"
                        },
                        cvv2 = purchaseInfo.CVV2,
                        expire_month = purchaseInfo.ExpMonth,
                        expire_year = purchaseInfo.ExpYear,
                        first_name = purchaseInfo.FirstName,
                        last_name = purchaseInfo.LastName,
                        number = purchaseInfo.CreditCardNumber,
                        type = Common.GetCardType(purchaseInfo.CreditCardNumber)
                    };

                    payer.funding_instruments.Add(new FundingInstrument()
                    {
                        credit_card = creditCard
                    });
                }

                if (!purchaseInfo.IsRecurring)
                {
                    var transaction = new Transaction
                    {
                        amount = new Amount
                        {
                            currency = "USD",
                            total = purchaseInfo.Amount.ToString()
                        },
                        description = "Featured Profile on ProductionHUB",
                        invoice_number = Common.GetRandomInvoiceNumber()
                    };

                    var payment = new Payment()
                    {
                        intent = "sale",
                        payer = payer,
                        transactions = new List<Transaction>() { transaction }
                    };

                    var createdPayment = payment.Create(apiContext);
                    TempData["info"] = createdPayment.id;

                    if (createdPayment.state == "approved")
                    {
                        action = "Completed";
                    }
                    else
                    {
                        action = "Rejected";
                    }
                }
                else
                {
                    var agreement = new Agreement()
                    {
                        name = "Basic profile",
                        description = "Monthly basic profile in perpetuity",
                        payer = payer,
                        plan = new Plan { id = purchaseInfo.BillingPlanId },
                        start_date = DateTime.UtcNow.AddDays(1).ToString("u").Replace(" ", "T"),
                    };

                    var createdAgreement = agreement.Create(apiContext);

                    TempData["info"] = createdAgreement.create_time;

                    TempData["success"] = "Recurring agreement created";
                }

                if (purchaseInfo.SavePaymentInfo)
                {
                    creditCard.external_customer_id = customerId;
                    creditCard.Create(apiContext);
                }

                return RedirectToAction(action);

            }
            catch (Exception exc)
            {
                TempData["error"] = exc.Message;
            }

            ViewBag.Cards = CreditCard.List(apiContext, externalCustomerId: customerId);
            ViewBag.Plans = plans;
            AddPaymentDropdowns();
            return View();
        }
        public static Agreement CreateBillingAgreement(string planId, ShippingAddress shippingAddress, 
            string name, string description, DateTime startDate)
        {
            // PayPal Authentication tokens
            var apiContext = PayPalConfiguration.GetAPIContext();

            var agreement = new Agreement()
            {
                name = name,
                description = description,
                start_date = startDate.ToString("yyyy-MM-ddTHH:mm:ss") + "Z",
                payer = new Payer() { payment_method = "paypal" },
                plan = new Plan() { id = planId },
                shipping_address = shippingAddress
            };
            
            var createdAgreement = agreement.Create(apiContext);
            return createdAgreement;
        }
        public static void ReactivateBillingAgreement(string agreementId)
        {
            var apiContext = PayPalConfiguration.GetAPIContext();

            var agreement = new Agreement() { id = agreementId };
            agreement.ReActivate(apiContext, new AgreementStateDescriptor()
                { note = "Reactivating the agreement" });
        }
 /// <summary>
 /// Execute a billing agreement after buyer approval by passing the payment token to the request URI.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <returns>Agreement</returns>
 public Agreement Execute(APIContext apiContext)
 {
     return(Agreement.Execute(apiContext, this.token));
 }
 /// <summary>
 /// Create a new billing agreement by passing the details for the agreement, including the name, description, start date, payer, and billing plan in the request JSON.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <returns>Agreement</returns>
 public Agreement Create(APIContext apiContext)
 {
     return(Agreement.Create(apiContext, this));
 }
        /// <summary>
        /// 
        /// </summary>
        private void CreateBillingAgreement(APIContext apiContext)
        {
            // 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;

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Create new billing plan", plan);
            #endregion

            var createdPlan = plan.Create(apiContext);

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordResponse(createdPlan);
            #endregion

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

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Update the plan", patchRequest);
            #endregion

            createdPlan.Update(apiContext, patchRequest);

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordActionSuccess("Plan updated successfully");
            #endregion

            // 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 = "2016-02-19T00:37:04Z",
                payer = payer,
                plan = new Plan() { id = createdPlan.id },
                shipping_address = shippingAddress
            };

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Create billing agreement", agreement);
            #endregion

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

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordResponse(createdAgreement);
            #endregion

            // 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())
            {
                var link = links.Current;
                if (link.rel.ToLower().Trim().Equals("approval_url"))
                {
                    this.flow.RecordRedirectUrl("Redirect to PayPal to approve billing agreement...", link.href);
                }
            }
            Session.Add("flow-" + guid, this.flow);
            Session.Add(guid, createdAgreement.token);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Bill an outstanding amount for an agreement by passing the ID of the agreement to the request URI. In addition, pass an AgreementStateDescriptor object in the request JSON that includes a note about the reason for changing the state of the agreement and the amount and currency for the agreement.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="agreementStateDescriptor">AgreementStateDescriptor</param>
 public void BillBalance(APIContext apiContext, AgreementStateDescriptor agreementStateDescriptor)
 {
     Agreement.BillBalance(apiContext, this.id, agreementStateDescriptor);
 }
Exemplo n.º 8
0
 public void AgreementExecuteTest()
 {
     var agreement = new Agreement() { token = "EC-2CD33889A9699491E" };
     var executedAgreement = agreement.Execute(TestingUtil.GetApiContext());
     Assert.AreEqual("I-ASXCM9U5MJJV", executedAgreement.id);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Reactivate a suspended billing agreement by passing the ID of the agreement to the appropriate URI. In addition, pass an AgreementStateDescriptor object in the request JSON that includes a note about the reason for changing the state of the agreement and the amount and currency for the agreement.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="agreementStateDescriptor">AgreementStateDescriptor</param>
 public void ReActivate(APIContext apiContext, AgreementStateDescriptor agreementStateDescriptor)
 {
     Agreement.ReActivate(apiContext, this.id, agreementStateDescriptor);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Cancel a billing agreement by passing the ID of the agreement to the request URI. In addition, pass an agreement_state_descriptor object in the request JSON that includes a note about the reason for changing the state of the agreement and the amount and currency for the agreement.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="agreementStateDescriptor">AgreementStateDescriptor</param>
 public void Cancel(APIContext apiContext, AgreementStateDescriptor agreementStateDescriptor)
 {
     Agreement.Cancel(apiContext, this.id, agreementStateDescriptor);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Suspend a particular billing agreement by passing the ID of the agreement to the request URI.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="agreementStateDescriptor">AgreementStateDescriptor</param>
 public void Suspend(APIContext apiContext, AgreementStateDescriptor agreementStateDescriptor)
 {
     Agreement.Suspend(apiContext, this.id, agreementStateDescriptor);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Update details of a billing agreement, such as the description, shipping address, and start date, by passing the ID of the agreement to the request URI.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="patchRequest">PatchRequest</param>
 public void Update(APIContext apiContext, PatchRequest patchRequest)
 {
     Agreement.Update(apiContext, this.id, patchRequest);
 }
        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();

            // For demonstration purposes, we'll first setup a billing plan in
            // an active state to be used to make an agreement with.
            var plan = BillingPlanCreate.CreatePlanObject(HttpContext.Current);
            var guid = Convert.ToString((new Random()).Next(100000));
            plan.merchant_preferences.return_url = Request.Url.ToString() + "?guid=" + guid;
            plan.merchant_preferences.cancel_url = Request.Url.ToString();

            // > Note: Both `return_url` and `cancel_url` are being set in
            // `plan.merchant_preferences` since many plans may be setup well
            // in advance of anyone setting up an agreement for it.  These URLs
            // are only used for agreements setup with `payer.payment_method`
            // set to `paypal` to establish where the customer should be redirected
            // once they approve or cancel the agreement using their PayPal account.

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Create new billing plan", plan);
            #endregion

            var createdPlan = plan.Create(apiContext);

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordResponse(createdPlan);
            #endregion

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

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Update the plan", patchRequest);
            #endregion

            createdPlan.Update(apiContext, patchRequest);

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordActionSuccess("Plan updated successfully");
            #endregion

            // With the plan created and activated, we can now create the billing agreement.
            // A resource representing a Payer that funds a payment.
            var payer = new Payer
            {
                payment_method = "credit_card",
                funding_instruments = new List<FundingInstrument>
                {
                    new FundingInstrument
                    {
                        credit_card = new CreditCard
                        {
                            billing_address = new Address
                            {
                                city = "Johnstown",
                                country_code = "US",
                                line1 = "52 N Main ST",
                                postal_code = "43210",
                                state = "OH"
                            },
                            cvv2 = "874",
                            expire_month = 11,
                            expire_year = 2018,
                            first_name = "Joe",
                            last_name = "Shopper",
                            number = "4877274905927862",
                            type = "visa"
                        }
                    }
                }
            };

            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
            };

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Create billing agreement", agreement);
            #endregion

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

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordResponse(createdAgreement);
            #endregion

            // For more information, please visit [PayPal Developer REST API Reference](https://developer.paypal.com/docs/api/).
        }
        public static void ExecuteBillingAgreement(string token)
        {
            // PayPal Authentication tokens
            var apiContext = PayPalConfiguration.GetAPIContext();

            var agreement = new Agreement() { token = token };
            var executedAgreement = agreement.Execute(apiContext);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Set the balance for an agreement by passing the ID of the agreement to the request URI. In addition, pass a Currency object in the request JSON that specifies the currency type and value of the balance.
 /// </summary>
 /// <param name="apiContext">APIContext used for the API call.</param>
 /// <param name="currency">Currency</param>
 public void SetBalance(APIContext apiContext, Currency currency)
 {
     Agreement.SetBalance(apiContext, this.id, currency);
 }
        public static void SuspendBillingAgreement(string agreementId)
        {
            var apiContext = PayPalConfiguration.GetAPIContext();

            var agreement = new Agreement() { id = agreementId };
            agreement.Suspend(apiContext, new AgreementStateDescriptor()
                { note = "Suspending the agreement" });
        }
Exemplo n.º 17
0
        /// <summary>
        /// Create a new billing agreement by passing the details for the agreement, including the name, description, start date, payer, and billing plan in the request JSON.
        /// </summary>
        /// <param name="apiContext">APIContext used for the API call.</param>
        /// <param name="agreement">The Agreement object to be used when creating the PayPal resource.</param>
        /// <returns>Agreement</returns>
        public static Agreement Create(APIContext apiContext, Agreement agreement)
        {
            // Validate the arguments to be used in the request
            ArgumentValidator.ValidateAndSetupAPIContext(apiContext);

            // Configure and send the request
            var resourcePath = "v1/payments/billing-agreements";
            var resource = PayPalResource.ConfigureAndExecute<Agreement>(apiContext, HttpMethod.POST, resourcePath, agreement.ConvertToJson());
            resource.token = resource.GetTokenFromApprovalUrl();
            return resource;
        }
Exemplo n.º 18
0
 public void AgreementGetTest()
 {
     var apiContext = TestingUtil.GetApiContext();
     var agreement = new Agreement() { token = "EC-2CD33889A9699491E" };
     var executedAgreement = agreement.Execute(apiContext);
     var agreementId = executedAgreement.id;
     var retrievedAgreement = Agreement.Get(apiContext, agreementId);
     Assert.AreEqual(agreementId, retrievedAgreement.id);
     Assert.AreEqual("-6514356286402072739", retrievedAgreement.description);
     Assert.AreEqual("2015-02-19T08:00:00Z", retrievedAgreement.start_date);
     Assert.IsNotNull(retrievedAgreement.plan);
 }
        /// <summary>
        /// 
        /// </summary>
        private void ExecuteBillingAgreement(APIContext apiContext, string token)
        {
            var agreement = new Agreement() { token = token };

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.AddNewRequest("Execute billing agreement", description: string.Format("URI: v1/payments/billing-agreements/{0}/agreement-execute", agreement.token));
            #endregion
            
            var executedAgreement = agreement.Execute(apiContext);

            // ^ Ignore workflow code segment
            #region Track Workflow
            this.flow.RecordResponse(executedAgreement);
            #endregion
        }