-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEasyAPI.js
105 lines (94 loc) · 2.79 KB
/
EasyAPI.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
/**
* Easy API
* https://tech.dibspayment.com/easy/api/paymentapi
*/
import moment from 'moment';
export default class EasyAPI {
constructor() {
// A redirect URL used to identify navigation
// from Mia SDK interface to the application.
//
// Note: Pass the same `redirectURL` for
// payment registration with Easy API and
// when presenting Mia SDK following payment registration.
this.redirectURL = 'https://127.0.0.1/redirect.php';
// Cancellation URL passed to EASY and the SDK to indentify
// user cancellation by using the "Go back" link rendered
// in the checkout webview.
// Note: Pass the same `cancelURL` for
// payment registration with Easy API and
// when presenting Mia SDK following payment registration.
this.cancelURL = 'https://cancellation-identifier-url';
// Default currency
this.currency = 'SEK';
this.secretKey = 'YOUR TEST SECRET KEY';
}
createPaymentWithRequestBody(requestBody, callback) {
// EASY test environment is used for this demo
const baseURL = 'https://test.api.dibspayment.eu/v1/';
fetch(baseURL + '/payments', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Token ' + this.secretKey,
},
body: requestBody,
})
// .then(response => {
// console.log('response', response)
//
// })
.then(response => response.json())
.then(json => {
callback(json);
})
.catch(error => {
console.error(error);
});
}
makeSubscriptionRequest(price) {
const paymentRequest = this.makePaymentRequest(price);
const utc = '+03:00'; // EEST timezone is used for the sample app
const subscriptionEndDate =
moment()
.local()
.utcOffset(utc)
.add(3, 'years')
.format('YYYY-MM-DDThh:mm:ss') + utc;
paymentRequest.subscription = {
endDate: subscriptionEndDate,
interval: 0,
};
return paymentRequest;
}
// Returns the JSON request body required to create
// payment with Easy API.
makePaymentRequest(price) {
const amount = price * 100; // cents to notes
return {
checkout: {
termsUrl: 'http://localhost:8080/terms',
cancelURL: this.cancelURL,
returnURL: this.redirectURL,
integrationType: 'HostedPaymentPage',
merchantHandlesConsumerData: true,
},
order: {
reference: 'MiaSDK-iOS',
items: [
{
reference: 'MiaSDK-iOS',
name: 'Lightning Cable',
quantity: 1,
unit: 'pcs',
unitPrice: amount,
grossTotalAmount: amount,
netTotalAmount: amount,
},
],
amount: amount,
currency: 'DKK',
},
};
}
}