mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
add tests
This commit is contained in:
2
paymentrequest/index.d.ts
vendored
2
paymentrequest/index.d.ts
vendored
@@ -27,7 +27,7 @@ interface PaymentDetails {
|
||||
total: PaymentItem;
|
||||
displayItems?: PaymentItem[];
|
||||
shippingOptions?: PaymentShippingOption[];
|
||||
modifiers: PaymentDetailsModifier[];
|
||||
modifiers?: PaymentDetailsModifier[];
|
||||
}
|
||||
|
||||
interface PaymentDetailsModifier {
|
||||
|
||||
106
paymentrequest/tests.ts
Normal file
106
paymentrequest/tests.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
/// <reference path="index.d.ts" />
|
||||
/// <reference path="../promise" />
|
||||
|
||||
function makeRequest() {
|
||||
if (!window.PaymentRequest) {
|
||||
return Promise.reject(new Error("PaymentRequest not available"))
|
||||
}
|
||||
|
||||
const methodData = [
|
||||
{
|
||||
supportedMethods: ["visa", "mastercard"]
|
||||
}
|
||||
]
|
||||
|
||||
const details = {
|
||||
displayItems: [
|
||||
{
|
||||
label: "Original donation amount",
|
||||
amount: { currency: "USD", value : "65.00" }, // US$65.00
|
||||
},
|
||||
{
|
||||
label: "Friends and family discount",
|
||||
amount: { currency: "USD", value : "-10.00" }, // -US$10.00
|
||||
}
|
||||
],
|
||||
total: {
|
||||
label: "Total",
|
||||
amount: { currency: "USD", value : "55.00" }, // US$55.00
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
requestShipping: true,
|
||||
requestPayerEmail: true,
|
||||
requestPayerPhone: true
|
||||
}
|
||||
|
||||
const request = new window.PaymentRequest(methodData, details, options)
|
||||
request.addEventListener("shippingaddresschange", onShippingAddressChange)
|
||||
|
||||
return request.show()
|
||||
}
|
||||
|
||||
async function processPayment() {
|
||||
try {
|
||||
const paymentResponse = await makeRequest()
|
||||
} catch (error) {
|
||||
location.href = '/checkout';
|
||||
return;
|
||||
}
|
||||
|
||||
var paymentData = {
|
||||
// payment method string
|
||||
method: paymentResponse.methodName,
|
||||
// payment details as you requested
|
||||
details: paymentResponse.details,
|
||||
// shipping address information
|
||||
address: toDict(paymentResponse.shippingAddress),
|
||||
// shipping option
|
||||
shippingOption: paymentResponse.shippingOption
|
||||
}
|
||||
|
||||
// make call to backend to process payment data
|
||||
const ok = await Promise.resolve(true)
|
||||
|
||||
if (ok) {
|
||||
paymentResponse.complete("success")
|
||||
} else {
|
||||
paymentResponse.complete("fail")
|
||||
}
|
||||
}
|
||||
|
||||
function onShippingAddressChange(e) {
|
||||
e.updateWith(((details, addr) => {
|
||||
if (addr.country === 'US') {
|
||||
var shippingOption = {
|
||||
id: '',
|
||||
label: '',
|
||||
amount: {currency: 'USD', value: '0.00'},
|
||||
selected: true
|
||||
};
|
||||
if (addr.region === 'US') {
|
||||
shippingOption.id = 'us';
|
||||
shippingOption.label = 'Standard shipping in US';
|
||||
shippingOption.amount.value = '0.00';
|
||||
details.total.amount.value = '55.00';
|
||||
} else {
|
||||
shippingOption.id = 'others';
|
||||
shippingOption.label = 'International shipping';
|
||||
shippingOption.amount.value = '10.00';
|
||||
details.total.amount.value = '65.00';
|
||||
}
|
||||
if (details.displayItems.length === 2) {
|
||||
details.displayItems.splice(1, 0, shippingOption);
|
||||
} else {
|
||||
details.displayItems.splice(1, 1, shippingOption);
|
||||
}
|
||||
details.shippingOptions = [shippingOption];
|
||||
} else {
|
||||
details.shippingOptions = [];
|
||||
}
|
||||
return Promise.resolve(details);
|
||||
})(details, request.shippingAddress));
|
||||
}
|
||||
|
||||
document.querySelector("#pay").addEventListener("click", processPayment)
|
||||
@@ -13,6 +13,7 @@
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts"
|
||||
"index.d.ts",
|
||||
"tests.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user