Skip to main content

Python SDK

Le SDK Python officiel pour intégrer SahelPay dans vos applications Python, Django ou Flask.

Installation

pip install sahelpay
En attendant la publication sur PyPI, clonez le repo GitHub :
git clone https://github.com/dione24/sahelpay-sdks.git
cd sahelpay-sdks/python
pip install -e .

Configuration

from sahelpay import SahelPay

sahelpay = SahelPay('sk_test_xxx')

Créer un lien de paiement

link = sahelpay.payment_links.create(
    title='Commande #123',
    price=5000,
    redirect_url='https://votre-site.com/checkout/return'
)

# Rediriger le client
redirect(link.url)

Vérifier le statut

payment = sahelpay.payments.retrieve('txn_abc123')

print(payment.status)  # 'PENDING', 'SUCCESS', 'FAILED'
print(payment.amount)

Vérifier un webhook

from sahelpay import SahelPay

sahelpay = SahelPay('sk_test_xxx', webhook_secret='whsec_xxx')

# Flask
@app.route('/webhooks/sahelpay', methods=['POST'])
def webhook():
    raw_body = request.get_data(as_text=True)
    signature = request.headers.get('X-SahelPay-Signature')

    is_valid = sahelpay.webhooks.verify(raw_body, signature)

    if not is_valid:
        return jsonify({'error': 'Invalid signature'}), 401

    event = request.json

    if event['event'] == 'payment.success':
        # Mettre à jour la commande
        pass

    return jsonify({'received': True})

Django View

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from sahelpay import SahelPay
import json

sahelpay = SahelPay(settings.SAHELPAY_SECRET_KEY)

@csrf_exempt
def webhook(request):
    raw_body = request.body.decode('utf-8')
    signature = request.headers.get('X-SahelPay-Signature')

    is_valid = sahelpay.webhooks.verify(raw_body, signature)

    if not is_valid:
        return JsonResponse({'error': 'Invalid signature'}, status=401)

    event = json.loads(raw_body)

    if event['event'] == 'payment.success':
        # Mettre à jour la commande
        pass

    return JsonResponse({'received': True})

Méthodes de paiement

MéthodeDescriptionPays
ORANGE_MONEYOrange MoneyMali, Sénégal, CI
WAVEWaveMali, Sénégal, CI
MOOVMoov MoneyMali, Bénin, Togo
CARDCarte bancaireTous
payment = client.payments.create(
    amount=5000,
    provider='ORANGE_MONEY',  # ou WAVE, MOOV, CARD
    customer_phone='+22370123456',
    description='Commande #123'
)

# Informations de routing (automatique)
print(payment.gateway_used)    # Identifiant technique interne
print(payment.routing_reason)  # Explication du routing choisi
SahelPay utilise un Smart Routing automatique. Vous spécifiez la méthode de paiement (ex: ORANGE_MONEY) et SahelPay choisit le meilleur gateway en interne.

Capabilities

from sahelpay import has_capability, get_methods_with_capability

# Vérifier si Orange Money supporte les payouts
has_capability('ORANGE_MONEY', 'payouts')  # True

# Lister les méthodes avec QR code natif
get_methods_with_capability('qr_code')  # ['WAVE']

Paiement par Carte

Pour les paiements par carte, customer_name et customer_email sont requis :
payment = client.payments.create(
    amount=10000,
    provider='CARD',
    customer_phone='+22370123456',
    customer_name='Mamadou Diallo',       # Requis
    customer_email='[email protected]', # Requis
    return_url='https://votre-site.com/checkout/return'
)