Simple way to integrate PayPal into your C# web app - Part 1
The purpose of this post is to provide a crash course in PayPal integration. Simply put, if your web application is close to being released to the public and you're looking for a fast-and-safe way to provide your customers with an option for purchasing your services, then this blog post might be what you need.
Integrating PayPal with a web application can be anywhere from amazingly simple to frustratingly complex, depending on what services you are planning to use. To this end, I'm going to illustrate a quick and easy way to integrate a PayPal Express payment system into a C# application.
For a start, you'll need to register with PayPal. To do this, access https://sandbox.paypal.com and create a free development account. This will be your master account, with 2 additional accounts being required for testing: a test-buyer-account and a test-merchant-account. It's important to use valid email addresses, as you might want to verify their inboxes for confirmation of payment messages.
Your master account allows you to monitor transactions between the 2 test accounts, ensuring that the payment is handled correctly. It also offers a central management console for all your test accounts. Keep in mind that you'll have to add some "money" to the buyer's virtual wallet (this is all test data). No money changes hands until you switch the system from sandbox to production. By that time, however, you're on the receiving end, so that shouldn't be a problem.
Once you have these little administrative tasks over and done with, we can get started with the actual integration. Here's a basic check list of what we'll need to cover in order to get the payment system up and running:
A. Understand the purchase data flow, to and from PayPal.
B. Create a PayPal control in the checkout page, and the logic behind sending and collecting data from PayPal.
C. Process the PayPal response, to acknowledge confirmation of payment for your products / receiving the 2 buyer specific tokens.
D. Pass the received tokens back to PayPal for purchase approval.
E. Simple error handling.
A. Payment flow
The basic flow is relatively simple: You pass data to PayPal via a your web app's checkout page (such as the merchant ID that will receive the payment, product name and description, the amount, currency, tax, return URL on confirmation or cancel and so on). The buyer is then taken to the PayPal payment portal and a payment confirmation is requested.
Once payment is accepted by your customer, the PayPal portal takes the user back to the redirect URL specified in the call and sends back the transaction tokens and PayerID.
These tokens do not represent acknowledgement of payment, however. Both are returned even if the user cancels the operation, but in that case, your application distinguishes between user actions via the return URL.
If the payment is confirmed on the client side, you'll use the 2 tokens to request the actual payment once the order has been successfully submitted on your side. This way, the system is notified that the user actually went through the correct purchase flow on your side (and this minimizes the risk of fraud, both on the vendor and buyer sides).
As a small visual aid to illustrate the flow, have a look at a simple diagram of the process:
In the next article we'll pick up with the second item on our check list, namely adding a PayPal control to a checkout page, so don't forget to also check this out.
Other posts in this series:
Simple way to integrate PayPal into your C# web app - Part 2
Simple way to integrate PayPal into your C# web app - Part 3
No comments yet.