-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharge.js
118 lines (102 loc) · 4.37 KB
/
charge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
const AWS = require('aws-sdk');
const paymentHighway = require('paymenthighway');
const PaymentApi = paymentHighway.PaymentAPI;
const SecureSigner = paymentHighway.SecureSigner;
const moment = require('moment');
const account = process.env.ACCOUNT;
const merchantId = process.env.MERCHANT_ID;
const testKey = process.env.KEY;
const testSecret = process.env.SECRET;
const serviceUrl = process.env.SERVICE_URL;
const paymentApi = new PaymentApi(serviceUrl, testKey, testSecret, account, merchantId);
const secureSigner = new SecureSigner(testKey, testSecret);
const DynamoDb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
module.exports.charge = async (event) => {
const user = event.queryStringParameters.user;
const currency = 'EUR';
const webhookSuccessUrl = (process.env.URL + 'charge/webhook?user=' + user).trim();
const webhookFailureUrl = (process.env.URL + 'charge/webhook?user=' + user).trim();
const webhookCancelUrl = (process.env.URL + 'charge/webhook?user=' + user).trim();
return paymentApi.initTransaction()
.then(transactionResponse => {
const transactionId = transactionResponse.id;
const returnUrls = paymentHighway.ReturnUrls.Builder(
event.queryStringParameters.successUrl,
event.queryStringParameters.cancelUrl,
event.queryStringParameters.failureUrl
).setWebhookSuccessUrl(webhookSuccessUrl)
.setWebhookFailureUrl(webhookFailureUrl)
.setWebhookCancelUrl(webhookCancelUrl)
.build();
const strongCustomerAuthentication = paymentHighway.StrongCustomerAuthentication.Builder(returnUrls).build();
const chargeCitRequest = paymentHighway.ChargeCitRequest.Builder(
event.queryStringParameters.amount,
currency,
event.queryStringParameters.order,
strongCustomerAuthentication
)
.setToken(new paymentHighway.Token(event.queryStringParameters.token))
.build();
return paymentApi.chargeCustomerInitiatedTransaction(transactionId, chargeCitRequest)
.then(debitResponse => {
if (debitResponse.result.code === 400) {
return {
statusCode: 200,
body: JSON.stringify({
response_code: debitResponse.result.code,
three_d_secure_url: debitResponse.three_d_secure_url
})
}
} else if (debitResponse.result.code !== 100) {
throw 'Wrong result code: ' + debitResponse.result.code + ' message: ' + debitResponse.result.message;
}
return commitPayment(transactionId, amount, currency, user)
.then(() => {
return {statusCode: 200, body: JSON.stringify({response_code: debitResponse.result.code})}
})
})
.catch(error => {
return {statusCode: 500, body: error}
});
});
};
module.exports.commitScaPayment = async (event) => {
if (secureSigner.validateFormRedirect(event.queryStringParameters)) {
// Match the transaction by order id and handle user authentication
// This is just a dummy sample
return commitPayment(
event.queryStringParameters["sph-transaction-id"],
event.queryStringParameters["sph-amount"],
event.queryStringParameters["sph-currency"],
event.queryStringParameters["user"]
)
.then(() => {
return {statusCode: 200, body: ""}
});
} else {
console.log("Signature validation failed");
return {statusCode: 200, body: ""}
}
};
function commitPayment(transactionId, amount, currency, user) {
return paymentApi.commitTransaction(transactionId, new paymentHighway.CommitTransactionRequest(amount, currency))
.then(transactionResultResponse => {
const params = {
TableName: process.env.TRANSACTIONS_TABLE,
Item: {
user: user,
transaction_id: transactionId,
committed: transactionResultResponse.committed,
committed_amount: transactionResultResponse.committed_amount,
card_token: transactionResultResponse.card_token,
recurring: transactionResultResponse.recurring,
filing_code: transactionResultResponse.filing_code,
cardholder_authentication: transactionResultResponse.cardholder_authentication,
card: transactionResultResponse.card,
date: moment().toISOString(),
}
};
return DynamoDb.put(params).promise();
})
}