PacktPublishing / Django-3-by-Example

Django 3 by Example (3rd Edition) published by Packt
https://djangobyexample.com/
MIT License
715 stars 690 forks source link

Chapter 8:Payment AuthorizationError #73

Closed allielbadry closed 3 years ago

allielbadry commented 3 years ago

i did everything the book says and when i place order i get authorization error caused by this line client_token = gateway.client_token.generate()

`import braintree from django.shortcuts import render, redirect, get_object_or_404 from django.conf import settings from order.models import Order

gateway = braintree.BraintreeGateway(settings.BRAINTREE_CONF)

def payment_process(request): order_id = request.session.get('order_id') order = get_object_or_404(Order, id=order_id) total_cost = order.get_total_cost()

if request.method == 'POST':
    # retrieve nonce
    nonce = request.POST.get('payment_method_nonce', None)
    # create and submit transaction
    result = gateway.transaction.sale({
        'amount': f'{total_cost:.2f}',
        'payment_method_nonce': nonce,
        'options': {
            'submit_for_settlement': True
        }
    })
    if result.is_success:
        # mark the order as paid
        order.paid = True
        # store the unique transaction id
        order.braintree_id = result.transaction.id
        order.save()
        return redirect('payment:done')
    else:
        return redirect('payment:canceled')
else:
    # generate token
    client_token = gateway.client_token.generate()
    return render(request,
                  'payment/process.html',
                  {'order': order,
                   'client_token': client_token})`
michaurbanczyk commented 3 years ago

Hi @allielbadry ,

can you please show me your settings file? Braintree part.

allielbadry commented 3 years ago

Thank you I solved it when I correct the braintree settings it was a problem I didn't write my ID correctly