PayPal REST API With .Net

By | January 28, 2017

We are just coming out of an implementation of payment integration with PayPal for one of our customers which has proved to be a frustrating and time wasting experience.

We started by going over the documentation and examples made available on their development web site. Developed and fully tested the implementation on PayPal’s sandbox just to find out when we switched the implementation to the live environment that nothing was working. This, 3 days before our customer had schedule to go live.

Contacting their support over the phone proved to be a frustrating experience with each individual we spoke to telling us something different every time we called. The ticket we submitted with (supposedly) their expert team we never heard back from.

So, if you find yourself in a similar bind and you are using .Net for your implementation I hope this article will send you in the right direction saving some time and hair pulling.

Something we didn’t find documented was what are the minimum required fields for credit card and PayPal payments. So, starting there for the credit card payment the following fields are required for the transaction to be accepted:

  • First name
  • Last name
  • Cvv
  • Expire month
  • Expire year
  • Card number
  • Type (visa, mastercard, etc)

Concerning the type we found out that it has to be sent in all lowercase. Below is the minimum requirement for a credit card payment.

Another area which might save you some time is getting as much information as you can when you get an exception while calling PayPal. This is another area that is poorly document and which includes non documented new exception objects. While testing the credit card payment we ran into a 400 Bad Request exception. Apparently you get this exception just for anything PayPal API doesn’t like.

Hidden in this exception is an API custom exception which contains details concerning what is failing. Below is the excerpt of the code that shows how we dug into this information.

 

To get the information out of the exception object this is what you need to to:


( (PayPal.PaymentsException)ex ).Details
( (PayPal.PaymentsException)ex ).Response
( (PayPal.PaymentsException)ex ).Request
( (PayPal.ConnectionException)ex ).Message

Another undocumented problem we ran into was another 400 exception trying to complete PayPal payments. As you know there are two parts to a PayPal payment. First you create the payment and with that you send the callback url so PayPal can call your application back with the confirmation.

When your page is called it should use the payer id and payment id to call PayPal.Api.Payment.Execute to complete the payment. Your code should look something like the below:

The problem here is that the ApiContext object contains a RequestId. If your ApiContext variable maintains state between calls then the RequestId is going to be the same used in previous payments. In our case we were using the property called PaypalAPIContext which is a static property. The API requires that the RequestId is unique in every call.

We had to add a line at the end of the payment process with PaypalAPIContext.ResetRequestId(). This stopped the 400 exception that was causing the second part of the payment to not complete.

Hope these notes will save you some time. For reasons of confidentiality we are not adding here the full code for the payment implementation.

If you have any questions feel free to contact us at info@qualtechsoftware.com

Drop us a comment or if you have any questions please contact us through our QualTech-Software Solutions or QualTechCloud Integrated Cloud Solutions customer form.

 

 

 

Leave a Reply