Created
October 19, 2017 21:07
-
-
Save michelle/6e7e5a64e5a9f8cbcb3da30f7392d59f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
const supportedInstruments = [ | |
{ | |
supportedMethods: ['basic-card'], | |
} | |
]; | |
const paymentDetails = { | |
total: { | |
label: 'Total', | |
amount: { | |
currency: 'USD', | |
value: '1.00', | |
}, | |
}, | |
shippingOptions: [{ | |
id: 'FREE', | |
label: 'Free shipping', | |
amount: { | |
currency: 'USD', | |
value: '0.00', | |
}, | |
selected: true, | |
}, { | |
id: 'EXPENSIVE', | |
label: 'Really expensive shipping', | |
amount: { | |
currency: 'USD', | |
value: '50.00', | |
}, | |
}], | |
}; | |
const paymentOptions = { | |
requestShipping: true, | |
}; | |
const pr = new PaymentRequest(supportedInstruments, paymentDetails, paymentOptions); | |
pr.addEventListener('shippingaddresschange', (ev) => { | |
console.log('[event] shippingaddresschange', pr.shippingAddress); | |
if (pr.shippingAddress.country === 'US') { | |
ev.updateWith(paymentDetails); | |
} else { | |
ev.updateWith(Object.assign({}, paymentDetails, {shippingOptions: []})); | |
} | |
}); | |
pr.addEventListener('shippingoptionchange', (ev) => { | |
console.log('[event] shippingoptionchange'); | |
ev.updateWith(paymentDetails); | |
}); | |
pr.show().then((resp, rej) => { | |
console.log('[success]', resp); | |
resp.complete(Promise.resolve('success')); | |
}).catch((ex) => { | |
console.log('[exception]', ex); | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment