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/). }
public ActionResult PaymentWithPaypal(PaypalViewModel pvm) { pvm = PaypalViewModel.GetSamplePayment(); //getting the apiContext as earlier APIContext apiContext = PayPalConfig.GetAPIContext(); try { string payerId = Request.Params["PayerID"]; if (string.IsNullOrEmpty(payerId)) { //this section will be executed first because PayerID doesn't exist //it is returned by the create function call of the payment class // Creating a payment // baseURL is the url on which paypal sendsback the data. // So we have provided URL of this controller only // string baseURI = Request.Url.GetLeftPart(UriPartial.Authority) // Request.Url.Scheme + "://" + Request.Url.Authority + "/" + Url.Action("PaymentWithPayPal", "Paypal", new { area = "OrderFramework" }) + "?"; //guid we are generating for storing the paymentID received in session //after calling the create function and it is used in the payment execution var guid = Convert.ToString((new Random()).Next(100000)); //CreatePayment function gives us the payment approval url //on which payer is redirected for paypal account payment var createdPayment = this.CreatePayment(pvm, apiContext, baseURI + "guid=" + guid); //get links returned from paypal in response to Create function call var links = createdPayment.links.GetEnumerator(); string paypalRedirectUrl = null; while (links.MoveNext()) { Links lnk = links.Current; if (lnk.rel.ToLower().Trim().Equals("approval_url")) { //saving the payapalredirect URL to which user will be redirected for payment paypalRedirectUrl = lnk.href; } } // saving the paymentID in the key guid Session.Add(guid, createdPayment.id); return(Redirect(paypalRedirectUrl)); } else { // This section is executed when we have received all the payments parameters // from the previous call to the function Create // Executing a payment var guid = Request.Params["guid"]; var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string); if (executedPayment.state.ToLower() != "approved") { return(Content("Failed")); } } } catch (Exception ex) { //Logger.log("Error" + ex.Message); //return View("FailureView"); return(Content("Failed")); } //return View("SuccessView"); return(Content("Success")); }