node-payments
Version:
node-payments is a express based facade to multiple payment services.
121 lines (97 loc) • 2.9 kB
text/coffeescript
querystring = require( "querystring" )
config = require( "../../lib/config" )
request = require( "request" )
_ = require( "lodash" )
module.exports = class PaypalPayment extends require( "../_base/payment" )
type: "paypal"
initialize: =>
= config.get( "defaultcurrency" )
= config.get( "paypal" )
return
setAuthentication: ( cb )=>
data =
USER: .userid
PWD: .password
SIGNATURE: .signature
cb( null, data )
return
requestProvider: ( auth, cb )=>
# construct paypal JSON object
_urls =
data =
VERSION: 98
METHOD: "SetExpressCheckout"
LOCALECODE: .localcode
PAYMENTREQUEST_0_PAYMENTACTION: "SALE"
REQCONFIRMSHIPPING: 0
NOSHIPPING: 1
ALLOWNOTE: 0
SOLUTIONTYPE: "Sole"
LANDINGPAGE: "Billing"
PAYMENTREQUEST_0_AMT:
PAYMENTREQUEST_0_CURRENCYCODE:
PAYMENTREQUEST_0_CUSTOM:
L_PAYMENTREQUEST_0_NAME0:
L_PAYMENTREQUEST_0_AMT0:
RETURNURL: _urls.success
CANCELURL: _urls.cancel
if ?
data.L_PAYMENTREQUEST_0_NUMBER0 =
if ?
data.L_PAYMENTREQUEST_0_QTY0 =
opt =
url: .endpoint
method: "POST"
form:
"send paypal payment", JSON.stringify( opt, true, 4 )
request opt, ( err, response, rbody )=>
body = querystring.parse( rbody )
if err or body?.error? or body?.ACK isnt "Success"
if err
cb( err )
else if body?.error?
cb( _.first( body?.error ) )
else
cb( body )
return
"rawProviderState", body.PAYMENTINFO_0_PAYMENTSTATUS
link = _.template( .linkTemplate, token: body.TOKEN )
"paypal payment response", body, link
cb( null, body.TOKEN, link )
return
return
executeProvider: ( token, auth, cb )=>
data =
VERSION: 98
METHOD: "DoExpressCheckoutPayment"
LOCALECODE: .localcode
PAYMENTREQUEST_0_PAYMENTACTION: "SALE"
PAYMENTREQUEST_0_AMT:
PAYMENTREQUEST_0_CURRENCYCODE:
L_PAYMENTREQUEST_0_NUMBER0: 1
L_PAYMENTREQUEST_0_NAME0:
L_PAYMENTREQUEST_0_QTY0: 1
L_PAYMENTREQUEST_0_AMT0:
token:
payerid:
opt =
url: .endpoint
method: "POST"
form:
"send paypal payment", JSON.stringify( opt, true, 4 )
request opt, ( err, response, rbody )=>
body = querystring.parse( rbody )
if err or body?.error? or body?.ACK isnt "Success"
if err
cb( err )
else if body?.error?
cb( _.first( body?.error ) )
else
cb( body )
return
"rawProviderState", body.PAYMENTINFO_0_PAYMENTSTATUS
_state = body.PAYMENTINFO_0_PAYMENTSTATUS.toUpperCase()
"EXEC RESPONSE", JSON.stringify( body, true, 4 )
cb( null, _state )
return
return