# @provenanceauth/rails/x402

@provenanceauth/rails/x402 in the Rails: 12 methods. X402 (HTTP 402 agent payments) mapping + merchant verification.

## Methods

### [encodeX402Header](https://provenanceauth.com/docs/sdks/rails/x402/encode-x402-header.md)

```ts
encodeX402Header(obj: X402PaymentRequired | X402PaymentPayload): string
```

Encode an x402 object as a header value (base64 of its JSON).

### [decodePaymentRequired](https://provenanceauth.com/docs/sdks/rails/x402/decode-payment-required.md)

```ts
decodePaymentRequired(headerValue: string | null | undefined): X402PaymentRequired | null
```

Decode + shape-check a `PAYMENT-REQUIRED` value.

### [decodePaymentPayload](https://provenanceauth.com/docs/sdks/rails/x402/decode-payment-payload.md)

```ts
decodePaymentPayload(headerValue: string | null | undefined): X402PaymentPayload | null
```

Decode + shape-check a `PAYMENT-SIGNATURE` value.

### [intentExtension](https://provenanceauth.com/docs/sdks/rails/x402/intent-extension.md)

```ts
intentExtension(opts?: { required?: boolean; profile?: DiscoveryBlock }): X402Extension
```

The extension entry a merchant advertises.

### [withIntentExtension](https://provenanceauth.com/docs/sdks/rails/x402/with-intent-extension.md)

```ts
withIntentExtension(paymentRequired: X402PaymentRequired, opts?: { required?: boolean; profile?: DiscoveryBlock }): X402PaymentRequired
```

Add the extension to a `PaymentRequired` your x402 server already builds (returns a new object; other extensions are kept).

### [x402Challenge](https://provenanceauth.com/docs/sdks/rails/x402/x402-challenge.md)

```ts
x402Challenge(opts: { resource: X402ResourceInfo | string; accepts: X402PaymentRequirements[]; error?: string; required?: boolean; extensions?: Record<string, X402Extension>; /** #110: the business's discovery block (`discoveryFields(profile).x402.profile`). */ profile?: DiscoveryBlock; }): { status: 402; headers: Record<string, string>; body: X402PaymentRequired }
```

Build the 402 a merchant returns: the `PaymentRequired` with the extension, its base64 `PAYMENT-REQUIRED` header, and the same object as an informational JSON body.

### [x402ProfileOf](https://provenanceauth.com/docs/sdks/rails/x402/x402-profile-of.md)

```ts
x402ProfileOf(paymentRequired: Pick<X402PaymentRequired, "extensions"> | null | undefined): DiscoveryBlock | null
```

Agent side: the business's discovery block in a 402's extension, or null (older merchants send none).

### [x402BindingPayload](https://provenanceauth.com/docs/sdks/rails/x402/x402-binding-payload.md)

```ts
x402BindingPayload(resource: X402ResourceInfo | string, requirements: Pick<X402PaymentRequirements, "scheme" | "network" | "amount" | "asset" | "payTo">): X402Binding
```

The exact object to pass as `action_payload` to `POST /v1/mandate/intents`, so the token's `payload_hash` binds this resource and these payment requirements.

### [x402BindingHash](https://provenanceauth.com/docs/sdks/rails/x402/x402-binding-hash.md)

```ts
x402BindingHash(b: X402Binding): Promise<string>
```

Sha256(canonical JSON) of the binding, the value a bound token carries as `payload_hash`.

### [withIntentToken](https://provenanceauth.com/docs/sdks/rails/x402/with-intent-token.md)

```ts
withIntentToken(payment: X402PaymentPayload, extension: X402Extension, token: string, opts?: { jwksUrl?: string }): X402PaymentPayload
```

Append the intent token to a `PaymentPayload`'s extensions, echoing the merchant's entry unchanged (x402 v2: clients may append info but never delete or overwrite it).

### [intentPaymentFetch](https://provenanceauth.com/docs/sdks/rails/x402/intent-payment-fetch.md)

```ts
intentPaymentFetch(fetchImpl: typeof fetch, getToken: (challenge: X402IntentChallenge) => Promise<string | null> | string | null, opts: IntentPaymentFetchOptions): typeof fetch
```

Client middleware: wrap a fetch so a 402 that asks for a Provenance intent is retried once with a `PAYMENT-SIGNATURE` carrying the payment and the token.

### [verifyX402Payment](https://provenanceauth.com/docs/sdks/rails/x402/verify-x402-payment.md)

```ts
verifyX402Payment(paymentSignature: string | null | undefined, opts: X402VerifyOptions): Promise<X402VerifyResult>
```

Merchant side: verify the intent token in a `PAYMENT-SIGNATURE` header before you settle.
