Stores a credit card in the card vault

Stores a credit card in the card vault. Credit cards cannot be sent to the Checkout API directly. They must be sent to the card vault, which in response will return a session ID. This session ID can then be used when calling the POST #{token}/payments.json endpoint. A session ID is valid only for a single call to the endpoint. The card vault has a static URL and is located at https://elb.deposit.shopifycs.com/sessions. It is also provided via the payment_url property on the Checkout resource.

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
 * Stores a credit card in the card vault
7
 * Stores a credit card in the card vault. Credit cards cannot be sent to the Checkout API directly. They must be sent to the card vault, which in response will return a session ID. This session ID can then be used when calling the POST #{token}/payments.json endpoint. A session ID is valid only for a single call to the endpoint. The card vault has a static URL and is located at https://elb.deposit.shopifycs.com/sessions. It is also provided via the payment_url property on the Checkout resource.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  body: {
12
    credit_card?: {
13
      first_name?: string;
14
      last_name?: string;
15
      month?: string;
16
      number?: string;
17
      verification_value?: string;
18
      year?: string;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://${auth.store_name}.myshopify.com/https:/elb.deposit.shopifycs.com/sessions`
26
  );
27

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