Cajuu'
Cajuu'

Reputation: 1166

How can one use Webhooks from Paypal in its django application

I'm using django-paypal and paypalrestsdk to integrate Paypal payment & subscriptions to my website.

I've looked over django-paypal and other modules but I wasn't able to fully understand the process of handling a webhook.

I'm getting a 405 Error in my console when I'm completing a payment.

I have successfully created a paypal sandbox account for testing purposes ( two user accounts were automatically created on it for tests ).

In my settings.py:

PAYPAL_RECEIVER_EMAIL = "the email"
PAYPAL_IDENTITY_TOKEN = "_BB3dqp-crOrUo2uh84g0zN2alX0LwWPAT85r0g-2Eo0"

In my index.html:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="business" value="my_email" id="id_business" />
    <input type="hidden" name="amount" value="1" id="id_amount" />
    <input type="hidden" name="item_name" value="Subscription Package" id="id_item_name" />
    <input type="hidden" name="notify_url" value="website/page" id="id_notify_url" />
    <input type="hidden" name="cancel_return" value="website/page" id="id_cancel_return" />
    <input type="hidden" name="return" value="website/page" id="id_return_url" />
    <input type="hidden" name="invoice" value="UID" id="id_invoice" />
    <input type="hidden" name="cmd" value="_xclick" id="id_cmd" />
    <input type="hidden" name="charset" value="utf-8" id="id_charset" />
    <input type="hidden" name="currency_code" value="USD" id="id_currency_code" />
    <input type="hidden" name="no_shipping" value="1" id="id_no_shipping" />
    <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="Buy it Now" />
</form>

And my views.py:

class PayPalWebhook(View):
    @staticmethod
    def post(request):
        event_json = json.loads(request.body)

        print '=========='
        print event_json.type
        print '=========='
        print event_json

        return HttpResponse(status=200)

class PaypalAPI(View):
    @staticmethod
    def post(request):
        pass

I would just like to see the webhooks in my console, that's all.

Upvotes: 0

Views: 1559

Answers (2)

Hvitis
Hvitis

Reputation: 555

You need to add

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def PayPalReturnView(request):
    # Do something here

to the view that PayPal posts too. I think I got your problem right but anyway try adding more info: your urls for example which are crucial here.

Upvotes: 1

VJ-PP
VJ-PP

Reputation: 151

Hope this helps: 405 is usually when the API/Endpoint you are calling is used with an incorrect method. For example using a POST for a HTTP Call which only supports GET or not using the correct endpoint.

Python SDKs are a good point to start with as well https://github.com/paypal/PayPal-Python-SDK

Also, its a good practice to not share any tokens in forums/QnA sites. :)

Upvotes: 3

Related Questions