Skip to content

Instantly share code, notes, and snippets.

@alpeb
Forked from mslinn/PaypalController.scala
Last active December 10, 2015 04:58
Show Gist options
  • Save alpeb/4384401 to your computer and use it in GitHub Desktop.
Save alpeb/4384401 to your computer and use it in GitHub Desktop.
package controllers
import com.micronautics.paypal.PaypalTransaction
import play.api._
import play.api.mvc._
object PayPalController extends Controller {
var live = true
def url =
if (live)
"https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate&"
else
"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate&"
def ipn = Action(parse.tolerantFormUrlEncoded) {
request =>
val queryString = request.body.toString
Logger.info("Paypal request: " + queryString)
Logger.info("Verifying request: " + url + queryString)
val response = io.Source.fromURL(url + queryString).mkString.trim
if (response != "VERIFIED") {
Logger.warn("Could not verify PayPal call " + "Verification response: " + response)
BadRequest("Verification response: " + response)
} else {
if (request.body.get("payment_status").exists(_ == "Completed")) {
val reqStr = request.body.toString
val dataMap = BodyParserUtils.parseTextAsFormUrlEncodedForJava(reqStr)
new PaypalTransaction(dataMap).processTransaction()
Ok
} else {
// what situation arises to get us here?
Ok
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment