payment-request #
The dictionaries of W3C Payment Request
(Candidate Recommendation Draft 2026-11-17) as
nox-json codecs, with the
checks the PaymentRequest constructor makes on them, and
W3C Payment Method Identifiers
(Recommendation 2026-11-18), and the payment method manifests of
W3C Payment Method Manifest
(First Public Working Draft 2017-12-12), with the respond routes that serve
one.
A merchant backend builds the request a page passes to new PaymentRequest()
from its own prices, checks it as the browser will, and checks the
PaymentResponse the page sends back against it.
Contents #
Installation #
$ opam install payment-request
The library is payment-request and its module is Payment_request. The
library payment-request.respond, module Payment_request_respond, holds
the routes that serve a manifest, and the payment-request command reaches
both.
Usage #
Request.check runs the constructor's checks (section 3.1) and canonicalizes
each amount's currency to uppercase. A value stays as written: "55" is not
"55.00" (section 5.1).
# open Payment_request;;
# let total = Item.v ~label:"Total" (Amount.v ~currency:"usd" "55");;
val total : Item.t =
{Item.label = "Total"; amount = <abstr>; pending = false}
# let methods = [ Method_data.v "https://example.com/bobpay" ];;
val methods : Method_data.t list =
[{Method_data.supported_methods = "https://example.com/bobpay";
data = None}]
# match Request.check methods (Details_init.v ~id:"order-1" total) with
| Ok r -> Fmt.pr "%a@." Amount.pp r.details.total.amount
| Error e -> Fmt.pr "%a@." Check_error.pp e;;
USD 55
- : unit = ()
A refusal is the TypeError or RangeError the browser would throw:
# let refund = Item.v ~label:"Total" (Amount.v ~currency:"USD" "-1.00") in
match Request.check methods (Details_init.v refund) with
| Ok _ -> ()
| Error e -> Fmt.pr "%a@." Check_error.pp e;;
TypeError: total "-1.00" is negative
- : unit = ()
Payment method identifiers are standardized (secure-payment-confirmation)
or URL-based (https with no credentials):
# Method_id.is_valid "https://example.com/bobpay";;
- : bool = true
# Method_id.is_valid "http://example.com/bobpay";;
- : bool = false
# Method_id.is_valid "Basic-Card";;
- : bool = false
The dictionaries decode and encode with their codecs:
# Json.of_string Amount.codec {|{"currency":"EUR","value":"10.50"}|}
|> Result.map Amount.value;;
- : (string, Json.Error.t) result = Ok "10.50"
The owner of a URL-based identifier serves a payment method manifest that
names the payment apps that may handle it. Manifest.of_string validates
and parses one as section 3.4 does, against the URL it is fetched from:
# let manifest_url =
Result.get_ok
(Http.Huri.of_string "https://alicepay.com/pay/payment-manifest.json");;
val manifest_url : Http.Huri.t = <abstr>
# Manifest.of_string ~url:manifest_url
{|{"default_applications": ["app.json"], "supported_origins": "*"}|}
|> Result.iter (Fmt.pr "%a@." Manifest.pp);;
default_applications: https://alicepay.com/pay/app.json
supported_origins: *
- : unit = ()
Payment_request_respond.routes answers GET and HEAD of the identifier URL
with the Link header that points at the manifest (section 3.3) and the
manifest as the body, and GET and HEAD of the manifest URL with the
manifest, both with Access-Control-Allow-Origin: *; it refuses a manifest
section 3.4 refuses. From the command line:
$ payment-request manifest check --manifest https://alicepay.com/pay/payment-manifest.json manifest.json
$ payment-request manifest serve --identifier https://alicepay.com/pay \
--manifest https://alicepay.com/pay/payment-manifest.json --port 8080 manifest.json
What it covers #
- Payment Request: PaymentMethodData (section 4), PaymentCurrencyAmount and
its validity checkers (section 5.1), PaymentDetailsInit and
PaymentDetailsUpdate (section 6), PaymentDetailsModifier, PaymentOptions,
PaymentItem, PaymentShippingOption (sections 7 to 13), the fields of a
PaymentResponse with PaymentValidationErrors and PayerErrors (section 14),
and AddressErrors (section 15).Response.checkchecks a response against
the request it answers. - Payment Method Identifiers: validity (section 1.1), URL-based identifiers
and their comparison (sections 2.1 and 2.2), standardized identifiers and
their comparison (sections 3 and 3.2), and the registry (section 4). - Payment Method Manifest: the manifest format (section 2), validation and
parsing (section 3.4), and theLinkheader with the relation
payment-method-manifest(sections 3.3 and 5.1), served by a respond
route.
A URL-based identifier is parsed by nox-http's Huri, an RFC 3986 parser,
where the specification runs the URL Standard's basic URL parser; the two
can disagree on input that is not an RFC 3986 URI; the URLs of a manifest
are parsed the same way. Converting a value to
minor units belongs to a money library.
In the browser #
payment-request-js (module Payment_request_js) calls the browser's
PaymentRequest on brr. It builds each
object it passes to the browser from these codecs' values and decodes what it
reads back with them, so a checkout page and its backend share one model.
Payment_request_js.v ~options methods details is new PaymentRequest()
over a Method_data.t list, a Details_init.t and an Options.t;
Payment_request_js.show resolves to the payer's response, whose
Response.fields is the Response.t the backend checks with
Response.check; and Response.complete and Response.retry answer the
payer. An update event's update_with takes the Details_update.t of the
payment sheet. The page under examples/checkout is that flow compiled.
The secure-payment-confirmation method data carries its challenge and
credentialIds as base64url text, as WebAuthn's JSON forms do, and reaches
the browser as the ArrayBuffers Secure Payment Confirmation takes. The
page under examples/checkout opens the payment sheet in Chrome, with Google
Pay's TEST environment as its method.
It covers these members of the interfaces of the specification's IDL, as
WPT's payment-request/idlharness.https.window.js names them:
PaymentRequest: the constructor,show(detailsPromise),abort(),
canMakePayment(),id,shippingAddress,shippingOption,
shippingType, and the eventsshippingaddresschange,
shippingoptionchangeandpaymentmethodchange, through brr'sEv.PaymentRequestUpdateEvent:updateWith(detailsPromise).PaymentMethodChangeEvent:methodName,methodDetails.PaymentResponse:toJSON(),requestId,methodName,
complete(result, details),retry(errorFields).
License #
ISC. See LICENSE.md.