// Payment intents: one attempt to collect a payment. import { collect, request, segment } from "./stripe" import type { Client, Page, RequestOptions, SearchPage, StripeError } from "./stripe" /// A payment intent. `customer`, `payment_method` and `latest_charge` are expandable: `idOf` gives their ids. export type PaymentIntent = { id: string, amount: float, currency: string, status: string, amount_capturable?: float, amount_received?: float, application_fee_amount?: float, capture_method?: string, client_secret?: string, confirmation_method?: string, customer?: json, description?: string, last_payment_error?: json, latest_charge?: json, metadata?: json, next_action?: json, on_behalf_of?: json, payment_method?: json, payment_method_types?: string[], receipt_email?: string, statement_descriptor?: string, transfer_data?: json, transfer_group?: string, canceled_at?: float, cancellation_reason?: string, created?: float, livemode?: boolean, } /// A new payment intent. `amount` is in the currency's smallest unit. `extra` holds any field this type doesn't list. export type PaymentIntentParams = { amount: float, currency: string, customer?: string, description?: string, metadata?: Map, payment_method?: string, payment_method_types?: string[], automatic_payment_methods?: { enabled: boolean, allow_redirects?: string }, capture_method?: string, confirm?: boolean, return_url?: string, receipt_email?: string, setup_future_usage?: string, statement_descriptor?: string, application_fee_amount?: float, on_behalf_of?: string, transfer_data?: { destination: string, amount?: float }, transfer_group?: string, extra?: json, } /// The fields of a payment intent that can change before it is confirmed. export type PaymentIntentUpdateParams = { amount?: float, currency?: string, customer?: string, description?: string, metadata?: Map, payment_method?: string, receipt_email?: string, application_fee_amount?: float, extra?: json, } /// What to list. export type PaymentIntentListParams = { customer?: string, limit?: float, starting_after?: string, ending_before?: string, extra?: json, } /// Creates a payment intent. /// /// ```weft /// let intent = try await paymentIntents.create(stripe, { /// amount: 1999, /// currency: "eur", /// automatic_payment_methods: { enabled: true }, /// }, { idempotencyKey: `order-${order.id}` }) /// ``` export async fn create( c: Client, params: PaymentIntentParams, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "POST", "/v1/payment_intents", params, options, j => try j as PaymentIntent, ) } /// Gets a payment intent by id. export async fn retrieve( c: Client, id: string, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "GET", `/v1/payment_intents/${segment(id)}`, null, options, j => try j as PaymentIntent, ) } /// Changes the fields given. export async fn update( c: Client, id: string, params: PaymentIntentUpdateParams, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "POST", `/v1/payment_intents/${segment(id)}`, params, options, j => try j as PaymentIntent, ) } /// Confirms that the customer intends to pay. `params` may name a `payment_method` or a `return_url`. export async fn confirm( c: Client, id: string, params: json = null, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "POST", `/v1/payment_intents/${segment(id)}/confirm`, params, options, j => try j as PaymentIntent, ) } /// Captures an authorised payment, all of it unless `params` gives an `amount_to_capture`. export async fn capture( c: Client, id: string, params: json = null, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "POST", `/v1/payment_intents/${segment(id)}/capture`, params, options, j => try j as PaymentIntent, ) } /// Cancels a payment intent that hasn't succeeded. export async fn cancel( c: Client, id: string, params: json = null, options: RequestOptions = {}, ): PaymentIntent ! StripeError { return try await request( c, "POST", `/v1/payment_intents/${segment(id)}/cancel`, params, options, j => try j as PaymentIntent, ) } /// One page of payment intents, newest first. export async fn list( c: Client, params: PaymentIntentListParams = {}, options: RequestOptions = {}, ): Page ! StripeError { return try await request( c, "GET", "/v1/payment_intents", params, options, j => try j as Page, ) } /// Every payment intent matching `params`, up to `max`. export async fn listAll( c: Client, params: PaymentIntentListParams = {}, options: RequestOptions = {}, max: int = 10000, ): PaymentIntent[] ! StripeError { return try await collect( c, "/v1/payment_intents", params, options, j => try j as Page, x => x.id, max, ) } /// Payment intents matching a search query (`status:"succeeded"`). export async fn search(c: Client, query: string, options: RequestOptions = {}): SearchPage< PaymentIntent, > ! StripeError { return try await request( c, "GET", "/v1/payment_intents/search", { query }, options, j => try j as SearchPage, ) }