Vize

use-payment-request

Collect payments with a typed Payment Request API wrapper.

Package @vizejs/composable/use-payment-request
Own the source vize lib pull composable:use-payment-request
Runtime exports usePaymentRequest
Gzip budget 2304 B

Minimal setup

Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).

vp install @vizejs/composable

Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.

import { usePaymentRequest } from "@vizejs/composable/use-payment-request";

Try it

Offer the browser payment sheet

Check whether the reader can pay with a browser payment method before offering it.

Open the interactive example

What to observe: Check payment options reports whether a saved method is available; otherwise the example points to the regular checkout.

show() opens the sheet and complete() closes it after your server confirms. Always keep a regular checkout. Every example renders a neutral message until it mounts, so server and client markup match, and then explains what to do when the browser lacks the API.

Compare the initial and interacted states
Offer the browser payment sheet: initial state
Initial state
Offer the browser payment sheet: state after the verified interactions
After the verified interactions

Copy the example

Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.

<script setup lang="ts">
import { ref } from "vue";
import { useMounted } from "@vizejs/composable/use-mounted";
import { usePaymentRequest } from "@vizejs/composable/use-payment-request";

const mounted = useMounted();
const { supported, canMakePayment } = usePaymentRequest({
  methods: [{ supportedMethods: "https://google.com/pay" }],
  details: { total: { label: "Workshop ticket", amount: { currency: "EUR", value: "49.00" } } },
});
const available = ref<boolean>();

async function check() {
  available.value = await canMakePayment();
}
</script>

<template>
  <div class="composable-example">
    <p>Offer a browser payment sheet only when the reader can pay with it.</p>
    <button type="button" :disabled="!mounted || !supported" @click="check">
      Check payment options
    </button>
    <output aria-live="polite">{{
      !mounted
        ? "Checking browser support…"
        : !supported
          ? "The Payment Request API is not available; show your regular checkout."
          : available === undefined
            ? "Payment Request is available."
            : available
              ? "The reader can pay with the browser's payment sheet."
              : "No saved payment method; show your regular checkout."
    }}</output>
  </div>
</template>

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
usePaymentRequest capability experimental deterministic-fallback caller-managed caller, reactive-scope web, desktop window tryOnScopeDispose

API

usePaymentRequest

Collect payments with a typed Payment Request API wrapper. Every show() constructs a fresh request from the reactive methods, details, and paymentOptions, wires the shipping hooks (which call updateWith synchronously when they return an update), and resolves to a discriminated PaymentShowResult. Call complete() after processing a "completed" result. A showing sheet is aborted when the owning reactive scope stops; outside a scope call abort(). Server rendering: nothing is constructed, supported is false and state is "idle". Inside a component supported turns true only after mounting, so hydration renders this server state first.

function usePaymentRequest(options: UsePaymentRequestOptions): PaymentRequestControls
const payment = usePaymentRequest({ methods, details: { total } });
const result = await payment.show();
if (result.status === "completed") await payment.complete(await charge(result.response));

Types

PaymentAmount

Monetary amount.

Member Type Description
currency string ISO 4217 currency code.
value string Decimal amount, for example "9.99".

PaymentLineItem

Line item shown on the payment sheet.

Member Type Description
label string Human-readable label.
amount PaymentAmount Amount.
pending? boolean Whether the amount is not final yet.

PaymentShippingOption

Selectable shipping option.

Member Type Description
id string Option id reported back in shippingOption.
label string Human-readable label.
amount PaymentAmount Price of the option.
selected? boolean Whether the option is preselected.

PaymentMethod

Accepted payment method.

Member Type Description
supportedMethods string Payment method identifier (URL or standardized name).
data? unknown Method-specific data.

PaymentDetails

Initial payment details.

Member Type Description
id? string Request id.
total PaymentLineItem Total amount.
displayItems? readonly PaymentLineItem[] Line items.
shippingOptions? readonly PaymentShippingOption[] Shipping options.

PaymentDetailsUpdate

Details passed to updateWith after a shipping change.

Member Type Description
total? PaymentLineItem Updated total.
displayItems? readonly PaymentLineItem[] Updated line items.
shippingOptions? readonly PaymentShippingOption[] Updated shipping options (empty to reject the address).
error? string Error message shown to the user.
shippingAddressErrors? Readonly<Record<string, string>> Per-field shipping address errors.

PaymentRequestOptionsInit

Payer information requested from the sheet.

Member Type Description
requestPayerName? boolean Request the payer name.
requestPayerEmail? boolean Request the payer email.
requestPayerPhone? boolean Request the payer phone.
requestShipping? boolean Request a shipping address.
shippingType? PaymentShippingType Shipping category.

PaymentResponseLike

Minimal PaymentResponse.

Member Type Description
requestId string Request id.
methodName string Method the payer chose.
details unknown Method-specific response data.
payerName? string | null Payer name, when requested.
payerEmail? string | null Payer email, when requested.
payerPhone? string | null Payer phone, when requested.
shippingAddress? unknown Shipping address, when requested.
shippingOption? string | null Selected shipping option id.
complete (result?: PaymentCompletion) => Promise<void> Close the sheet with an outcome.

PaymentRequestLike

Minimal PaymentRequest instance.

Member Type Description
shippingAddress? unknown Current shipping address.
shippingOption? string | null Current shipping option id.
canMakePayment () => Promise<boolean> Whether a method can be used.
show () => Promise<PaymentResponseLike> Show the payment sheet (needs user activation).
abort () => Promise<void> Abort a showing sheet.

PaymentShippingChange

Context passed to shipping change hooks.

Member Type Description
request PaymentRequestLike Request that fired the change.
shippingAddress unknown Current shipping address.
shippingOption string | null Current shipping option id.

UsePaymentRequestOptions

Options for usePaymentRequest.

Member Type Description
methods MaybeRefOrGetter<readonly PaymentMethod[]> Accepted payment methods. Reactive; read when a request is created.
details MaybeRefOrGetter<PaymentDetails> Payment details. Reactive; read when a request is created.
paymentOptions? MaybeRefOrGetter<PaymentRequestOptionsInit> Payer information to request.
PaymentRequest? MaybeRef<PaymentRequestHost | null | undefined> PaymentRequest constructor for alternate runtimes and tests. A ref (not a getter) because the host is a constructor function.
onShippingAddressChange? PaymentShippingChangeHook Called on shippingaddresschange.
onShippingOptionChange? PaymentShippingChangeHook Called on shippingoptionchange.

PaymentRequestControls

Reactive state and actions returned by usePaymentRequest.

Member Type Description
supported ComputedRef<boolean> Whether the Payment Request API is available.
state Readonly<ShallowRef<PaymentRequestState>> Sheet lifecycle.
response Readonly<ShallowRef<PaymentResponseLike | undefined>> Most recent response, cleared when a new sheet is shown.
error Readonly<ShallowRef<unknown>> Most recent failure (not dismissals).
canMakePayment () => Promise<boolean> Whether the configured methods can be used.
show () => Promise<PaymentShowResult> Show the payment sheet. Aborts a sheet that is still showing.
complete (result?: PaymentCompletion) => Promise<boolean> Close the sheet after processing the response.
abort () => Promise<boolean> Abort the showing sheet.