This week I’ve been at the PayPal Innovate Conference in San Francisco where Microsoft and PayPal have been talking to thousands of developers about the work we have been doing to create the easiest way to build apps that use PayPal for payments. Today we are releasing the PayPal Helper for WebMatrix (in beta) which provides a simple way to do PayPal payments from the websites you build in WebMatrix.
What’s included in the PayPal Helper?
- Button Manager APIs let you create, manage, edit, and delete Website Payments Standard buttons. Create payment buttons that let customers purchase single or multiple items, make donations, set up recurring payments, or buy gift certificates.
- Adaptive Payments enable developers to build apps that process transactions. App builders can control the entire transaction within a single interface.
Downloading and installing the PayPal Helper
- Download and install WebMatrix
- Download the assemblies for the Helper (PayPal.dll, paypal_base.dll, log4net.dll)
- Create a new website or template in WebMatrix
- Create a bin folder off the root
- Copy and paste the assemblies into the bin
- You’re done!
PayPal Helper: Using Button Manager
Note – If you’re not familiar with the Button Manager API from PayPal, check out PayPal’s developer portal to find out more.
The first thing you need to do is initialize the helper with your PayPal account credentials. You can of course do this inline in the page, but the recommended option is to place the init call in your _AppStart.cshtml page so that it is called once when the app runs. Here’s the init call code:
Next, to create a button on the PayPal side, call the method below. When the Create AddToCart Button is fired, it returns the HTML code needed to display the button which we can then save to the database. We’ll typically use this HTML when we display the product detail page.
Now, if you login to PayPal (remember to use your Sandbox account if you have been using that) and click the “Profile” link and then “My Saved Buttons” under Selling Preferences, you will be able to see the button you have just created:
PayPal Helper: Using Adaptive Payments
Note – if you are not familiar with the Adaptive Payments service from PayPal, check out their developer portal for more information
I’m a big fan of the Adaptive Payment API because it has some cool features like Split Payments which allow you to pay other people “behind the scenes” without the customer knowing. This is great if you have suppliers that you want to pay whenever an item gets sold through your online application but you don’t want to bother the customer with the details or let them know what you are paying your suppliers
First up you’ll need to Initialize the profile, just like we did for Button Manager. Here’s the code for that:
Next, we prepare the payment by setting up a single instance of a “receiver” which will the store that is receiving the payment. After that we setup a list of receivers, which represents our suppliers (or other people that we need to pay after the transaction). Up top I’ve done a quick calculation to give the supplier 10% of the product price, of course you would typically pull this information from a database where you stored the amount you want to pay them.
Once all that is setup we then call the PayPal.ChainedPay.Execute method passing in the receivers, an empty string (this is the sender of the payment – to be specified on PayPal’s side) as well as some information on the transaction.
PayPal returns a response from which we can get a PayKey which represents the transaction on the PayPal side. We then redirect the user to PayPal’s site, passing in the PayKey in the querystring so that PayPal knows how the transaction has been setup.
This is what the consumer sees – note it’s just one line item to James’ test store.
Thanks to Chained Pay, the store sees this in it’s transaction log. Note that our supplier has been paid automatically.
The code for the helper is based on the existing PayPal Base library for .NET that PayPal ship as part of the developer SDK. This is already under the Apache license so it seemed natural to continue the tradition and release the helper in CodePlex under the Apache license. You can download it here.
As this is a beta there’s a few items that we wanted to polish up for the final version.
- Remove dependency on Log4Net
- Merge PayPal base assembly code with the new Helper assembly
- Consolidate initialization methods for Button Manager and Adaptive Payments
As always please contact me if you have other suggestions, or leave feedback on the Codeplex project site.