Using Paypal’s SDK with .Net Core (FULL EXPLANATION)
Hi everybody! Today I’d like to write about one of my last tasks I’ve been working on, trying to integrate the Paypal SDK with an E-commerce project at work.
Even tough the SDK itself is just utterly amazing and simple, it took me some time to understand the whole process of made a payment.
The first problem was that the C# documentation wasn’t as good as others more popular SDKs, like Node for example. Furthermore the examples are really useless (please to be clear I’m always talking about my own experience) because they don’t describe the real process and just focus on the SDK’s methods.
After that quick introduction I’m going to try to explain how the paypal payment process works inside an .Net Core API and showing a few samples of code during the explanation.
I’m not going to focus in the front-end part because is out of the scope of this article.
THE PAYMENT PROCECES
As we can see the key concepts here are that we need two endpoints (create and execute) in our API, one to create the payment and another to execute it.
My main problem here was thinking that a payment is just the execution part…so I tried to achieved it executing the SDK directly without endpoints…a completely disaster.
CREATE A PAYPAL USER AND INSTALL THE SDK
This part is really straight forward, just create a developer account here and then grab the client ID and the secret ID for the next part.
Now we can install the SDK via the Nugget Package.
This part is the easiest one, just follow the documentation and fill the PayPal Config Settings in our config file:
First we need to create the first endpoint which creates the payment itself, in order to follow the well known best practices we need a service as well to create the payment asynchronously:
As we can see we need to take care of the result with the new payment and check if there is and approval_url inside the JSON response. If it is the case then we should redirect the client to the Paypal Login Page.
The service is quite self explanatory:
- Get the configuration from our app.
- Get the access token.
- Create an APIContext object.
- Create a Payment Object (SDK object).
- Call the Create method of the SDK (I prefer to do it inside a Task in order to make it async)
- Return the result.
Following the same process we need to create the success and cancel endpoints (actually the cancel endpoint is no strictly necessary because we can just redirect to another point of our page).
In the success’ case when the user login in Paypal and accept the payment we are going to receive a HTTP GET request with 3 parameters (paymentID, token and payerID)
The service is quite simple and just:
- Create a new APIContext.
- Create a PaymentExecution with the PayerID.
- Create a new Payment with the PaymentID.
- Execute the Payment in async mode.
If everything was well we should have a new payment in our account, in case something was wrong the API should redirect the client to the cancel endpoint.
As you can see the whole process is not quite complicated at all, mainly because of the great job done by the SDK. In my opinion, the most difficult part was understand the flow from the start to the end because I couldn’t found it neither in the huge and well document API or in the docs page.