Horai Nuri
Horai Nuri

Reputation: 5578

How to configure Stripe Connect on Django to collect fees on transactions?

After several research I've come to a conclusion that Stripe Connect documentation on Django wasn't really furnished. I've been struggling to create a simple marketplace using the Standalone Package (OAuth).

The goal of the plateforme is to let users do transactions between each other and collect fees on each charges. To achieve this I'm using django-allauth and stripe.

I've set up django-allauth so users can already create an account on stripe and login on my website. The problem I'm facing now is on how to charge users between each other.

Here is the error I get when I'm trying to make a payment from two different accounts, UserA (customer) to UserB (publisher/seller) :

stripe.error.InvalidRequestError: Request req_*************: The 'destination' param cannot be set to your own account.

Here is my views

customer_id = request.user.profile.stripe_id

if request.method == 'POST':
    try:
        gig = Gig.objects.get(id=request.POST.get('gig_id'))
    except Gig.DoesNotExist:
        return redirect('/')


    token = request.POST['stripeToken']

    # Create a charge: this will charge the user's card
    try:
        customer = stripe.Customer.retrieve(customer_id)
        customer.sources.create(source=token)

        destination_id = gig.user.socialaccount_set.get().uid 

        charge = stripe.Charge.create(
            amount=5000,  # Amount in cents
            currency="chf",
            customer=customer,
            description=gig.title,
            application_fee=123,
            destination=destination_id, #'acct_**************'
        )

    except stripe.error.CardError as e:
        # The card has been declined
        pass

Gig is my product model and Profile is my profile model where I keep stripe_id

I've read the documentation and I'm providing the account destination with the seller's destination id. Am I missing something ? Why do I get this error ?

Upvotes: 2

Views: 1721

Answers (1)

Horai Nuri
Horai Nuri

Reputation: 5578

Success, the first payment went through ! Here's a short tutorial on how to setup Stripe connect:

For the standalone package, here is what I use Django-allauth and Stripe.

Things you need to know for making it work on test mode :

a. configure django-allauth with stripe (create a customer each time an account is created).

b. You cannot set the destination with the admin account destination ID if it's the one you used to create the app otherwise you'll get this error :

stripe.error.InvalidRequestError: Request req_*************: The 'destination' param cannot be set to your own account.

c. You need to make sure to put this code on your settings as you can proceed to do charges or transfers only if the scope is set to read_write, put this code in your settings.py

SOCIALACCOUNT_PROVIDERS = {
    'stripe': {
        'SCOPE': ['read_write'],
    } 
}

d. give a stripe_id to your profile model in order to give a unique id to each connected user.

e. Show the checkout form on the template.

Know that you'll probably have to create a second test account in order to make your payment in dev mode because you cannot use the admin account.

Don't hesitate to use the views.py code in the question.

Enjoy stripe connect on your marketplace !

Upvotes: 7

Related Questions