PayPal API

const { Client, GatewayIntentBits, Collection, Events, EmbedBuilder, PermissionsBitField, Permissions, MessageManager, Embed } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const fs = require('fs');

client.commands = new Collection();
const paypal = require('paypal-rest-sdk');
client.config = require("./config.json");
const prefix = '!';
const functions = fs.readdirSync("./functions").filter(file => file.endsWith(".js"));
const eventFiles = fs.readdirSync("./events").filter(file => file.endsWith(".js"));
const commandFolders = fs.readdirSync("./commands");

(async () => {
for (file of functions) {
require(`./functions/${file}`)(client);
}
client.handleEvents(eventFiles, "./events");
client.handleCommands(commandFolders, "./commands");

})();

paypal.configure({
'mode': 'sandbox', // set to 'live' for production
'client_id': '',
'client_secret': ''
});

client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();

if (command === 'pay') {
const paymentData = {
'intent': 'sale',
'payer': {
'payment_method': 'paypal'
},
'redirect_urls': {
'return_url': 'https://example.com/success',
'cancel_url': 'https://example.com/cancel'
},
'transactions': [{
'item_list': {
'items': [{
'name': 'Payment for Discord bot',
'sku': 'DISCORDBOT01',
'price': '35.00',
'currency': 'EUR',
'quantity': 1
}]
},
'amount': {
'currency': 'EUR',
'total': '35.00'
},
'description': 'Payment for Discord bot service'
}]
};

const { Client, GatewayIntentBits, Collection, Events, EmbedBuilder, PermissionsBitField, Permissions, MessageManager, Embed } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const fs = require('fs');

client.commands = new Collection();
const paypal = require('paypal-rest-sdk');
client.config = require("./config.json");
const prefix = '!';
const functions = fs.readdirSync("./functions").filter(file => file.endsWith(".js"));
const eventFiles = fs.readdirSync("./events").filter(file => file.endsWith(".js"));
const commandFolders = fs.readdirSync("./commands");

(async () => {
for (file of functions) {
require(`./functions/${file}`)(client);
}
client.handleEvents(eventFiles, "./events");
client.handleCommands(commandFolders, "./commands");

})();

paypal.configure({
'mode': 'sandbox', // set to 'live' for production
'client_id': '',
'client_secret': ''
});

client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();

if (command === 'pay') {
const paymentData = {
'intent': 'sale',
'payer': {
'payment_method': 'paypal'
},
'redirect_urls': {
'return_url': 'https://example.com/success',
'cancel_url': 'https://example.com/cancel'
},
'transactions': [{
'item_list': {
'items': [{
'name': 'Payment for Discord bot',
'sku': 'DISCORDBOT01',
'price': '35.00',
'currency': 'EUR',
'quantity': 1
}]
},
'amount': {
'currency': 'EUR',
'total': '35.00'
},
'description': 'Payment for Discord bot service'
}]
};


paypal.payment.create(paymentData, (error, payment) => {
if (error) {
console.error(error);
message.reply('There was an error creating the payment. Please try again later.');
} else {
const approvalUrl = payment.links.find(link => link.rel === 'approval_url').href;
message.reply(`Please approve the payment at ${approvalUrl}`);

const paymentId = payment.id;
const checkPayment = setInterval(() => {
paypal.payment.get(paymentId, (error, payment) => {
if (error) {
console.error(error);
clearInterval(checkPayment);
message.reply('There was an error checking the payment status. Please contact the bot owner.');
} else {
if (payment.state === 'approved') {
clearInterval(checkPayment);
message.reply('Payment successful! Thank you for your payment.');
}
}
});
}, 5000);
}
});
}
});




client
.login(client.config.token)
.then(() => {
console.log(`Cliend logged in as ${client.user.username}`)
client.user.setActivity(`Support`)


})

paypal.payment.create(paymentData, (error, payment) => {
if (error) {
console.error(error);
message.reply('There was an error creating the payment. Please try again later.');
} else {
const approvalUrl = payment.links.find(link => link.rel === 'approval_url').href;
message.reply(`Please approve the payment at ${approvalUrl}`);

const paymentId = payment.id;
const checkPayment = setInterval(() => {
paypal.payment.get(paymentId, (error, payment) => {
if (error) {
console.error(error);
clearInterval(checkPayment);
message.reply('There was an error checking the payment status. Please contact the bot owner.');
} else {
if (payment.state === 'approved') {
clearInterval(checkPayment);
message.reply('Payment successful! Thank you for your payment.');
}
}
});
}, 5000);
}
});
}
});




client
.login(client.config.token)
.then(() => {
console.log(`Cliend logged in as ${client.user.username}`)
client.user.setActivity(`Support`)


})
No errors, but doesn't send (The PayPal API stuff w. !pay)
4 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Syjalo
Syjalo2y ago
Ask support from paypal-rest-sdk. Not related to discord.js.
Marcus
MarcusOP2y ago
Where can I get their support?
d.js docs
d.js docs2y ago
Tag suggestion for @marqusdev: The message and interaction events were renamed to messageCreate and interactionCreate respectively, to bring the library in line with Discord's naming conventions.
- client.on('message', message => { ... });
+ client.on('messageCreate', message => { ... });
- client.on('interaction', interaction => { ... });
+ client.on('interactionCreate', interaction => { ... });
- client.on('message', message => { ... });
+ client.on('messageCreate', message => { ... });
- client.on('interaction', interaction => { ... });
+ client.on('interactionCreate', interaction => { ... });
Want results from more Discord servers?
Add your server