-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (62 loc) · 2.09 KB
/
server.js
File metadata and controls
73 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const PAYPAL_CLIENT = 'YOUR_PAYPAL_CLIENT_ID';
const PAYPAL_SECRET = 'YOUR_PAYPAL_SECRET';
const PAYPAL_API = 'https://api-m.sandbox.paypal.com'; // Sandbox URL, change to live URL for production
app.post('/send-payment', async (req, res) => {
const { recipientEmail, amount, currency } = req.body;
if (!recipientEmail || !amount || !currency) {
return res.status(400).json({ error: 'Invalid request data' });
}
try {
// Get OAuth token
const authResponse = await axios({
url: `${PAYPAL_API}/v1/oauth2/token`,
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
auth: {
username: PAYPAL_CLIENT,
password: PAYPAL_SECRET
},
data: 'grant_type=client_credentials'
});
const accessToken = authResponse.data.access_token;
// Create payment
const payoutData = {
sender_batch_header: {
email_subject: 'You have a payment'
},
items: [{
recipient_type: 'EMAIL',
amount: {
value: amount,
currency: currency
},
receiver: recipientEmail,
note: 'Payment note',
sender_item_id: 'item-1'
}]
};
const paymentResponse = await axios({
url: `${PAYPAL_API}/v1/payments/payouts`,
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
data: payoutData
});
res.json(paymentResponse.data);
} catch (error) {
console.error(error);
res.status(500).send('Something went wrong');
}
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});