Getting Started with the PayPal REST API SDK for .Net

date_range  04 February 2014

.Net Development
Tutorial
ASP.Net

First of all, lets pick up the package from NuGet. The one you are looking for is “REST API SDK for .NET” in the package manager. This should install two assemblies for you; “PayPalCoreSDK” and “RestAPISDK”.

Next would it would be a good idea to get your API credentials setup at developer.paypal.com if you haven’t already. Once you’ve created an app the are under the API Credentials tab. For testing make sure you are using the Sandbox credentials.

Now for some code. For starters, there is a totally undocumented configuration section that is required to access the API object that if missing will produce the error Cannot parse *.Config file. Ensure you have configured the ‘paypal’ section correctly. You will need to add this to your root web.config.

You will also need to store your ClientId and Secret Token somewhere else in the web.config, I just used appSettings for this. You can now create an OAuthTokenCredential for the APIContext.

Here I have an example Payment Service class that wraps calls for paying for Order objects.

A controller would first call the CreatePayment method, the cancelUrl being an action that PayPal will redirect the user to if the click the link to not approve the payment. The returnUrl is the URL PayPal will send the user to if the payment is approved so that the application can confirm the payment and tell PayPal to transfer the funds.

The Payment object returned CreatePayment method has an id property that should be saved with the order so that it can be retrieved when the returnUrl is followed and the payment is executed/confirmed.

Here’s another example of a controller that does just that.

comments powered by Disqus

chevron_left Archive