mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
76 lines
2.7 KiB
JavaScript
76 lines
2.7 KiB
JavaScript
/***********************************************************************************************************************************************************************
|
|
* File Name: paypal.service.js
|
|
* Type of Program: Service
|
|
* Description: PayPal Orders API helpers — create order, capture order.
|
|
* Uses client-side JS SDK button → server capture flow.
|
|
* Author: Kenneth Obsequio (@lash0000)
|
|
* Date Created: Jun. 6, 2026
|
|
***********************************************************************************************************************************************************************/
|
|
const axios = require('axios');
|
|
|
|
const BASE_URL = process.env.PAYPAL_ENV === 'live'
|
|
? 'https://api-m.paypal.com'
|
|
: 'https://api-m.sandbox.paypal.com';
|
|
|
|
const getAccessToken = async () => {
|
|
const { data } = await axios.post(
|
|
`${BASE_URL}/v1/oauth2/token`,
|
|
'grant_type=client_credentials',
|
|
{
|
|
auth: {
|
|
username: process.env.PAYPAL_CLIENT_ID,
|
|
password: process.env.PAYPAL_CLIENT_SECRET,
|
|
},
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
}
|
|
);
|
|
return data.access_token;
|
|
};
|
|
|
|
exports.createOrder = async ({ amount, currency = 'USD', referenceId, returnUrl, cancelUrl }) => {
|
|
const token = await getAccessToken();
|
|
const { data } = await axios.post(
|
|
`${BASE_URL}/v2/checkout/orders`,
|
|
{
|
|
intent: 'CAPTURE',
|
|
purchase_units: [{
|
|
reference_id: referenceId,
|
|
amount: { currency_code: currency, value: String(amount) },
|
|
}],
|
|
application_context: {
|
|
return_url: returnUrl ?? `${process.env.FRONTEND_URL}/plans/checkout`,
|
|
cancel_url: cancelUrl ?? `${process.env.FRONTEND_URL}/plans/checkout?cancelled=true`,
|
|
brand_name: 'Philproperties',
|
|
user_action: 'PAY_NOW',
|
|
},
|
|
},
|
|
{ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } }
|
|
);
|
|
return data;
|
|
};
|
|
|
|
exports.captureOrder = async (orderId) => {
|
|
const token = await getAccessToken();
|
|
const { data } = await axios.post(
|
|
`${BASE_URL}/v2/checkout/orders/${orderId}/capture`,
|
|
{},
|
|
{ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } }
|
|
);
|
|
return data; // { id, status, purchase_units, payer }
|
|
};
|
|
|
|
exports.refundCapture = async (captureId, amount, currency = 'USD') => {
|
|
const token = await getAccessToken();
|
|
const { data } = await axios.post(
|
|
`${BASE_URL}/v2/payments/captures/${captureId}/refund`,
|
|
{
|
|
amount: {
|
|
value: String(amount),
|
|
currency_code: currency,
|
|
},
|
|
},
|
|
{ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } }
|
|
);
|
|
return data; // { id, status, amount, ... }
|
|
};
|