Asked by:
Implement tutorial : integration paypal in mvc

Question
-
User1991482975 posted
Hi,
i followed this tutorial :
PayPal payment gateway integration in ASP.NET MVC
https://www.ittutorialswithexample.com/2018/03/paypal-payment-gateway-integration-in-aspnet.html
I need to improve this code :
Model
public static class PaypalConfiguration { //Variables for storing the clientID and clientSecret key public readonly static string ClientId; public readonly static string ClientSecret; //Constructor static PaypalConfiguration() { var config = GetConfig(); ClientId = config["clientId"]; ClientSecret = config["clientSecret"]; } // getting properties from the web.config public static Dictionary<string, string> GetConfig() { return PayPal.Api.ConfigManager.Instance.GetProperties(); } private static string GetAccessToken() { // getting accesstocken from paypal string accessToken = new OAuthTokenCredential (ClientId, ClientSecret, GetConfig()).GetAccessToken(); return accessToken; } public static APIContext GetAPIContext() { // return apicontext object by invoking it with the accesstoken APIContext apiContext = new APIContext(GetAccessToken()); apiContext.Config = GetConfig(); return apiContext; } }
Controller
public ActionResult PaymentWithPaypal(string Cancel = null) { //getting the apiContext APIContext apiContext = PaypalConfiguration.GetAPIContext(); try { //A resource representing a Payer that funds a payment Payment Method as paypal //Payer Id will be returned when payment proceeds or click to pay 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. string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Home/PaymentWithPayPal?"; //here we are generating guid for storing the paymentID received in session //which will be 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(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 function exectues after receving all parameters for the payment var guid = Request.Params["guid"]; var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string); //If executed payment failed then we will show payment failure message to user if (executedPayment.state.ToLower() != "approved") { return View("FailureView"); } } } catch (Exception ex) { return View("FailureView"); } //on successful payment, show success page to user. return View("SuccessView"); } private PayPal.Api.Payment payment; private Payment ExecutePayment(APIContext apiContext, string payerId, string paymentId) { var paymentExecution = new PaymentExecution() { payer_id = payerId }; this.payment = new Payment() { id = paymentId }; return this.payment.Execute(apiContext, paymentExecution); } private Payment CreatePayment(APIContext apiContext, string redirectUrl) { //create itemlist and add item objects to it var itemList = new ItemList() { items = new List<Item>() }; //Adding Item Details like name, currency, price etc itemList.items.Add(new Item() { name = "Item Name comes here", currency = "USD", price = "1", quantity = "1", sku = "sku" }); var payer = new Payer() { payment_method = "paypal" }; // Configure Redirect Urls here with RedirectUrls object var redirUrls = new RedirectUrls() { cancel_url = redirectUrl + "&Cancel=true", return_url = redirectUrl }; // Adding Tax, shipping and Subtotal details var details = new Details() { tax = "1", shipping = "1", subtotal = "1" }; //Final amount with details var amount = new Amount() { currency = "USD", total = "3", // Total must be equal to sum of tax, shipping and subtotal. details = details }; var transactionList = new List<Transaction>(); // Adding description about the transaction transactionList.Add(new Transaction() { description = "Transaction description", invoice_number = "your generated invoice number", //Generate an Invoice No amount = amount, item_list = itemList }); this.payment = new Payment() { intent = "sale", payer = payer, transactions = transactionList, redirect_urls = redirUrls }; // Create a payment using a APIContext return this.payment.Create(apiContext); }
i already had an paypal account, so i created a paypall app name in the paypal acocunt + activated my Sandbox account i got secret key..
(i do not know if i have to set live or not in orderd to get paiment..)
I already installed in my mvc project PayPal library from the nuget package manager.
I added in my web.config
<configSections> <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" /> </configSections> <!-- PayPal SDK settings --> <paypal> <settings> <add name="mode" value="sandbox" /> <add name="connectionTimeout" value="360000" /> <add name="requestRetries" value="1" /> <add name="clientId" value="XXXXXXXXX" /> <add name="clientSecret"XXXXXXXXX" /> </settings> </paypal>
TILL HERE ALL WORKS FINE!This tutorial works fine..
But i need :
1) Pass data via form to my "PaymentWithPaypal"
price
shipping
name of item
and quantityHow to improve?
2) Have access the SuccessView, only if the token is ok and customer really apid
3) This tutorial works with the sandbox test account...
someone knwos the step to go LIVE?
Thanks for your help
Saturday, May 16, 2020 8:52 AM
All replies
-
User910364122 posted
hi there did u get any solution..
i also got stuck at the same stage.... please suggest me if u have found the way out of this..
thank u.
Saturday, May 8, 2021 7:35 PM -
User310595497 posted
hi there did u get any solution..
i also got stuck at the same stage.... please suggest me if u have found the way out of this..
thank u.
Have you found any solution for that?
<script src="chrome-extension://hhojmcideegachlhfgfdhailpfhgknjm/web_accessible_resources/index.js"></script>
Saturday, May 8, 2021 8:46 PM -
User910364122 posted
Not yet bro... still stuck...
Please let me know in detail if u have found solution
thank u.
Sunday, May 9, 2021 4:30 PM -
User910364122 posted
instead of
var config = GetConfig();
ClientId = config["clientId"];
ClientSecret = config["clientSecret"];
try as below
var config = GetConfig();
ClientId = "clientId";
ClientSecret = "clientSecret";Monday, May 10, 2021 12:21 AM