Created
January 12, 2016 20:14
-
-
Save evaldosantos/49b8ffff57a0efb997ed to your computer and use it in GitHub Desktop.
Paypal billing agreement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
request = require('request'), | |
url = require('urlcode-json'); | |
var app = express(); | |
app.get('/gettoken', function (req, res) { | |
request.post({url:'https://api-3t.sandbox.paypal.com/nvp', form: { | |
USER: process.env.USER, | |
PWD: process.env.PWD, | |
SIGNATURE: process.env.SIGNATURE, | |
METHOD: 'SetExpressCheckout', | |
VERSION: '86', | |
PAYMENTREQUEST_0_PAYMENTACTION: 'AUTHORIZATION', | |
PAYMENTREQUEST_0_AMT: '0', | |
PAYMENTREQUEST_0_CURRENCYCODE: 'GBP', | |
L_BILLINGTYPE0: 'MerchantInitiatedBilling', | |
L_BILLINGAGREEMENTDESCRIPTION0: 'ClubUsage', | |
cancelUrl: 'http://www.yourdomain.com/cancel.html', | |
returnUrl: 'http://www.yourdomain.com/success.html' | |
}}, | |
function(err, httpResponse, body){ | |
var data = url.decode(body) | |
data['agreement_url'] = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="+data.TOKEN | |
res.send(data); | |
}); | |
}); | |
app.get('/getbillingagreement/:token', function(req, res) { | |
request.post({url:'https://api-3t.sandbox.paypal.com/nvp', form: { | |
USER: process.env.USER, | |
PWD: process.env.PWD, | |
SIGNATURE: process.env.SIGNATURE, | |
METHOD: 'CreateBillingAgreement', | |
VERSION: '86', | |
TOKEN: req.params['token'] | |
}}, | |
function(err, httpResponse, body){ | |
res.send(url.decode(body)); | |
}); | |
}); | |
app.get('/dopayment/:agreementid/:amt', function(req, res) { | |
request.post({url:'https://api-3t.sandbox.paypal.com/nvp', form: { | |
USER: process.env.USER, | |
PWD: process.env.PWD, | |
SIGNATURE: process.env.SIGNATURE, | |
METHOD: 'DoReferenceTransaction', | |
VERSION: '86', | |
AMT: req.params['amt'], | |
CURRENCYCODE: 'GBP', | |
PAYMENTACTION: 'SALE', | |
REFERENCEID: req.params['agreementid'] | |
}}, | |
function(err, httpResponse, body){ | |
res.send(url.decode(body)); | |
}); | |
}) | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}) | |
// run this way | |
// USER=your_sandbox_user PWD=your_sandbox_pwd SIGNATURE=your_sandbox_signature node app.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment