Creates a new payment

Creates a payment on a checkout using the session ID returned by the card vault

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a new payment
7
 * Creates a payment on a checkout using the session ID returned by the card vault
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  token: string,
13
  body: {
14
    payment?: {
15
      amount?: string;
16
      request_details?: {
17
        accept_language?: string;
18
        ip_address?: string;
19
        user_agent?: string;
20
        [k: string]: unknown;
21
      };
22
      session_id?: string;
23
      unique_token?: string;
24
      [k: string]: unknown;
25
    };
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/checkouts/${token}/payments.json`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      "X-Shopify-Access-Token": auth.token,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47