[2.x] Has anyone interfaced with PayPal IPN using Scala?

482 views
Skip to first unread message

Michael Slinn

unread,
Dec 24, 2012, 6:48:32 PM12/24/12
to play-fr...@googlegroups.com
If so, I'd appreciate looking at any code you'd like to share that interfaces PayPal IPN with Play 2.x and Scala. The best example I have found so far is:

http://stackoverflow.com/questions/12711713/how-to-implement-a-paypal-ipn-controller-in-java-using-play-2-0

If you have a simple standalone Play 2.x app that handles the IPN callback and reports and/or persists the data fields that would be ideal. I guess one day there will eventually be a module for this.

Thanks,

Mike

Will Sargent

unread,
Dec 25, 2012, 1:23:53 AM12/25/12
to play-fr...@googlegroups.com
I've used Paypal IPN, but in Ruby, not Scala. It's not all that bad,
but it is annoyingly tedious. There is a PDF that tells you about the
API, but ultimately the best way to play with it is to get a Paypal
developer account and see what the actual output is.

Will.
> --
>
>

Mike Slinn

unread,
Dec 25, 2012, 1:42:05 AM12/25/12
to play-fr...@googlegroups.com, Will Sargent
Thanks, Will.

I wrote a store based on PayPal IPN a few years ago in PHP. I am
converting it to Scala. Not a lot of fun, but I'm grinding through it.

Mike

Mike Slinn

unread,
Dec 26, 2012, 2:12:02 PM12/26/12
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
Alejandro,

Nice to see this in Scala.

Looks like you are using a custom field for something, and a Purchase
(whatever that is) is inserted into the database somehow before the IPN
is called by PayPal. I've also got a custom field, but it is static, so
the Completed logic would differ and there would be no Purchase. I'll
play with the remaining code; the task for me would be to create a
PaypalTransaction, along the lines of what Will does. Here is my
untested version of a chunk of his Java code, which I will convert to Scala:

Map<String, List<String>> dataMap1 = BodyParserUtils.parseTextAsFormUrlEncodedForJava(request().body().asText());
Map<String, String> dataMap = new HashMap<String, String>();
for (String key : dataMap1.keySet())
dataMap.put(key, dataMap1.get(key).get(0));
PaypalTransaction tx = new PaypalTransaction(dataMap);
if (StringUtils.equals(result, "VERIFIED"))
tx.processTransaction();

Thanks,

Mike

Jorge Aliss

unread,
Dec 26, 2012, 2:23:58 PM12/26/12
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
I implemented IPN once.  Keep in mind you can get a confirmation from PayPal twice.  One after the user buys your product and is redirected back to your site, and the other as a post to your IPN endpoint.  Make sure you use the custom field to identify the transaction and avoid recording it twice :)

Jorge




--



Mike Slinn

unread,
Dec 26, 2012, 6:05:19 PM12/26/12
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
Alejandro,

I've been noodling on your gist:

https://gist.github.com/4382183

HttpClient is no longer required. Still have not tested it yet...

Mike

Mike Slinn

unread,
Dec 27, 2012, 12:51:38 AM12/27/12
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
This part is just standard Scala, and reads the contents of resource
pointed to by a URL as a string:

val response = io.Source.fromURL(url + queryString).mkString.trim

I am still working on PaypalTransaction. It has >22 fields so Slick
needs special handling in order to persist it. I pasted my work in
progress into the gist, in case anyone has suggestions.

Mike

Michael Slinn

unread,
Dec 31, 2012, 8:13:05 PM12/31/12
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
I updated the Paypal controller gist. It now uses a synchronous POST for the IPN verification instead of the GET performed by io.Source.fromURL. I also moved away from the raw Action parser so the incoming post fields could be accessed more easily.

https://gist.github.com/4382183

I did not update the other file in the gist because it still has issues with Slick.

Mike

Michael Slinn

unread,
Mar 5, 2013, 8:42:41 AM3/5/13
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
I wonder how much work it would be to port the PayPal module for LiftWeb to Play?

Mike

Michael Slinn

unread,
Mar 5, 2013, 9:39:10 AM3/5/13
to play-fr...@googlegroups.com, Alejandro Pedraza, Will Sargent
Here is the GitHub repo for the PayPal LiftWeb module https://github.com/liftmodules/paypal

Mike Slinn

unread,
Mar 5, 2013, 1:17:51 PM3/5/13
to play-fr...@googlegroups.com
The LiftWeb PayPal module does use actors for this very reason (see
requestQueue method starting on line 540):
https://github.com/liftmodules/paypal/blob/master/src/main/scala/net/liftmodules/paypal/Paypal.scala

Mike

Michael Slinn

unread,
Mar 13, 2013, 7:04:01 PM3/13/13
to play-fr...@googlegroups.com
I started porting the LiftWeb PayPal module to Play 2.1/Scala. It is still a work in progress. If anyone would like to help out, a 2nd pair of eyes would be welcome.

Mike
Reply all
Reply to author
Forward
0 new messages