Skip to content

Instantly share code, notes, and snippets.

@soapdog
Created May 2, 2012 02:30
Show Gist options
  • Save soapdog/2573122 to your computer and use it in GitHub Desktop.
Save soapdog/2573122 to your computer and use it in GitHub Desktop.
RevIPN ipn.inc
<?rev
/*
* PayPal Instant Payment Notification Include
* by
* Andre Garzia ([email protected])
*/
local _IPN
--> Debug routines
on debug pMsg
local tBuf
if getPref("Debug") is true then
# Generates a nice HTML Debug report
put "<h3>" & the internet date & "</h3>" & cr after tBuf
put "<h1>" & pMsg & "</h1>" & cr after tBuf
put "<h3>Server</h3>" & cr after tBuf
#put format("<p><strong>%Raw Post:</strong> %s</p>\n", $_POST_RAW) after tBuf
repeat for each key tKey in $_SERVER
put format("<p><strong>%s:</strong> %s</p>\n", tKey, $_SERVER[tKey]) after tBuf
end repeat
put "<h3>_IPN</h3>" & cr after tBuf
repeat for each key tKey in _IPN
put format("<p><strong>%s:</strong> %s</p>\n", tKey, _IPN[tKey]) after tBuf
end repeat
put "<h3>$_GET</h3>" & cr after tBuf
repeat for each key tKey in $_GET
put format("<p><strong>%s:</strong> %s</p>\n", tKey, $_GET[tKey]) after tBuf
end repeat
put "<h3>$_POST</h3>" & cr after tBuf
repeat for each key tKey in $_POST
put format("<p><strong>%s:</strong> %s</p>\n", tKey, $_POST[tKey]) after tBuf
end repeat
put "<hr/>" & cr after tBuf
put tBuf before url "binfile:debug.html"
put pMsg
end if
end debug
--> Simple routines for setting and getting preferences
on setPref pVar, pVal
put pVal into _IPN[pVar]
end setPref
function getPref pVar
return _IPN[pVar]
end getPref
--> Database routines
function DatabasePreferencesLoaded
switch
getPref("Database host") is empty
getPref("Database db") is empty
getPref("Database username") is empty
getPref("Database password") is empty
getPref("Database type") is empty
return false
break
end switch
return true
end DatabasePreferencesLoaded
on ConnectToDatabase
if DatabasePreferencesLoaded() then
get revOpenDatabase(getPref("Database type"), getPref("Database host"), getPref("Database db"), getPref("Database username"), getPref("Database password"))
if it is a number then
put it into _IPN["Database id"]
else
debug "Error: can't connect to database."
end if
else
debug "Error: database not set."
end if
end ConnectToDatabase
on DisconnectFromDatabase
if _IPN["Database id"] is in RevOpenDatabases() then
revCloseDatabase _IPN["Database id"]
put empty into _IPN["Database id"]
end if
end DisconnectFromDatabase
--> Basic IPN transaction database routines
function PayPalDateToMySQLDate pDate
replace comma with empty in pDate
replace "." with empty in pDate
put word 1 of pDate into tTime
put "01" into tYearA["Jan"]
put "02" into tYearA["Feb"]
put "03" into tYearA["Mar"]
put "04" into tYearA["Apr"]
put "05" into tYearA["May"]
put "06" into tYearA["Jun"]
put "07" into tYearA["Jul"]
put "08" into tYearA["Aug"]
put "09" into tYearA["Sep"]
put "10" into tYearA["Oct"]
put "11" into tYearA["Nov"]
put "12" into tYearA["Dec"]
get tYearA[word 2 of pDate]
put word 4 of pDate & "-" & it & "-" & word 3 of pDate into tPaymentDate
put tPaymentDate && tTime into tPaymentDate
return tPaymentDate
end PayPalDateToMySQLDate
on newTransaction pTxn_id
put PayPalDateToMySQLDate($_POST["payment_date"]) into tPaymentDate
RevExecuteSQL _IPN["Database id"], "REPLACE INTO ipn_transactions(id, status, payment_date) VALUES (:1, 'unverified', :2)", "pTxn_id", "tPaymentDate"
RevExecuteSQL _IPN["Database id"], "UPDATE ipn_transactions(payment_date) VALUES (:2) WHERE id = :1", "pTxn_id", "tPaymentDate"
if the result is not a number then
debug "Error: error creating transaction:" && the result
end if
put pTxn_id into _IPN["Transaction id"]
end newTransaction
on setTransactionVar pVar, pValue
put _IPN["Transaction id"] into tTransactionId
RevExecuteSQL _IPN["Database id"], "REPLACE INTO ipn_transactions_vars(t_id, name, value) VALUES (:1, :2, :3);", "tTransactionId", "pVar", "pValue"
if the result is not a number then
debug "Error: error setting transaction variable:" && the result
end if
end setTransactionVar
on setTransactionStatus pStatus
put _IPN["Transaction id"] into tTransactionId
RevExecuteSQL _IPN["Database id"], "UPDATE ipn_transactions(status) VALUES(:2) WHERE id = :1", "tTransactionId", "pStatus"
if the result is not a number then
debug "Error: error updating transaction status:" && the result
end if
end setTransactionStatus
function getTransactions
local tTransactions, tTransactionId, tTransactionsA
put RevDataFromQuery(cr, comma, _IPN["Database id"], "SELECT id FROM ipn_transactions ORDER BY payment_date ASC") into tTransactions
repeat with x = 1 to the number of items in tTransactions
put item x of tTransactions into tTransactionId
put RevDataFromQuery(cr, comma, _IPN["Database id"], "SELECT status, date FROM ipn_transactions WHERE id = :1", "tTransactionId") into tTransactionInformation
put line 1 of tTransactionInformation into tTransactionsA[x]["Status"]
put line 2 of tTransactionInformation into tTransactionsA[x]["Date"]
put RevDataFromQuery(cr, tab, _IPN["Database id"], "SELECT name, value FROM ipn_transactions_vars WHERE t_id = :1", "tTransactionId") into tTransactionData
split tTransactionData by tab and cr
put tTransactionData into tTransactionsA[x]["Data"]
end repeat
return tTransactionsA
end getTransactions
--> IPN routines
function isTheTransactionForMe
switch
case $_POST["receiver_email"] is not getPref("PayPal email")
case $_POST["receiver_id"] is not getPref("PayPal id")
return false
break
end switch
return true
end isTheTransactionForMe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment