Вот ответ JSON:
json
Код: Выделить всё
{
"message": "Authorization token required"
}
Код: Выделить всё
const getAccessToken = async (): Promise => {
const PAYPAL_CLIENT_ID = process.env.PAYPAL_CLIENTID;
const PAYPAL_CLIENT_SECRET = process.env.PAYPAL_SECRET;
const PAYPAL_API_URL = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
const response = await axios.post(
PAYPAL_API_URL,
"grant_type=client_credentials",
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
auth: {
username: `${PAYPAL_CLIENT_ID}`,
password: `${PAYPAL_CLIENT_SECRET}`,
},
}
);
return response.data.access_token || "";
};
Код: Выделить всё
async createPaymentController(req: Request, res: Response, next: NextFunction): Promise {
const { price } = req.body;
const uniqueInvoiceId = uuidv4();
const token = await getAccessToken();
try {`your text`
const request = new paypal.orders.OrdersCreateRequest();
if (token) {
request.headers["Authorization"] = `Bearer ${token}`;
}
request.requestBody({
intent: "CAPTURE",
purchase_units: [{
invoice_id: uniqueInvoiceId,
amount: { currency_code: "USD", value: price },
}],
application_context: {
brand_name: "Your Company Name",
landing_page: "LOGIN",
shipping_preference: "GET_FROM_FILE",
user_action: "PAY_NOW",
return_url: `${redirectUrl.return_url}`,
cancel_url: `${redirectUrl.cancel_url}`,
},
});
const response = await client.execute(request);
const approvalLink = response.result.links?.find((link: any) => link.rel === "approve")?.href;
if (approvalLink) res.status(200).json({ link: approvalLink });
else res.status(500).json({ message: "Approval link not found." });
} catch (error) {
next(error);
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... uthorizati