I have a problem with my Python script for my telegram bot, I have configured a rest API on Paypal, when the user clicks on the payment button on Telegram, the user should be redirected to the payment page, except that my script creating the token in the database gives access but does not redirect the user to pay.
On Paypal I can’t find the links for return_url
and cancel_url
anywhere.
Here are my functions (I’m not sending you all my code):
async def start(update: Update, context: CallbackContext) -> None:
user_id = update.effective_user.id
if check_payment(user_id):
# L'utilisateur a déjà payé, donc on peut activer le bot
await context.bot.send_message(chat_id=update.effective_chat.id, text='Bienvenue, voici la liste des commandes disponibles:nn /start - Lancer le bot et liste toutes les commandes du botn/setdate - Modification de la prochaine commande fournisseurn/countdown - Afficher un compte à rebours jusqu’à la prochaine commande fournisseurn/list - Affiche toute la liste des commandes du bot')
else:
# L'utilisateur n'a pas encore payé, donc on affiche le bouton de paiement
await start_paiement(update, context)
paypalrestsdk.configure({
"mode": "live", # sandbox ou live
"client_id": "-",
"client_secret": "-" })
cnx = mysql.connector.connect(user='-', password='-',
host='localhost',
database='-')
def generate_token():
return binascii.hexlify(os.urandom(20)).decode()
def check_payment(user_id):
cursor = cnx.cursor()
query = ("SELECT token FROM tokens WHERE user_id = %s")
cursor.execute(query, (user_id,))
result = cursor.fetchone()
return result is not None
async def start_paiement(update: Update, context: CallbackContext) -> None:
user_id = update.effective_user.id
if check_payment(user_id):
# L'utilisateur a déjà payé, donc on démarre le bot normalement
await context.bot.send_message(chat_id=update.effective_chat.id, text="Bienvenue !")
else:
# L'utilisateur n'a pas encore payé, donc on affiche le bouton de paiement
keyboard = [[InlineKeyboardButton("Payer", callback_data='pay')]]
reply_markup = InlineKeyboardMarkup(keyboard)
welcome_message = "Bienvenue, nn Veuillez effectuer le paiement de 100 euros n pour un accès à vie à ce bot. nn Si vous souhaitez d'abord tester nos différents bots avant de procéder à votre achat, vous pouvez rejoindre notre groupe <a href='-'>exemple</a>"
await context.bot.send_message(chat_id=update.effective_chat.id, text=welcome_message, reply_markup=reply_markup, parse_mode='HTML', disable_web_page_preview=True)
async def pay(update: Update, context: CallbackContext) -> None:
query = update.callback_query
await query.answer() # gérer les callbacks du bouton de paiement
payment = paypalrestsdk.Payment({
"intent": "sale",
# Configurez votre retour d'appel et les URL d'annulation ici
"redirect_urls": {
"return_url": "-m",
"cancel_url": "-"},
"payer": {"payment_method": "paypal"},
# Configurez votre paiement ici
"transactions": [{
"item_list": {
"items": [{
"name": "Accès au Bot",
"sku": "item",
"price": '100.00',
"currency": "EUR",
"quantity": 1}]},
"amount": {
"total": '100.00',
"currency": "EUR"},
"description": "Paiement pour l'accès au Bot"}]})
if payment.create():
print('Paiement créé avec succès')
token = generate_token()
print(f'Le jeton pour l'utilisateur est: {token}')
cursor = cnx.cursor()
query = ("INSERT INTO tokens "
"(user_id, token) "
"VALUES (%s, %s)")
cursor.execute(query, (update.effective_user.id, token))
cnx.commit()
else:
print('Échec de la création du paiement PayPal')
print(payment.error)
Can you help me resolve the problem or find a solution?
2
Answers
Thank you for your response can you give me an example of non-obsolete code or the paypal documentation to integrate the script with python telegram bot? :)
If you are redirecting a payer over to PayPal to approve a payment, the return_url and cancel_url are your own URLs where they will return afterward. In the case of return_url, you need to show a confirmation page and when the transaction is confirmed, capture/execute the payment.
I’m not sure what the "paypalrestsdk" is in your code, but all REST SDKs from PayPal have been deprecated and should not be used for new integrations. Integrate with the REST API directly (use client id and secret to obtain an access token, then HTTPS request/response in JSON format)