124 lines
3.6 KiB
JavaScript
124 lines
3.6 KiB
JavaScript
import fetch from "node-fetch";
|
|
|
|
// set some important variables
|
|
const { CLIENT_ID, APP_SECRET } = process.env;
|
|
const base = "https://api-m.sandbox.paypal.com";
|
|
|
|
// call the create order method
|
|
export async function createOrder(data,PaypalClientId,PaypalAppSecret) {
|
|
const purchaseAmount = data.price;//"100.00"; // TODO: pull prices from a database
|
|
const accessToken = await generateAccessToken(PaypalClientId,PaypalAppSecret);
|
|
const url = `${base}/v2/checkout/orders`;
|
|
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(orderId,PaypalClientId,PaypalAppSecret) {
|
|
const accessToken = await generateAccessToken(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);
|
|
}
|
|
|
|
// generate access token
|
|
export async function generateAccessToken(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(PaypalClientId,PaypalAppSecret) {
|
|
const accessToken = await generateAccessToken(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;
|
|
}
|
|
|
|
async function handleResponse(response) {
|
|
if (response.status === 200 || response.status === 201) {
|
|
return response.json();
|
|
}
|
|
|
|
const errorMessage = await response.text();
|
|
throw new Error(errorMessage);
|
|
}
|
|
|
|
|
|
// capture payment for an order
|
|
export async function refundPayment(orderId) {
|
|
const accessToken = await generateAccessToken();
|
|
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);
|
|
}
|
|
|