|
| 1 | +import axios from 'axios' |
| 2 | + |
| 3 | +type AuthProps = { |
| 4 | + username: string |
| 5 | + password: string |
| 6 | +} |
| 7 | + |
| 8 | +type PaypalEnvironment = 'LIVE' | 'SANDBOX' |
| 9 | + |
| 10 | +type Currencies = |
| 11 | + | 'USD' |
| 12 | + | 'NZD' |
| 13 | + | 'NOK' |
| 14 | + | 'PHP' |
| 15 | + | 'PLN' |
| 16 | + | 'GBP' |
| 17 | + | 'RUB' |
| 18 | + | 'SGD' |
| 19 | + | 'SEK' |
| 20 | + | 'CHF' |
| 21 | + | 'THB' |
| 22 | + | 'CZK' |
| 23 | + | 'DKK' |
| 24 | + | 'EUR' |
| 25 | + | 'HKD' |
| 26 | + | 'HUF' |
| 27 | + | 'ILS' |
| 28 | + | 'JPY' |
| 29 | + | 'MYR' |
| 30 | + | 'MXN' |
| 31 | + | 'TWD' |
| 32 | + | 'AUD' |
| 33 | + | 'BRL' |
| 34 | + | 'CAD' |
| 35 | + | 'CNY' |
| 36 | + |
| 37 | +type TransactionConfig = { |
| 38 | + PAYPAL_ENVIRONMENT: PaypalEnvironment |
| 39 | + PAYPAL_CLIENT_ID: string |
| 40 | + PAYPAL_SECRET_ID: string |
| 41 | +} |
| 42 | + |
| 43 | +type CreateTokenOptions = TransactionConfig |
| 44 | + |
| 45 | +type TransactionOptions = { |
| 46 | + amount: number |
| 47 | + payment_description: string |
| 48 | + return_url?: string |
| 49 | + cancel_url?: string |
| 50 | + currency?: Currencies |
| 51 | +} |
| 52 | + |
| 53 | +type TransactionData = { |
| 54 | + intent: 'sale' |
| 55 | + payer: { |
| 56 | + payment_method: 'paypal' |
| 57 | + } |
| 58 | + transactions: { |
| 59 | + amount: { |
| 60 | + total: number |
| 61 | + currency: Currencies |
| 62 | + } |
| 63 | + description: string |
| 64 | + }[] |
| 65 | + redirect_urls: { |
| 66 | + return_url: string |
| 67 | + cancel_url: string |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +type CreateTokenResponse = { |
| 72 | + access_token: string |
| 73 | +} |
| 74 | + |
| 75 | +type CreateTransactionResponse = { |
| 76 | + orderId?: string |
| 77 | +} |
| 78 | + |
| 79 | +module Paypal { |
| 80 | + const getUrl = (env: PaypalEnvironment): string => { |
| 81 | + return env === 'LIVE' |
| 82 | + ? 'https://api-m.paypal.com' |
| 83 | + : 'https://api-m.sandbox.paypal.com' |
| 84 | + } |
| 85 | + |
| 86 | + export const createToken = async ( |
| 87 | + options: CreateTokenOptions |
| 88 | + ): Promise<CreateTokenResponse> => { |
| 89 | + try { |
| 90 | + const PAYPAL_API = getUrl(options.PAYPAL_ENVIRONMENT) |
| 91 | + const url: string = `${PAYPAL_API}/v1/oauth2/token` |
| 92 | + |
| 93 | + const auth: AuthProps = { |
| 94 | + username: options.PAYPAL_CLIENT_ID, |
| 95 | + password: options.PAYPAL_SECRET_ID |
| 96 | + } |
| 97 | + |
| 98 | + const response = await axios({ |
| 99 | + method: 'post', |
| 100 | + url, |
| 101 | + data: 'grant_type=client_credentials', // => this is mandatory x-www-form-urlencoded. DO NOT USE json format for this |
| 102 | + headers: { |
| 103 | + Accept: 'application/json', |
| 104 | + 'Content-Type': 'application/x-www-form-urlencoded', // => needed to handle data parameter |
| 105 | + 'Accept-Language': 'en_US', |
| 106 | + 'Access-Control-Allow-Origin': '*' |
| 107 | + }, |
| 108 | + auth |
| 109 | + }) |
| 110 | + |
| 111 | + return { |
| 112 | + access_token: response.data.access_token |
| 113 | + } |
| 114 | + } catch (err: any) { |
| 115 | + throw Error(err) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + export const createTransaction = async ( |
| 120 | + transaction: TransactionOptions, |
| 121 | + configs: TransactionConfig |
| 122 | + ): Promise<CreateTransactionResponse> => { |
| 123 | + try { |
| 124 | + const PAYPAL_API: string = getUrl(configs.PAYPAL_ENVIRONMENT) |
| 125 | + const url: string = `${PAYPAL_API}/v1/payments/payment` |
| 126 | + |
| 127 | + const token = await createToken(configs) |
| 128 | + |
| 129 | + const data: TransactionData = { |
| 130 | + intent: 'sale', |
| 131 | + payer: { |
| 132 | + payment_method: 'paypal' |
| 133 | + }, |
| 134 | + transactions: [ |
| 135 | + { |
| 136 | + amount: { |
| 137 | + total: transaction.amount, |
| 138 | + currency: transaction.currency ?? 'USD' |
| 139 | + }, |
| 140 | + description: transaction.payment_description |
| 141 | + } |
| 142 | + ], |
| 143 | + redirect_urls: { |
| 144 | + return_url: transaction.return_url ?? 'https://example.com', |
| 145 | + cancel_url: transaction.cancel_url ?? 'https://example.com' |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + const response = await axios.post(url, data, { |
| 150 | + headers: { |
| 151 | + Authorization: 'Bearer ' + token, |
| 152 | + 'Content-Type': 'application/json' |
| 153 | + } |
| 154 | + }) |
| 155 | + |
| 156 | + let orderId: undefined | string |
| 157 | + |
| 158 | + for (let link of response.data.links) { |
| 159 | + if (link.rel === 'approval_url') { |
| 160 | + orderId = link.href.match(/EC-\w+/)[0] |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return { orderId } |
| 165 | + } catch (err: any) { |
| 166 | + throw Error(err) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +const paypal = Paypal |
| 172 | +export default paypal |
0 commit comments