import fetch from "node-fetch"; // set some important variables // const { CLIENT_ID, APP_SECRET } = process.env; // const base = "https://api-m.sandbox.paypal.com"; async function handleResponse(response) { if (response.status === 200 || response.status === 201) { return response.json(); } const errorMessage = await response.text(); throw new Error(errorMessage); } // generate access token export async function generateAccessToken(base,PaypalClientId,PaypalAppSecret) { console.log(PaypalClientId + ":" + PaypalAppSecret); const auth = Buffer.from(PaypalClientId + ":" + PaypalAppSecret).toString("base64"); console.log(auth); const response = await fetch(`${base}/v1/oauth2/token`, { method: "post", body: "grant_type=client_credentials", headers: { Authorization: `Basic ${auth}`, }, }); const jsonData = await handleResponse(response); return jsonData.access_token; } // generate client token export async function generateClientToken(base,PaypalClientId,PaypalAppSecret) { const accessToken = await generateAccessToken(base, PaypalClientId,PaypalAppSecret); const response = await fetch(`${base}/v1/identity/generate-token`, { method: "post", headers: { Authorization: `Bearer ${accessToken}`, "Accept-Language": "en_US", "Content-Type": "application/json", }, }); console.log('response', response.status) const jsonData = await handleResponse(response); return jsonData.client_token; } // call the create order method export async function createOrder(base,data,PaypalClientId,PaypalAppSecret) { const purchaseAmount = data.price;//"100.00"; // TODO: pull prices from a database const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v2/checkout/orders`; console.log(url) const response = await fetch(url, { method: "post", headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, }, body: JSON.stringify({ intent: "CAPTURE", purchase_units: [ { amount: { currency_code: "USD", value: purchaseAmount, }, }, ], }), }); return handleResponse(response); } // capture payment for an order export async function capturePayment(base,orderId,PaypalClientId,PaypalAppSecret) { const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v2/checkout/orders/${orderId}/capture`; const response = await fetch(url, { method: "post", headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, }, }); return handleResponse(response); } export async function verifyWebHook(base,PaypalClientId,PaypalAppSecret){ const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v1/notifications/verify-webhook-signature`; // fetch('https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature', { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}`, }, body: JSON.stringify({ "transmission_id": "69cd13f0-d67a-11e5-baa3-778b53f4ae55", "transmission_time": "2016-02-18T20:01:35Z", "cert_url": "cert_url", // PAYPAL-CERT-URL "auth_algo": "SHA256withRSA", // PAYPAL-AUTH-ALGO "transmission_sig": "lmI95Jx3Y9nhR5SJWlHVIWpg4AgFk7n9bCHSRxbrd8A9zrhdu2rMyFrmz+Zjh3s3boXB07VXCXUZy/UFzUlnGJn0wDugt7FlSvdKeIJenLRemUxYCPVoEZzg9VFNqOa48gMkvF+XTpxBeUx/kWy6B5cp7GkT2+pOowfRK7OaynuxUoKW3JcMWw272VKjLTtTAShncla7tGF+55rxyt2KNZIIqxNMJ48RDZheGU5w1npu9dZHnPgTXB9iomeVRoD8O/jhRpnKsGrDschyNdkeh81BJJMH4Ctc6lnCCquoP/GzCzz33MMsNdid7vL/NIWaCsekQpW26FpWPi/tfj8nLA==", "webhook_id": "1JE4291016473214C", "webhook_event": { "id": "8PT597110X687430LKGECATA", "create_time": "2013-06-25T21:41:28Z", "resource_type": "authorization", "event_type": "PAYMENT.AUTHORIZATION.CREATED", "summary": "A payment authorization was created", "resource": { "id": "2DC87612EK520411B", "create_time": "2013-06-25T21:39:15Z", "update_time": "2013-06-25T21:39:17Z", "state": "authorized", "amount": { "total": "7.47", "currency": "USD", "details": { "subtotal": "7.47" } }, "parent_payment": "PAY-36246664YD343335CKHFA4AY", "valid_until": "2013-07-24T21:39:15Z", "links": [ { "href": "https://api-m.paypal.com/v1/payments/authorization/2DC87612EK520411B", "rel": "self", "method": "GET" }, { "href": "https://api-m.paypal.com/v1/payments/authorization/2DC87612EK520411B/capture", "rel": "capture", "method": "POST" }, { "href": "https://api-m.paypal.com/v1/payments/authorization/2DC87612EK520411B/void", "rel": "void", "method": "POST" }, { "href": "https://api-m.paypal.com/v1/payments/payment/PAY-36246664YD343335CKHFA4AY", "rel": "parent_payment", "method": "GET" } ] } } })// end body });// end fetch if (response.status === 200 || response.status === 201) { return response.json(); } const errorMessage = await response.text(); throw new Error(errorMessage); } export async function payout(base,PaypalClientId,PaypalAppSecret,data){ const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v1/payments/payouts`; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${accessToken}` }, body: data }); return handleResponse(response); } // capture payment for an order export async function refundPayment(base,PaypalClientId,PaypalAppSecret,orderId) { const accessToken = await generateAccessToken(base, PaypalClientId,PaypalAppSecret); const url = `${base}/v2/payments/captures/${capture_id}/refund`; const response = await fetch(url, { method: "post", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${accessToken}`, 'PayPal-Request-Id': '123e4567-e89b-12d3-a456-426655440020', 'Prefer': 'return=representation' }, body: JSON.stringify( { "amount": { "value": "10.00", "currency_code": "USD" }, "invoice_id": "INVOICE-123", "note_to_payer": "DefectiveProduct", "payment_instruction": { "platform_fees": [ { "amount": { "currency_code": "USD", "value": "1.00" } } ] } }) }); return handleResponse(response); } export async function create_subscription_product(base,PaypalClientId,PaypalAppSecret,data){ const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v1/catalogs/products`; // let data = { // "consumer": { // "accountNumber":1181198218909172527, // "merchantId":"5KW8F2FXKX5HA" // }, // "merchant":{ // "accountNumber":1659371090107732880, // "merchantId":"2J6QB8YJQSJRJ" // }, // "apiCaller":{ // "clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP", // "appId":"APP-6DV794347V142302B", // "payerId":"2J6QB8YJQSJRJ", // "accountNumber":"1659371090107732880" // }, // "scopes":["https://api-m.paypal.com/v1/subscription/.*","https://uri.paypal.com/services/subscription","openid"] // } const response = await fetch(url, { //fetch('https://api-m.sandbox.paypal.com/v1/catalogs/products', { method: 'POST', headers: { // 'X-PAYPAL-SECURITY-CONTEXT': '{"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["https://api-m.paypal.com/v1/subscription/.*","https://uri.paypal.com/services/subscription","openid"]}', 'X-PAYPAL-SECURITY-CONTEXT': JSON.stringify(data), 'Content-Type': 'application/json', 'Accept': 'application/json', 'PayPal-Request-Id': 'PRODUCT-18062019-001', 'Prefer': 'return=representation' }, body: JSON.stringify({ "name": "Video Streaming Service", "description": "Video streaming service", "type": "SERVICE", "category": "SOFTWARE", "image_url": "https://example.com/streaming.jpg", "home_url": "https://example.com/home" }) }); return handleResponse(response); } export async function create_subscription_plan(base,PaypalClientId,PaypalAppSecret,data){ const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v1/billing/plans`; // let data = { // "consumer":{ // "accountNumber":1181198218909172527, // "merchantId":"5KW8F2FXKX5HA" // }, // "merchant":{ // "accountNumber":1659371090107732880, // "merchantId":"2J6QB8YJQSJRJ" // }, // "apiCaller":{ // "clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP", // "appId":"APP-6DV794347V142302B", // "payerId":"2J6QB8YJQSJRJ", // "accountNumber":"1659371090107732880" // }, // "scopes":["https://api-m.paypal.com/v1/subscription/.*","https://uri.paypal.com/services/subscription","openid"] // }; const response = await fetch(url, { // fetch('https://api-m.sandbox.paypal.com/v1/billing/plans', { method: 'POST', headers: { 'X-PAYPAL-SECURITY-CONTEXT': JSON.stringify(data), 'Content-Type': 'application/json', 'Accept': 'application/json', 'PayPal-Request-Id': 'PLAN-18062019-001', 'Prefer': 'return=representation' }, body: JSON.stringify({ "product_id": "PROD-XXCD1234QWER65782", "name": "Video Streaming Service Plan", "description": "Video Streaming Service basic plan", "status": "ACTIVE", "billing_cycles": [ { "frequency": { "interval_unit": "MONTH", "interval_count": 1 }, "tenure_type": "TRIAL", "sequence": 1, "total_cycles": 2, "pricing_scheme": { "fixed_price": { "value": "3", "currency_code": "USD" } } }, { "frequency": { "interval_unit": "MONTH", "interval_count": 1 }, "tenure_type": "TRIAL", "sequence": 2, "total_cycles": 3, "pricing_scheme": { "fixed_price": { "value": "6", "currency_code": "USD" } } }, { "frequency": { "interval_unit": "MONTH", "interval_count": 1 }, "tenure_type": "REGULAR", "sequence": 3, "total_cycles": 12, "pricing_scheme": { "fixed_price": { "value": "10", "currency_code": "USD" } } } ], "payment_preferences": { "auto_bill_outstanding": true, "setup_fee": { "value": "10", "currency_code": "USD" }, "setup_fee_failure_action": "CONTINUE", "payment_failure_threshold": 3 }, "taxes": { "percentage": "10", "inclusive": false } }) }); return handleResponse(response); } export async function get_product_plan(base,PaypalClientId,PaypalAppSecret,data){ const accessToken = await generateAccessToken(base,PaypalClientId,PaypalAppSecret); const url = `${base}/v1/catalogs/products?page_size=2&page=1&total_required=true`; const response = await fetch(url, { // fetch('https://api-m.sandbox.paypal.com/v1/catalogs/products?page_size=2&page=1&total_required=true', { headers: { 'X-PAYPAL-SECURITY-CONTEXT': JSON.stringify(data), 'Content-Type': 'application/json', 'Accept': 'application/json', "Authorization": `Bearer ${accessToken}`, } }); return handleResponse(response); }