Integrate PayPal in LiftWeb application using Scala


If you are developing an E-Business web application , where you have to provide online payment solution to your customer then PayPal is one of the most secure , faster and easier way to make payment online .

In this blog, we will have a basic idea that how easily you can integrate PayPal functionality in your Liftweb project using Scala .
We are assuming that you are aware with the basics of LiftWeb and Scala .

Lift provides integration support for both PDT(Payment Data Transfer)  and IPN(Instant Payment Notification) . You do not need to add any extra functionality to handle response from PayPal .
Payment Data Transfer :
scr_ppPDTDiagram_513x282

PDT(Payment Data Transfer) provides you some transaction details to be displayed to buyer , when page is redirected after completing transaction . To use PDT , you must enable AUTO RETURN .
IPN(Instant Payment Notification):
scr_ppIPNDiagram_555x310
Once transaction is complete , IPN(Instant Payment Notification) sends a notification to your server with all specific details about completed transaction .

PayPal Environment setup :
Before implementing PayPal in your application , you must setup PayPal environment .

  • Go To https://developer.paypal.com
  • Set up both a “preconfigured seller” and “preconfigured buyer” account in the sandbox.
  • Log into the sandbox as the seller account you just created, and enable Auto Return in the selling preferences.
  • Enable PayPal Data Transfer (it’s on the same screen as the Auto Return configuration).
  • Edit the IPN callback URL and enable IPN.

LiftWeb Environment setup :

  • Add PayPal repository in your build.sbt
    "net.liftmodules" % "paypal_2.9.2" % "2.5-SNAPSHOT-1.2-SNAPSHOT"
  • Let’s create PayPal Handler class which implements two traits PaypalIPN and PaypalPDT.
    object PaypalHandler extends PaypalIPN with PaypalPDT with Loggable {
      import PaypalTransactionStatus._
      val paypalAuthToken = "yourtokengoeshere"
      def pdtResponse = {
        case (info, resp) => info.paymentStatus match {
          case Full(CompletedPayment) =>
            DoRedirectResponse.apply("/paypal/success")
          case _ => DoRedirectResponse.apply("/paypal/failure")
        }
      }
     
      def actions = {
        case (CompletedPayment,info,_) =>
          //Update your transaction detail
        case (FailedPayment,info,_) =>
          //Update your transaction detail
        case (status, info, resp) =>
      }
    }
    
  • Now add following lines in your Boot.Scala to initialize the PaypalRules object that contains configuration .
    ...........
    PaypalRules.init
    PaypalHandler.dispatch.foreach(LiftRules.dispatch.append(_))
    ...........
    
  • Now add Buy Now button to instantiate the transaction process .
    Screenshot from 2013-03-04 20:50:20

    Compose the BuyNowSnippet with your snippet:

     
    class TrasanctionSummary extends BuyNowSnippet {
    .........
    override val values = Map(
        "business" -> "test@business.com",
        "item_number" -> reference,
        "item_name" -> ("Auction Order: " + reference))
    .................
    }
    

    Above lines are required to implement the button .

  • Finally add PayPal response pages to the SiteMap .
     
    ..............
    Menu("Transaction Complete") / "paypal" / "success"
    >> LocGroup("public") >> Hidden,
    Menu("Transaction Failure") / "paypal" / "failure"
    >> LocGroup("public") >> Hidden,
    ...........
    

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Java, LiftWeb, Scala, Web. Bookmark the permalink.

2 Responses to Integrate PayPal in LiftWeb application using Scala

  1. jose says:

    Change PaypalHandler.dispatch.foreach([LiftRules.dispatch.append(_))
    for
    PaypalHandler.dispatch.foreach(LiftRules.dispatch.append(_))

    Good article
    Thanks

  2. Hi Jose ,
    Thanks for pointing this out . We have fixed this .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s