0

I try to implement 2checkout to my django app (Python 3.7 and Django 2.2).

I'm using the Sandox and when I see API logs my orders are successfully passed. I also chose a URL for the Passback and selected Header Redirect in my account settings.

Unfortunately, I don't succeed in getting the redirection works :

I get this error after twocheckout.Charge.authorize(params) if successfully executed:

The view subscriptions.views.subscription_payment didn't return an HttpResponse object. It returned None instead.

When I manually add an HttpResponseRedirect like this:

return HttpResponseRedirect('/payment-success') the GET request is empty because it doesn't come from 2checkout.

I tried to follow this tutorial https://github.com/2Checkout/2checkout-python-tutorial and it doesn't mention any HttpResponseRedirect.

Could you please help me with this issue? It's really frustrating. I'm a beginner developper so maybe I missed something. Please feel free to ask any further information that would be helpful to understand my issue.

Many thanks in advance

Redjam
  • 474
  • 3
  • 12

2 Answers2

1

I was integrating the API so no need for Passback URL. The question was a non sense :) That's why the tutorial doesn't mention HttpResponseRedirect.

Redjam
  • 474
  • 3
  • 12
1

Because it has been asked in the comments, here is the code used to fulfil the need I had at that moment. Hope this could help someone else.

   def subscription_payment(request, pk):
    property = Property.objects.get(pk=pk)
    if request.method == 'POST':
        pay_token = request.POST.get('token')

        twocheckout.Api.auth_credentials({
            'private_key': '######-####-####-####-#######',
            'seller_id': '#########',
            'mode': 'sandbox'  # Uncomment to use Sandbox
        })

        data = serializers.serialize('json', Profile.objects.filter(user=request.user), fields=(
            'address_2', 'city', 'zip_code', 'country', 'phone_number'))
        data = json.loads(data)

        params = {
            'merchantOrderId': request.user.id,
            'token': pay_token,
            'currency': 'USD',
            'billingAddr': {
                'name': request.user.get_full_name(),
                'addrLine1': data[0]['fields']['address_2'],
                'city': data[0]['fields']['city'],
                'state': ' ',
                'zipCode': data[0]['fields']['zip_code'],
                'country': data[0]['fields']['country'],
                'email': request.user.email,
                'phoneNumber': data[0]['fields']['phone_number'],
            },
            'lineItems': [
                {
                    'type': 'product',
                    'name': 'Abonnement mensuel basic',
                    'recurrence': '1 Month',
                    'duration': '3 Month',
                    'tangible': 'N',
                    'price': '150.00',
                    'description': '',
                    'productId': property.id,
                }
            ]
        }

        try:
            result = twocheckout.Charge.authorize(params)
            backup_response = json.dumps(result)
            print(result.responseCode)
            if result.responseCode == 'APPROVED':
                offer = Offer.objects.get(type='MONTHLY_BASIC')
                user = request.user
                payment_data = dict()
                payment_data['response_code'] = result.responseCode
                payment_data['transaction_id'] = result.transactionId
                payment_data['order_number'] = result.orderNumber
                payment_data['amount'] = result['lineItems'][0]['price']
                payment_data['property_title_bkp'] = property.title
                payment_data['backup_response'] = backup_response
                Payment.objects.create(
                    offer=offer,
                    property=property,
                    user=user,
                    initiated_on=timezone.now(),
                    **payment_data
                )
                return render(request, 'subscriptions/payment_success.html', payment_data)
        except TwocheckoutError as error:
            return HttpResponse(error.msg)
Redjam
  • 474
  • 3
  • 12