Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

[GCP] Use rawBody on IPN sample (fixes INVALID responses) #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions javascript/googlecloudfunctions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* Sample PayPal IPN Listener implemented for Google Clound Functions.
* Sample PayPal IPN Listener implemented for Google Cloud Functions.
*/

const querystring = require("querystring");
const request = require("request");

/**
Expand Down Expand Up @@ -42,10 +41,8 @@ exports.ipnHandler = function ipnHandler(req, res) {

// JSON object of the IPN message consisting of transaction details.
let ipnTransactionMessage = req.body;
// Convert JSON ipn data to a query string since Google Cloud Function does not expose raw request data.
let formUrlEncodedBody = querystring.stringify(ipnTransactionMessage);
// Build the body of the verification post message by prefixing 'cmd=_notify-validate'.
let verificationBody = `cmd=_notify-validate&${formUrlEncodedBody}`;
let verificationBody = `cmd=_notify-validate&${req.rawBody}`;

console.log(`Verifying IPN: ${verificationBody}`);

Expand All @@ -69,10 +66,10 @@ exports.ipnHandler = function ipnHandler(req, res) {
`Invalid IPN: IPN message for Transaction ID: ${ipnTransactionMessage.txn_id} is invalid.`
);
} else {
console.error("Unexpected reponse body.");
console.error("Unexpected response body.");
}
} else {
// Error occured while posting to PayPal.
// Error occurred while posting to PayPal.
console.error(error);
console.log(body);
}
Expand Down