0

Create batch payout

by
Published Apr 8, 2025

Creates a batch payout.

Script paypal Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Paypal = {
3
  clientId: string;
4
  clientSecret: string;
5
};
6

7
async function getToken(auth: Paypal): Promise<string> {
8
  const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
9
  const response = await fetch(url, {
10
    method: "POST",
11
    headers: {
12
      Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
13
    },
14
    body: new URLSearchParams({
15
      grant_type: "client_credentials",
16
    }),
17
  });
18
  if (!response.ok) {
19
    const text = await response.text();
20
    throw new Error(`Could not get token: ${response.status} ${text}`);
21
  }
22
  const json = await response.json();
23
  return json.access_token;
24
}
25
/**
26
 * Create batch payout
27
 * Creates a batch payout.
28
 */
29
export async function main(
30
  auth: Paypal,
31
  PayPal_Request_Id: string,
32
  body: {
33
    sender_batch_header: {
34
      sender_batch_id?: string;
35
      recipient_type?: string;
36
      email_subject?: string;
37
      email_message?: string;
38
      note?: string;
39
    };
40
    items: {
41
      recipient_type?: string;
42
      amount: { currency: string; value: string };
43
      note?: string;
44
      receiver: string;
45
      sender_item_id?: string;
46
      recipient_wallet?: string;
47
      alternate_notification_method?: {
48
        phone?: {
49
          country_code: string;
50
          national_number: string;
51
          extension_number?: string;
52
        };
53
      };
54
      notification_language?: string;
55
      application_context?: {
56
        social_feed_privacy?: string;
57
        holler_url?: string;
58
        logo_url?: string;
59
      };
60
      purpose?:
61
        | "AWARDS"
62
        | "PRIZES"
63
        | "DONATIONS"
64
        | "GOODS"
65
        | "SERVICES"
66
        | "REBATES"
67
        | "CASHBACK"
68
        | "DISCOUNTS"
69
        | "NON_GOODS_OR_SERVICES";
70
    }[];
71
  },
72
) {
73
  const token = await getToken(auth);
74
  const url = new URL(`https://api-m.paypal.com/v1/payments/payouts`);
75

76
  const response = await fetch(url, {
77
    method: "POST",
78
    headers: {
79
      "PayPal-Request-Id": PayPal_Request_Id,
80
      "Content-Type": "application/json",
81
      Authorization: "Bearer " + token,
82
    },
83
    body: JSON.stringify(body),
84
  });
85
  if (!response.ok) {
86
    const text = await response.text();
87
    throw new Error(`${response.status} ${text}`);
88
  }
89
  return await response.json();
90
}
91