Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
106 lines (93 loc) · 2.87 KB
/
Copy pathmain.js
File metadata and controls
106 lines (93 loc) · 2.87 KB
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
let paymentsClient = null;
function initGooglePay() {
const gatewayMerchantId = document.getElementById("gatewayKey").value.trim();
const merchantId = document.getElementById("merchantId").value.trim();
const environment = document.getElementById("env").value;
if (!gatewayMerchantId || !merchantId) {
alert("Please fill in both Omise Public Key and Merchant ID.");
return;
}
const tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'omise',
gatewayMerchantId
}
};
const baseCardPaymentMethod = {
type: 'CARD',
parameters: {
allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"],
allowedCardNetworks: ["AMEX", "JCB", "MASTERCARD", "VISA"],
billingAddressRequired: true,
billingAddressParameters: {
format: "FULL",
phoneNumberRequired: true
}
},
tokenizationSpecification
};
const baseRequest = {
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [baseCardPaymentMethod]
};
const paymentDataRequest = {
...baseRequest,
transactionInfo: {
currencyCode: 'THB',
totalPriceStatus: 'FINAL',
totalPrice: '35.00'
},
merchantInfo: {
merchantId,
merchantName: 'Merchant'
}
};
paymentsClient = new google.payments.api.PaymentsClient({ environment });
paymentsClient.isReadyToPay(baseRequest)
.then(response => {
if (response.result) {
const button = paymentsClient.createButton({
onClick: () => {
paymentsClient.loadPaymentData(paymentDataRequest)
.then(paymentData => handlePaymentSuccess(paymentData))
.catch(err => {
console.error(err);
alert("Payment failed: " + JSON.stringify(err));
});
}
});
const container = document.getElementById("container");
container.innerHTML = "";
container.appendChild(button);
} else {
alert("Google Pay is not available.");
}
})
.catch(err => {
console.error(err);
alert("Error initializing Google Pay: " + err);
});
}
function handlePaymentSuccess(paymentData) {
const tokenStr = paymentData.paymentMethodData.tokenizationData.token;
let payload;
try {
payload = JSON.stringify(JSON.parse(tokenStr));
} catch (e) {
payload = tokenStr; // fallback to raw token if parsing fails
}
const formatted = typeof payload === "string"
? payload
: JSON.stringify(payload, null, 2);
const outputElem = document.getElementById("payloadOutput");
outputElem.textContent = formatted;
const copyBtn = document.getElementById("copyPayloadBtn");
copyBtn.style.display = "inline-block";
copyBtn.onclick = () => {
navigator.clipboard.writeText(formatted)
// .then(() => alert("Copied to clipboard!"))
.catch(err => alert("Copy failed: " + err));
};
}