diff options
author | Sebastian <sebasjm@gmail.com> | 2022-09-16 16:03:58 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-09-16 16:03:58 -0300 |
commit | 59d235e8d29159bc8caccf8bee6a2bca8b0b90dc (patch) | |
tree | 551261c860b1989dc0b1a05ddcfd53d20e561626 /packages/taler-wallet-webextension/src/cta/Payment | |
parent | 6ddb2de84245ae3914c92b2b2eb7399e7f04500e (diff) | |
download | wallet-core-59d235e8d29159bc8caccf8bee6a2bca8b0b90dc.tar.xz |
redirect after success #7357
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Payment')
5 files changed, 108 insertions, 230 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Payment/index.ts b/packages/taler-wallet-webextension/src/cta/Payment/index.ts index b20f91b0c..1dd84fd7e 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/index.ts +++ b/packages/taler-wallet-webextension/src/cta/Payment/index.ts @@ -35,6 +35,7 @@ export interface Props { talerPayUri?: string; goToWalletManualWithdraw: (amount?: string) => Promise<void>; cancel: () => Promise<void>; + onSuccess: (tx: string) => Promise<void>; } export type State = @@ -43,7 +44,6 @@ export type State = | State.Ready | State.NoEnoughBalance | State.NoBalanceForCurrency - | State.Completed | State.Confirmed; export namespace State { @@ -86,13 +86,6 @@ export namespace State { balance: AmountJson; } - export interface Completed extends BaseInfo { - status: "completed"; - payStatus: PreparePayResult; - payResult: ConfirmPayResult; - paymentError?: TalerError; - balance: AmountJson; - } } const viewMapping: StateViewMap<State> = { @@ -101,7 +94,6 @@ const viewMapping: StateViewMap<State> = { "no-balance-for-currency": BaseView, "no-enough-balance": BaseView, confirmed: BaseView, - completed: BaseView, ready: BaseView, }; diff --git a/packages/taler-wallet-webextension/src/cta/Payment/state.ts b/packages/taler-wallet-webextension/src/cta/Payment/state.ts index 488263bb8..e8690be39 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/state.ts +++ b/packages/taler-wallet-webextension/src/cta/Payment/state.ts @@ -31,12 +31,9 @@ import * as wxApi from "../../wxApi.js"; import { Props, State } from "./index.js"; export function useComponentState( - { talerPayUri, cancel, goToWalletManualWithdraw }: Props, + { talerPayUri, cancel, goToWalletManualWithdraw, onSuccess }: Props, api: typeof wxApi, ): State { - const [payResult, setPayResult] = useState<ConfirmPayResult | undefined>( - undefined, - ); const [payErrMsg, setPayErrMsg] = useState<TalerError | undefined>(undefined); const hook = useAsyncAsHook(async () => { @@ -104,17 +101,6 @@ export function useComponentState( const foundAmount = Amounts.parseOrThrow(foundBalance.available); - if (payResult) { - return { - status: "completed", - balance: foundAmount, - payStatus, - paymentError: payErrMsg, - payResult, - ...baseResult, - }; - } - if (payStatus.status === PreparePayResultType.InsufficientBalance) { return { status: "no-enough-balance", @@ -157,7 +143,7 @@ export function useComponentState( console.log(`should d to ${fu}`); } } - setPayResult(res); + onSuccess(res.transactionId); } catch (e) { if (e instanceof TalerError) { setPayErrMsg(e); diff --git a/packages/taler-wallet-webextension/src/cta/Payment/stories.tsx b/packages/taler-wallet-webextension/src/cta/Payment/stories.tsx index 559e5c5d4..6946f6906 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/stories.tsx +++ b/packages/taler-wallet-webextension/src/cta/Payment/stories.tsx @@ -334,80 +334,4 @@ export const AlreadyConfirmedByOther = createExample(BaseView, { }, }); -export const AlreadyPaidWithoutFulfillment = createExample(BaseView, { - status: "completed", - error: undefined, - amount: Amounts.parseOrThrow("USD:10"), - balance: { - currency: "USD", - fraction: 40000000, - value: 11, - }, - - uri: "taler://pay/merchant-backend.taler/2021.242-01G2X4275RBWG/?c=66BE594PDZR24744J6EQK52XM0", - payResult: { - type: ConfirmPayResultType.Done, - contractTerms: {} as any, - transactionId: "", - }, - payStatus: { - status: PreparePayResultType.AlreadyConfirmed, - amountEffective: "USD:10", - amountRaw: "USD:10", - contractTerms: { - merchant: { - name: "the merchant", - logo: merchantIcon, - website: "https://www.themerchant.taler", - email: "contact@merchant.taler", - }, - summary: "some beers", - amount: "USD:10", - } as Partial<ContractTerms> as any, - contractTermsHash: "123456", - proposalId: "96YY92RQZGF3V7TJSPN4SF9549QX7BRF88Q5PYFCSBNQ0YK4RPK0", - paid: true, - }, -}); - -export const AlreadyPaidWithFulfillment = createExample(BaseView, { - status: "completed", - error: undefined, - amount: Amounts.parseOrThrow("USD:10"), - balance: { - currency: "USD", - fraction: 40000000, - value: 11, - }, - uri: "taler://pay/merchant-backend.taler/2021.242-01G2X4275RBWG/?c=66BE594PDZR24744J6EQK52XM0", - payResult: { - type: ConfirmPayResultType.Done, - contractTerms: { - fulfillment_message: "thanks for buying!", - fulfillment_url: "https://demo.taler.net", - } as Partial<ContractTerms> as any, - transactionId: "", - }, - payStatus: { - status: PreparePayResultType.AlreadyConfirmed, - amountEffective: "USD:10", - amountRaw: "USD:10", - contractTerms: { - merchant: { - name: "the merchant", - logo: merchantIcon, - website: "https://www.themerchant.taler", - email: "contact@merchant.taler", - }, - fulfillment_url: "https://demo.taler.net", - fulfillment_message: - "congratulations! you are looking at the fulfillment message! ", - summary: "some beers", - amount: "USD:10", - } as Partial<ContractTerms> as any, - contractTermsHash: "123456", - proposalId: "96YY92RQZGF3V7TJSPN4SF9549QX7BRF88Q5PYFCSBNQ0YK4RPK0", - paid: true, - }, -}); diff --git a/packages/taler-wallet-webextension/src/cta/Payment/test.ts b/packages/taler-wallet-webextension/src/cta/Payment/test.ts index 235b22c22..91d2d825d 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/test.ts +++ b/packages/taler-wallet-webextension/src/cta/Payment/test.ts @@ -75,6 +75,7 @@ describe("Payment CTA states", () => { talerPayUri: undefined, cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, @@ -110,18 +111,19 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:10", - status: PreparePayResultType.InsufficientBalance, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:10", + status: PreparePayResultType.InsufficientBalance, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [], - } as Partial<BalancesResponse>), + ({ + balances: [], + } as Partial<BalancesResponse>), } as Partial<typeof wxApi> as any, ), ); @@ -152,22 +154,23 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:10", - status: PreparePayResultType.InsufficientBalance, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:10", + status: PreparePayResultType.InsufficientBalance, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: "USD:5", - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: "USD:5", + }, + ], + } as Partial<BalancesResponse>), } as Partial<typeof wxApi> as any, ), ); @@ -198,23 +201,24 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:10", - amountEffective: "USD:10", - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:10", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: "USD:15", - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), } as Partial<typeof wxApi> as any, ), ); @@ -247,23 +251,24 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:9", - amountEffective: "USD:10", - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: "USD:15", - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), } as Partial<typeof wxApi> as any, ), ); @@ -296,28 +301,29 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:9", - amountEffective: "USD:10", - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: "USD:15", - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), confirmPay: async () => - ({ - type: ConfirmPayResultType.Done, - contractTerms: {}, - } as Partial<ConfirmPayResult>), + ({ + type: ConfirmPayResultType.Done, + contractTerms: {}, + } as Partial<ConfirmPayResult>), } as Partial<typeof wxApi> as any, ), ); @@ -340,18 +346,18 @@ describe("Payment CTA states", () => { r.payHandler.onClick(); } - await waitNextUpdate(); + // await waitNextUpdate(); - { - const r = getLastResultOrThrow(); - if (r.status !== "completed") expect.fail(); - expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); - expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); - // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); - // if (r.payResult.type !== ConfirmPayResultType.Done) expect.fail(); - // expect(r.payResult.contractTerms).not.undefined; - // expect(r.payHandler.onClick).undefined; - } + // { + // const r = getLastResultOrThrow(); + // if (r.status !== "completed") expect.fail(); + // expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + // expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + // // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); + // // if (r.payResult.type !== ConfirmPayResultType.Done) expect.fail(); + // // expect(r.payResult.contractTerms).not.undefined; + // // expect(r.payHandler.onClick).undefined; + // } await assertNoPendingUpdate(); }); @@ -364,28 +370,29 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: nullFunction, preparePay: async () => - ({ - amountRaw: "USD:9", - amountEffective: "USD:10", - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: "USD:15", - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), confirmPay: async () => - ({ - type: ConfirmPayResultType.Pending, - lastError: { code: 1 }, - } as Partial<ConfirmPayResult>), + ({ + type: ConfirmPayResultType.Pending, + lastError: { code: 1 }, + } as Partial<ConfirmPayResult>), } as Partial<typeof wxApi> as any, ), ); @@ -447,23 +454,24 @@ describe("Payment CTA states", () => { talerPayUri: "taller://pay", cancel: nullFunction, goToWalletManualWithdraw: nullFunction, + onSuccess: async () => { null; }, }, { onUpdateNotification: subscriptions.saveSubscription, preparePay: async () => - ({ - amountRaw: "USD:9", - amountEffective: "USD:10", - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), getBalance: async () => - ({ - balances: [ - { - available: Amounts.stringify(availableBalance), - }, - ], - } as Partial<BalancesResponse>), + ({ + balances: [ + { + available: Amounts.stringify(availableBalance), + }, + ], + } as Partial<BalancesResponse>), } as Partial<typeof wxApi> as any, ), ); diff --git a/packages/taler-wallet-webextension/src/cta/Payment/views.tsx b/packages/taler-wallet-webextension/src/cta/Payment/views.tsx index 0383960ed..a2e52269c 100644 --- a/packages/taler-wallet-webextension/src/cta/Payment/views.tsx +++ b/packages/taler-wallet-webextension/src/cta/Payment/views.tsx @@ -63,7 +63,6 @@ export function LoadingUriView({ error }: State.LoadingUriError): VNode { type SupportedStates = | State.Ready | State.Confirmed - | State.Completed | State.NoBalanceForCurrency | State.NoEnoughBalance; @@ -167,7 +166,6 @@ export function BaseView(state: SupportedStates): VNode { /> )} </section> - {state.status !== "completed" ? ( <ButtonsSection amount={state.amount} balance={state.balance} @@ -176,7 +174,6 @@ export function BaseView(state: SupportedStates): VNode { payHandler={state.status === "ready" ? state.payHandler : undefined} goToWalletManualWithdraw={state.goToWalletManualWithdraw} /> - ) : undefined} <section> <Link upperCased onClick={state.cancel}> <i18n.Translate>Cancel</i18n.Translate> @@ -285,35 +282,6 @@ function ShowImportantMessage({ state }: { state: SupportedStates }): VNode { ); } - if (state.status == "completed") { - const { payResult, paymentError } = state; - if (paymentError) { - return <ErrorTalerOperation error={paymentError.errorDetail} />; - } - if (payResult.type === ConfirmPayResultType.Done) { - return ( - <SuccessBox> - <h3> - <i18n.Translate>Payment complete</i18n.Translate> - </h3> - <p> - {!payResult.contractTerms.fulfillment_message ? ( - payResult.contractTerms.fulfillment_url ? ( - <i18n.Translate> - You are going to be redirected to $ - {payResult.contractTerms.fulfillment_url} - </i18n.Translate> - ) : ( - <i18n.Translate>You can close this page.</i18n.Translate> - ) - ) : ( - payResult.contractTerms.fulfillment_message - )} - </p> - </SuccessBox> - ); - } - } return <Fragment />; } |