Last active
November 1, 2017 19:09
-
-
Save aviaviavi/dcc912a93e89b4e1e1d80b697b717335 to your computer and use it in GitHub Desktop.
Getting started with the DotDashPay Node SDK (payment and identification)
This file contains 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 util = require("util") | |
var ddp = require("dotdashpay") | |
ddp.platform.initialize({ | |
// We are now using real payment hardware (e.g. a Kiosk III) | |
environment: "PROCESSOR_SIMULATOR" | |
}).onInitialized(data => { | |
console.log("initialized") | |
processPayment(500) | |
}).onInitializeError(err => { | |
console.error(err, "Error initializing DDP") | |
}) | |
function processPayment(amountInCents) { | |
console.log("processing a payment for: ", amountInCents) | |
ddp.payment.listenForInteractionThenSettle({ | |
amount: amountInCents, | |
currency: "USD", | |
// a key difference here is telling DDP to get customer information from | |
// transactions originating from a mobile wallet. If no loyalty pass is | |
// found, a push notification will be sent that can trigger the enrollment | |
// flow. | |
transactionMode: "IDENTIFY_AND_PAYMENT" | |
}) | |
.onStartedInteraction(res => { | |
printResult("onStartedInteraction")(res) | |
}) | |
.onGotInteraction(res => { | |
printResult("onGotInteraction")(res) | |
if (res.customerProfile && res.customerProfile.id) { | |
console.log(`Hello, ${res.customerProfile.fullName}`) | |
} | |
}) | |
.onStartedSettling(printResult("onStartedSettling")) | |
.onSettled(res => { | |
printResult("onSettled")(res) | |
// Here we fetch the full transaction chain, which will include details of | |
// every step of the transaction the just settled. | |
ddp.payment.getTransaction({transactionChainId: res.transactionChainId}) | |
.onGotTransaction(res => { | |
printResult("onGetTransaction")(res) | |
if (res.interaction.response.customerProfile && | |
res.interaction.response.customerProfile.id) { | |
console.log(`Thanks for shopping with us ${res.interaction.response.customerProfile.fullName}!`) | |
} | |
ddp.close() | |
}).onGetTransactionError(err => { | |
printResult("onGetTransactionError")(err) | |
ddp.close() | |
}) | |
}) | |
.onListenForInteractionError(printResult("onListenForInteractionError")) | |
.onSettleError(printResult("onSettleError")) | |
} | |
function printResult(step) { | |
return result => { | |
console.log(step, "result: ", util.inspect(result, { | |
depth: 8 | |
}, 4)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment