Introduction
This is my first ever article on CodeProject and it's about the use of Paypal REST API for payments. It's a world of eCommerce now and everyone wants to integrate Paypal in their website for accepting payments online for either their services of products they are selling. So, this article is about the Integration of PAYPAL REST API in an ASP.NET MVC 4 Framework. I am using this framework because it is a quick way to start on any good web application. I have commented my code as much as possible, so that you can understand what actually is happening in the code. Still if you are not able to understand, then you can ask me. I won't hesitate to help, if I am available.
Background
I have been searching for a good and up to date article or tutorials for the Paypal REST API usage for many days. I found some articles but they were either not very clear to understand or were mostly based on the old paypal webstandard methods. So, I decided to do it myself and finally, I have successfully used Paypal REST API in my projects for the payments and I am sharing that. I am not a PRO in web applications and I started a year ago only on websites and web apps. So, I am sorry if I missed anything.
I am using documentation references from paypal website here here.
PAYPAL REST API can be used for both payments using paypal or payments using the credit cards.
Pre-Requisites
- We need a facilitator Paypal account API credentials to be used in the web application.
- We need a buyer account. (You can get these accounts by registering on Paypal Developer website here https://developer.paypal.com for Free. So, Just register on the paypal developer website and create the test accounts to be used for testing purpose.)
- We also need Paypal SDK for .NET from here https://github.com/paypal/PayPal-NET-SDK or you can alternatively install from NuGet Package Manager (Recommended).
- A little knowledge is required to start with Visual Studio MVC 4 based project
For testing Paypal provides a Sandbox, where we can perform dummy transactions through our application.
Using the Code
Let's start.
So, first of all, open the Visual Studio IDE (I am using Visual Studio 2012) and create an ASP.NET MVC 4 project.
Now we need to install the PAYPAL SDK for .NET in our project. Let NuGet install it for you. This way, it is easier to add the SDK libraries to the project and is a recommended way especially for beginners.
So, Goto Tools -> Library Package Manager -> Package Manager Console. It will open a command console in the Visual Studio.
To add the Paypal SDK libraries in your project, use this command in the console:
Install-Package Paypal
Also, add log4net libraries using this command below using the same method as above. It is used by paypal libraries for logging.
Install-Package log4net
If the reference to the libraries are not automatically added, then you can add references to the DLLs manually by right clicking on the References in your project in Solution Explorer and Choosing Add Reference. Just browse the assembly and Select it. These are the assemblies which need to be referenced.
- PayPal.dll
- Newtonsoft.Json.dll
- log4net.dll
Basically, the SDK uses json to communicate with the Paypal API that is why it installs the latest version of newtonsoft.json
library. It installed version 6.xx.xx on my machine.
Now, we need to setup our web.config file with the configurations to be used.
So, open the web.config file and add the configurations as shown below.
Under the <configuration>
tag in the web.config file, put these settings in. Notice that we have mode set to sandbox in the paypal settings. And then, you need to configure your paypal facilitator account details inside here i.e., clientID
and clientSecret
key. Get these from your developer paypal account. (If you don't have a paypal developer account, then you need to register for an account first on the paypal developer website here https://developer.paypal.com and create a facilitator/vendor account).
Then, from the settings of that account, you can get this clientID
and clientSecret
and enter them here under the paypal settings tag.
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
-->
<paypal>
<settings>
<add name="mode" value="sandbox"/>
<add name="connectionTimeout" value="360000"/>
<add name="requestRetries" value="1"/>
<add name="clientId" value="your client ID of paypal account"/>
<add name="clientSecret" value="your client secret key of paypal account"/>
</settings>
</paypal>
-->
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="my_app.log"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date
[%thread] %-5level %logger [%property{NDC}] %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
<appSettings>
<add key="PayPalLogger" value="PayPal.Log.Log4netLogger"/>
</appSettings>
<system.web>
<trust level="High" />
</system.web>
Second, we are ready to write the C# code to make the calls to the paypal API now after adding the needed SDK libraries and adding their references in it.
So, first of all, add a controller in the controllers namespace and give any name you like to this controller. I gave PaypalController.cs.
When you add a controller, there is a default Action Index
created by the IDE for you. You need to add a new Action named PaymentWithCreditCard
.
Steps that are performed in the controller are listed below:
- Create
Payment
object with all the items and billing details
- Get access token using the
GetApiContext
call
- Call function
Create
of the payment
class
Add the action PaymentWithCreditCard
to this controller as below:
public ActionResult PaymentWithCreditCard()
{
Item item = new Item();
item.name = "Demo Item";
item.currency = "USD";
item.price = "5";
item.quantity = "1";
item.sku = "sku";
List<Item> itms = new List<Item>();
itms.Add(item);
ItemList itemList = new ItemList();
itemList.items = itms;
Address billingAddress = new Address();
billingAddress.city = "NewYork";
billingAddress.country_code = "US";
billingAddress.line1 = "23rd street kew gardens";
billingAddress.postal_code = "43210";
billingAddress.state = "NY";
CreditCard crdtCard = new CreditCard();
crdtCard.billing_address = billingAddress;
crdtCard.cvv2 = "874"; crdtCard.expire_month = 1; crdtCard.expire_year = 2020; crdtCard.first_name = "Aman";
crdtCard.last_name = "Thakur";
crdtCard.number = "1234567890123456"; crdtCard.type = "visa";
Details details = new Details();
details.shipping = "1";
details.subtotal = "5";
details.tax = "1";
Amount amnt = new Amount();
amnt.currency = "USD";
amnt.total = "7";
amnt.details = details;
Transaction tran = new Transaction();
tran.amount = amnt;
tran.description = "Description about the payment amount.";
tran.item_list = itemList;
tran.invoice_number = "your invoice number which you are generating";
List<Transaction> transactions = new List<Transaction>();
transactions.Add(tran);
FundingInstrument fundInstrument = new FundingInstrument();
fundInstrument.credit_card = crdtCard;
List<FundingInstrument> fundingInstrumentList = new List<FundingInstrument>();
fundingInstrumentList.Add(fundInstrument);
Payer payr = new Payer();
payr.funding_instruments = fundingInstrumentList;
payr.payment_method = "credit_card";
Payment pymnt = new Payment();
pymnt.intent = "sale";
pymnt.payer = payr;
pymnt.transactions = transactions;
try
{
APIContext apiContext = Configuration.GetAPIContext();
Payment createdPayment = pymnt.Create(apiContext);
if(createdPayment.state.ToLower() != "approved")
{
return View("FailureView");
}
}
catch (PayPal.PayPalException ex)
{
Logger.Log("Error: "+ex.Message);
return View("FailureView");
}
return View("SuccessView");
}
The above controller tries to make a direct payment with a credit card from your website. You can see there are multiple lists objects created above and assigned for the payment. This is the necessary data required by paypal to make the payment using Credit Cards.
Code for the configuration
class which is used in the above controller action. You need to make this class in the namespace which you want. I am adding it in Models
. Usually in Models
namespace, we keep those classes which are used to pass data to the Views. So, you can add your new namespace as well if you want and add this class to that namespace. It's all your choice.
public static class Configuration
{
public readonly static string ClientId;
public readonly static string ClientSecret;
static Configuration()
{
var config = GetConfig();
ClientId = config["clientId"];
ClientSecret = config["clientSecret"];
}
public static Dictionary<string, string> GetConfig()
{
return PayPal.Manager.ConfigManager.Instance.GetProperties();
}
private static string GetAccessToken()
{
string accessToken = new OAuthTokenCredential
(ClientId, ClientSecret, GetConfig()).GetAccessToken();
return accessToken;
}
public static APIContext GetAPIContext()
{
APIContext apiContext = new APIContext(GetAccessToken());
apiContext.Config = GetConfig();
return apiContext;
}
}
Now you can try paying on your website using a test Paypal credit card. Or you can first make the PaymentWithPaypal
action and try both of these at once like I am doing here.
Third, now we have already created a controller for the Credit Card payments above. It's time to create the controller for the Paypal Account payments using the Paypal REST API.
The procedure of making payments using paypal account is slightly different than making the payments using the credit cards directly.
Steps that are performed in the controller are listed below:
- Get the Access Token by getting the
APIContext
- Call the function
Create
of Payment
class with apiContext
- Execute
Payment
Add the Action PaymentWithPaypal
as below:
public ActionResult PaymentWithPaypal()
{
APIContext apiContext = Configuration.GetAPIContext();
try
{
string payerId = Request.Params["PayerID"];
if (string.IsNullOrEmpty(payerId))
{
string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority +
"/Paypal/PaymentWithPayPal?";
var guid = Convert.ToString((new Random()).Next(100000));
var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);
var links = createdPayment.links.GetEnumerator();
string paypalRedirectUrl = null;
while (links.MoveNext())
{
Links lnk = links.Current;
if (lnk.rel.ToLower().Trim().Equals("approval_url"))
{
paypalRedirectUrl = lnk.href;
}
}
Session.Add(guid, createdPayment.id);
return Redirect(paypalRedirectUrl);
}
else
{
var guid = Request.Params["guid"];
var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
if(executedPayment.state.ToLower() != "approved")
{
return View("FailureView");
}
}
}
catch(Exception ex)
{
Logger.log("Error"+ ex.Message);
return View("FailureView");
}
return View("SuccessView");
}
Execute payment function used in the controller PaymentWithPaypal
using a global variable named payment
. It is basically using the function Execute
of the Payment
class of Paypal SDK.
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);
}
CreatePayment
function is used in the controller PaymentWithPaypal
for making the payment. Basically, in this function, we are adding the Items
for which the payment
is being created.
private Payment CreatePayment(APIContext apiContext, string redirectUrl)
{
var itemList = new ItemList() { items = new List<Item>() };
itemList.items.Add(new Item()
{
name = "Item Name",
currency = "USD",
price = "5",
quantity = "1",
sku = "sku"
});
var payer = new Payer() { payment_method = "paypal" };
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl,
return_url = redirectUrl
};
var details = new Details()
{
tax = "1",
shipping = "1",
subtotal = "5"
};
var amount = new Amount()
{
currency = "USD",
total = "7", details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "Transaction description.",
invoice_number = "your invoice number",
amount = amount,
item_list = itemList
});
this.payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
return this.payment.Create(apiContext);
}
That's it. You are done with Paypal. Now, it's time to make the View Page.
Remember, we have an Index
action in the Paypal Controller as I mentioned above. Now, we will make a view for that page. We will put 2 links in that page and link them to the PaymentWithCreditCard
and PaymentWithPaypal
actions.
- Pay Direct with Credit Card
- Pay with Paypal Account
So, right click on Index
action in the code and select Add View.
In the View Code, add this Razor code as below. (Razor is the rendering engine and it responsible for rendering dynamic pages of an ASP.NET application.)
@Html.ActionLink("Pay Direct With Credit Card","PaymentWithCreditCard","Paypal")
@Html.ActionLink("Pay with Paypal Account", "PaymentWithPaypal", "Paypal")
Also, add the SuccessView
and FailureView
under the Paypal folder in the Views from the solution explorer. These views are returned upon the successful or failed transactions of Paypal. So, these views are used in both the actions we have made.
Now, let's test the application. Run your web application in the web browser now. And load the Paypal Index action. You just need to append the URL of the web application and add "/Paypal" at the end of that URL.
As soon as you hit enter, it will execute the Index
action because Index
is the default action of any controller in MVC. If you have a little knowledge about the routing in MVC, then you must know that the default route of the web application is URL/Controller/Action/id. We append Paypal to the URL because that is the controller name here we have made.
We have not provided any Action
name in the URL so, it will load the default action which is INDEX
and will return the View as below:
Now, click on the first option Pay Direct With Credit Card. Try using a debug breakpoint in the action PaymentWithCreditCard
, if you want to understand better. Below is the accesstoken
returned by Paypal for the credit card payment which will be used for creating the payment.
If you are encountering any assembly error, like in the below image:
Then, just add the namespace in your code file as "using PayPal.Api;
". The errors will go away. Logger
is my class which I have made for logging the exceptions. You will find the source code.
So, as soon as the transaction is finished from Paypal, you will be shown the SuccessView
.
Now, let's test the payment with Paypal. Load the same URL as before. In my case, it is localhost:6016/Paypal so check yours and load. This time, select Pay with Paypal Account and put a Breakpoint in the action PaymentWithPaypal
for better understanding.
As soon as the action gets the accesstoken
from Paypal, the action sends the URL to Paypal on which paypal will return the data. As soon as the createpayment
function is called, Paypal returns payment approval_url
of the user as seen below.
Approval_url
is the URL on which you need to redirect the user for paying.
As soon as the user authenticates and confirms the payment, the paypal will send the payerID
and related data which we can use to execute the payment via Paypal Account and you will see the successview
again.
That's it!!
If you have any queries, feel free to ask. I will help you...:)
FOR RECURRING PAYMENTS WITH PAYPAL SEE MY TIP: HERE
Points of Interest
PAYPAL REST API is more advanced than the classical methods of Paypal. It was fast learning using the REST API. So, you might enjoy integrating Paypal with REST.
History
- 30th January, 2015: Initial version