Exemple #1
0
        public ActionResult PaymentWithCreditCard(string CreditCardId = "")
        {
            var pvm = PaypalViewModel.GetSamplePayment("*****@*****.**");

            var apiContext = PayPalConfig.GetAPIContext();

            // A transaction defines the contract of a payment.
            var transaction = new Transaction()
            {
                amount = new Amount()
                {
                    currency = pvm.Transaction.Amount.Currency,
                    total    = pvm.Transaction.Amount.Total,
                    details  = new Details()
                    {
                        shipping = pvm.Transaction.Amount.Detail.Shipping,
                        subtotal = pvm.Transaction.Amount.Detail.Subtotal,
                        tax      = pvm.Transaction.Amount.Detail.Tax
                    }
                },
                description = pvm.Transaction.Description,
                item_list   = new ItemList()
                {
                    items = pvm.Transaction.ItemList.Select(s => new Item
                    {
                        name     = s.Name,
                        currency = s.Currency,
                        price    = s.Price,
                        quantity = s.Quantity,
                        sku      = s.Sku
                    }).ToList(),
                    shipping_address = new ShippingAddress
                    {
                        city           = pvm.Transaction.ShippingAddress.City,
                        country_code   = pvm.Transaction.ShippingAddress.CountryCodeDomain,
                        line1          = pvm.Transaction.ShippingAddress.AddressLine,
                        postal_code    = pvm.Transaction.ShippingAddress.PostalCode,
                        state          = pvm.Transaction.ShippingAddress.State,
                        recipient_name = pvm.Transaction.ShippingAddress.RecipientName
                    }
                },
                invoice_number = String.IsNullOrEmpty(pvm.Transaction.InvoiceNumber) ? GetRandomInvoiceNumber() : pvm.Transaction.InvoiceNumber
            };

            // A resource representing a Payer that funds a payment.
            var payer = new Payer()
            {
                payment_method = "credit_card",
                payer_info     = new PayerInfo
                {
                    email = pvm.Payer.PayerInfo.Email
                }
            };

            if (String.IsNullOrEmpty(CreditCardId))
            {
                payer.funding_instruments = new List <FundingInstrument>()
                {
                    new FundingInstrument()
                    {
                        credit_card = new CreditCard()
                        {
                            billing_address = new Address()
                            {
                                city         = pvm.Payer.FundingInstrument.CreditCard.BillingAddress.City,
                                country_code = pvm.Payer.FundingInstrument.CreditCard.BillingAddress.CountryCodeDomain,
                                line1        = pvm.Payer.FundingInstrument.CreditCard.BillingAddress.AddressLine,
                                postal_code  = pvm.Payer.FundingInstrument.CreditCard.BillingAddress.PostalCode,
                                state        = pvm.Payer.FundingInstrument.CreditCard.BillingAddress.State
                            },
                            cvv2         = pvm.Payer.FundingInstrument.CreditCard.Cvv2,
                            expire_month = pvm.Payer.FundingInstrument.CreditCard.ExpireMonth,
                            expire_year  = pvm.Payer.FundingInstrument.CreditCard.ExpireYear,
                            first_name   = pvm.Payer.FundingInstrument.CreditCard.FirstName,
                            last_name    = pvm.Payer.FundingInstrument.CreditCard.LastName,
                            number       = pvm.Payer.FundingInstrument.CreditCard.CcNumber,
                            type         = pvm.Payer.FundingInstrument.CreditCard.CcType
                        }
                    }
                };

                CreditCardId = PaypalVault.StoreCreditCardInPaypal(pvm.Payer.FundingInstrument.CreditCard);
            }
            else
            {
                //Here, we are assigning the User's Credit Card ID which we saved in Database
                payer.funding_instruments = new List <FundingInstrument>()
                {
                    new FundingInstrument()
                    {
                        credit_card_token = new CreditCardToken
                        {
                            credit_card_id = CreditCardId
                        }
                    }
                };
            }


            // A Payment resource; create one using the above types and intent as `sale` or `authorize`
            var payment = new PayPal.Api.Payment()
            {
                intent       = "sale",
                payer        = payer,
                transactions = new List <Transaction>()
                {
                    transaction
                }
            };

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

            // Create a payment using a valid APIContext
            var createdPayment = payment.Create(apiContext);

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

            if (createdPayment.state.ToLower() != "approved")
            {
                //return View("FailureView");
                return(Content("Failed"));
            }

            return(Content("Success Id: " + CreditCardId));

            // For more information, please visit [PayPal Developer REST API Reference](https://developer.paypal.com/docs/api/).
        }
Exemple #2
0
        public ActionResult GetPaypalVault(string CreditCardId)
        {
            var vault = PaypalVault.GetCreditCardDetailsFromVault(CreditCardId);

            return(Json(vault, JsonRequestBehavior.AllowGet));
        }