-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.html
131 lines (114 loc) · 3.74 KB
/
index.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bKash PGW Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button id="bKash_button" class="btn btn-danger" disabled="disabled">Pay With bKash</button>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://scripts.sandbox.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-sandbox.js"></script>
<script type="text/javascript">
let paymentID;
let username = "Your username";
let password = "Your password";
let app_key = "Your app key";
let app_secret = "Your app secret";
let grantTokenUrl = 'https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/token/grant';
let createCheckoutUrl = 'https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/create';
let executeCheckoutUrl = 'https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/execute';
$(document).ready(function () {
getAuthToken();
});
function getAuthToken() {
let body = {
"app_key": app_key,
"app_secret": app_secret
};
$.ajax({
url: grantTokenUrl,
headers: {
"username": username,
"password": password,
"Content-Type": "application/json"
},
type: 'POST',
data: JSON.stringify(body),
success: function (result) {
let headers = {
"Content-Type": "application/json",
"Authorization": result.id_token, // Contains access token
"X-APP-Key": app_key
};
let request = {
"amount": "85.50",
"intent": "sale",
"currency": "BDT",
"merchantInvoiceNumber": "123456"
};
initBkash(headers, request);
},
error: function (error) {
console.log(error);
}
});
}
function initBkash(headers, request) {
bKash.init({
paymentMode: 'checkout',
paymentRequest: request,
createRequest: function (request) {
$.ajax({
url: createCheckoutUrl,
headers: headers,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(request),
success: function (data) {
if (data && data.paymentID != null) {
paymentID = data.paymentID;
bKash.create().onSuccess(data);
}
else {
bKash.create().onError(); // Run clean up code
alert(data.errorMessage + " Tag should be 2 digit, Length should be 2 digit, Value should be number of character mention in Length, ex. MI041234 , supported tags are MI, MW, RF");
}
},
error: function () {
bKash.create().onError(); // Run clean up code
alert(data.errorMessage);
}
});
},
executeRequestOnAuthorization: function () {
$.ajax({
url: executeCheckoutUrl + '/' + paymentID,
headers: headers,
type: 'POST',
contentType: 'application/json',
success: function (data) {
if (data && data.paymentID != null) {
// On success, perform your desired action
alert('[SUCCESS] data : ' + JSON.stringify(data));
window.location.href = "/success_page.html";
} else {
alert('[ERROR] data : ' + JSON.stringify(data));
bKash.execute().onError();//run clean up code
}
},
error: function () {
alert('An alert has occurred during execute');
bKash.execute().onError(); // Run clean up code
}
});
},
onClose: function () {
alert('User has clicked the close button');
}
});
$('#bKash_button').removeAttr('disabled');
}
</script>
</html>