| /* |
| * Copyright 2020 The Chromium Authors. All rights reserved. |
| * Use of this source code is governed by a BSD-style license that can be |
| * found in the LICENSE file. |
| */ |
| |
| const kPaymentMethodIdentifier = 'secure-payment-confirmation'; |
| |
| /** |
| * Returns the challenge generated by the secure payment confirmation app. |
| * @param {string} credentialId - The base64 encoded identifier of the |
| * credential to use for payment. |
| * @param {string} totalAmount - The total amount to be charged. |
| * @return {Promise<object>} - Either the challenge string or an error message. |
| */ |
| async function getChallenge(credentialId, totalAmount) { // eslint-disable-line no-unused-vars, max-len |
| try { |
| const request = createPaymentRequest(credentialId, totalAmount, false, ''); |
| const response = await request.show(); |
| await response.complete(); |
| return response.details.challenge; |
| } catch (e) { |
| return e.message; |
| } |
| } |
| |
| /** |
| * Returns the challenge generated by the secure payment confirmation app with a |
| * modified total amount. |
| * @param {string} credentialId - The base64 encoded identifier of the |
| * credential to use for payment. |
| * @param {string} modifiedTotal - The total amount to be charged in the |
| * modifier. |
| * @return {Promise<object>} - Either the challenge string or an error message. |
| */ |
| async function getChallengeWithModifier(credentialId, modifiedTotal) { // eslint-disable-line no-unused-vars, max-len |
| try { |
| const request = createPaymentRequest( |
| credentialId, '0', true, modifiedTotal); |
| const response = await request.show(); |
| await response.complete(); |
| return response.details.challenge; |
| } catch (e) { |
| return e.message; |
| } |
| } |
| |
| /** |
| * Returns the challenge generated by the secure payment confirmation app. |
| * Passes a promise into PaymentRequest.show() that resolves with the finalized |
| * price after 0.5 seconds. |
| * @param {string} credentialId - The base64 encoded identifier of the |
| * credential to use for payment. |
| * @param {string} initialAmount - The initial, not yet final, amount to be |
| * charged. |
| * @param {string} finalizedAmount - The finalized amount to be charged. |
| * @return {Promise<object>} - Either the challenge string or an error message. |
| */ |
| async function getChallengeWithShowPromise(credentialId, initialAmount, finalizedAmount) { // eslint-disable-line no-unused-vars, max-len |
| try { |
| const request = createPaymentRequest( |
| credentialId, initialAmount, false, ''); |
| const response = await request.show(new Promise((resolve) => { |
| window.setTimeout(() => { |
| resolve(createDetails(finalizedAmount, false, '')); |
| }, 500); // 0.5 seconds. |
| })); |
| await response.complete(); |
| return response.details.challenge; |
| } catch (e) { |
| return e.message; |
| } |
| } |
| |
| /** |
| * Returns the challenge generated by the secure payment confirmation app. |
| * Passes a promise into PaymentRequest.show() that resolves with the finalized |
| * and modified price after 0.5 seconds. |
| * @param {string} credentialId - The base64 encoded identifier of the |
| * credential to use for payment. |
| * @param {string} finalizedModifierAmount - The finalized amount to be charged. |
| * @return {Promise<object>} - Either the challenge string or an error message. |
| */ |
| async function getChallengeWithModifierAndShowPromise(credentialId, finalizedModifierAmount) { // eslint-disable-line no-unused-vars, max-len |
| try { |
| const request = createPaymentRequest(credentialId, '0', false, ''); |
| const response = await request.show(new Promise((resolve) => { |
| window.setTimeout(() => { |
| resolve(createDetails('0', true, finalizedModifierAmount)); |
| }, 500); // 0.5 seconds. |
| })); |
| await response.complete(); |
| return response.details.challenge; |
| } catch (e) { |
| return e.message; |
| } |
| } |
| |
| /** |
| * Creates a PaymentRequest object for secure payment confirmation method. |
| * @param {string} credentialId - The base64 encoded identifier of the |
| * credential to use for payment. |
| * @param {string} totalAmount - The total amount to be charged. |
| * @param {bool} withModifier - Whether modifier should be added. |
| * @param {string} modifierAmount - The modifier amount, optional. |
| * @return {PaymentRequest} - A PaymentRequest object. |
| */ |
| function createPaymentRequest( |
| credentialId, totalAmount, withModifier, modifierAmount) { |
| return new PaymentRequest( |
| [{supportedMethods: kPaymentMethodIdentifier, |
| data: { |
| action: 'authenticate', |
| credentialIds: [Uint8Array.from(atob(credentialId), |
| (b) => b.charCodeAt(0))], |
| networkData: new TextEncoder().encode('hello world'), |
| timeout: 6000, |
| fallbackUrl: 'https://fallback.example/url', |
| }, |
| }], |
| createDetails(totalAmount, withModifier, modifierAmount)); |
| } |
| |
| /** |
| * Creates the payment details to be the second parameter of PaymentRequest |
| * constructor. |
| * @param {string} totalAmount - The total amount. |
| * @param {bool} withModifier - Whether modifier should be added. |
| * @param {string} modifierAmount - The modifier amount, optional. |
| * @return {PaymentDetails} - The payment details with the given total amount. |
| */ |
| function createDetails(totalAmount, withModifier, modifierAmount) { |
| let result = { |
| total: {label: 'TEST', amount: {currency: 'USD', value: totalAmount}}, |
| }; |
| if (withModifier) { |
| result.modifiers = [{ |
| supportedMethods: kPaymentMethodIdentifier, |
| total: { |
| label: 'MODIFIER TEST', |
| amount: {currency: 'USD', value: modifierAmount}}, |
| }]; |
| } |
| return result; |
| } |