New PayPal Helper for WebMatrix

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?

The helper we are shipping today, provides methods for the Button Manager API and Adaptive Payments API which are two of PayPal’s most popular APIs.  Here’s what PayPal says about both:

  • 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
  1. Download and install WebMatrix
  2. Download the assemblies for the Helper (PayPal.dll, paypal_base.dll, log4net.dll)
  3. Create a new website or template in WebMatrix
  4. Create a bin folder off the root
  5. Copy and paste the assemblies into the bin
  6. 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:

PayPal.Profile.InitializeBMProfile(
           “[API Username]“,
           “[API Password]“,
           “[API Secret]“,
           “sandbox”);

 

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.

var payPalButton = PayPal.AddToCartButton.Create(
                                           Business : “[insert your sandbox account]“,
                                           ItemName : name,
                                           Amount : price.ToString(“N2″));

 

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:

image

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 Smile

First up you’ll need to Initialize the profile, just like we did for Button Manager.  Here’s the code for that:

PayPal.Profile.InitializeFNProfile(               
       “ThreeToken”,
       “[API Username]“,
       “[API Password]“,
       “[API Key]“,
       “SOAP11″,
       “SOAP11″,
       “https://svcs.sandbox.paypal.com/”,
       “[App ID]“,
       “[PayPal User Email Address]“
   );

 

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.

var supplierAmount = decimal.Round(product.Price * .10m, 2);
          
        var MyStore = new Receiver();
       MyStore.amount = product.Price;
       MyStore.email = “[Store Email]“;
        
        var Suppliers = new List<Receiver>();
       var Supplier = new Receiver();
       Supplier.email = “[Supplier Email]“;
       Supplier.amount = supplierAmount;
       Suppliers.Add(Supplier);

       PayPal.ChainedPay.Language = “en_US”;
       PayPal.ChainedPay.CancelUrl = “http://www.mystore.com/ohtoobad.cshtml”;
       PayPal.ChainedPay.ReturnUrl = “http://www.mystore.com/thanks.cshtml”;
       PayPal.ChainedPay.CurrencyCode = “USD”;
      �
       var response = PayPal.ChainedPay.Execute
           (MyStore, Suppliers, “”, “Test Payment”, “127.0.0.1″, “MyDevice”);
      �
       if (response == null) {
           Response.Write(“null”);
       }
       else
       {
           Response.Write(“full”);
       }   
      �
       var payKey = response.payKey;
       var redirectUrl = “https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=” + payKey;           
      �
       Response.Redirect(redirectUrl);

 

This is what the consumer sees – note it’s just one line item to James’ test store.

image

Thanks to Chained Pay, the store sees this in it’s transaction log.  Note that our supplier has been paid automatically.

image

Open source

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.

Future work

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.

  • http://www.codersbarn.com Anthony Grace

    How about Web Forms or regular ASP.NET MVC, will this work there? Fail to see the reason for using WebMatrix…

  • camus

    nice

  • rtpHarry

    Hey, this article is featured on the homepage of http://www.asp.net today so I thought I would point out the following seeing as you can expect a lot of readers:

    – First set of bullet points overflow off edge of screen in FF and IE
    – There is a placeholder note for you to insert an image “[insrt screenshot of button manager]”
    – Missing link in “Open Source” section under “You can download it here.”

    Hope this feedback is useful.

  • http://www.jamessenior.com James

    Thanks Harry – not sure what happened with the styling, but that’s now fixed. The other items were down to lack of sleep :) also fixed.

  • http://www.jamessenior.com James

    The Helper works in ASP.NET MVC 3 with the new Razor View Engine. If you fail to see the reason for using WebMatrix… then don’t use it :)

  • http://www.getfrogworks.com computer support alexandria va

    This really answered my problem, thank you!

  • http://www.albanysuperstorage.com storage in albany ny

    Good article , thanks and we want more! Added to FeedBurner as well

  • Olivier Alessandri

    Hi

    I use your Paypal helper : button API for Subscribe.
    How can retrieve information from the purchase of the subscription to store in database and activate the associated services?
    I know the IPN but how your helper can we do?
    Do you have any examples?