spookylukey / django-paypal

A pluggable Django application for integrating PayPal Payments Standard or Payments Pro
MIT License
729 stars 208 forks source link

subscription button multiple items #164

Closed Quicky85 closed 7 years ago

Quicky85 commented 7 years ago

Hi,

I'm Using Django-Paypal 0.3.3 and got IPN working. But I have a question and can't find the answer anywhere else.

I'm trying to create a subscription button with a choice of multiple items from a dropdown in the button, i.e. basic, pro etc with different prices. I created a similar button in the Paypal Sandbox but couldn't replicate this using the "paypal_dict". Is this supported in Django-Paypal?

Now I tried to create a workaround creating separate buttons for the different plans but the render funtion "{{form.render}}" in the html template seems to only render a single button.

Hope you guys can advice or share a solution.

Thank you all Q.

spookylukey commented 7 years ago

You'll need to post details of what you created with the PayPal Sandbox - I don't know about its features.

Quicky85 commented 7 years ago

subscription button

Here is the HTML Code used for this button:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="xxxxxx">
<table>
<tr><td><input type="hidden" name="on0" value="Subscription category">Subscription category</td></tr><tr><td><select name="os0">
    <option value="Basic">Basic : €147,00 EUR - monthly</option>
    <option value="Pro">Pro : €247,00 EUR - monthly</option>
    <option value="Unlimited">Unlimited : €467,00 EUR - monthly</option>
</select> </td></tr>
</table>
<input type="hidden" name="currency_code" value="EUR">
<input type="image" src="https://www.sandbox.paypal.com/nl_NL/NL/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal, de veilige en complete manier van online betalen.">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

And in views.py using the paypal_dic I try to replicate it using these variables to somehow create a dropdown paypal_dict = { "cmd": "_xclick-subscriptions", "business": "myemail", "option_index": "1", "on0": "Subscription category", "os0": "Basic", "os1": "something", "option_select0": "option1", "option_amount0": "147", "option_select1": "option2", "option_amount1": "246", "currency_code": "EUR", "p3": "1", "t3": "M", "src": "1", "sra": "1", "no_note": "1", "custom": "0", "item_name": "Basic subscription", "notify_url": "http://efc9155567.ngrok.io/accounts/register/account_activated/", "return_url": "http://efc9155567.ngrok.io/", "cancel_return": "http//www.example.com/cancel-location/",

spookylukey commented 7 years ago

Yes, we don't support this, and there wouldn't be an easy way to build in support for all the possibilities.

There are a few approaches you could take:

1) Just copy and paste the generated HTML

2) If this is not sufficient e.g. you need the notify_url and other parameters, then merge together the two forms - the one generated by django-paypal and the one from the PayPal Sandbox site.

For both these cases, you won't be using django-paypal to finally generate the form, you will just have a block of HTML in your template. This has the disadvantage that things like the form action URL won't adjust automatically for test mode.

3) Do not attempt to pass into paypal_dict the fields that need to be a drop down box. Instead of using {{ form.render }}, use {{ form.as_p }}. This will render out only the hidden fields. To this, you will then need to manually add the <form> tag, and the other bits you need - the <select> drop down for example.

4) Subclass PayPalPaymentsForm and add a field for the dropdown, using the normal Django forms API - https://docs.djangoproject.com/en/1.10/topics/forms/ . In this way you can add a visible field to the form for the <select> (all the predefined fields on this form are hidden). This is the neatest solution, but perhaps requires you to understand more. I've used it successfully in the past - e.g. see https://bitbucket.org/spookylukey/cciw-website/src/7c777391cac2320b798997a652372edad3b5fe6a/cciw/bookings/views.py?at=default&fileviewer=file-view-default#views.py-803 where I have a form that allows people to put in the amount that they want to pay in some cases. In your cases you would be using a choices rather than letting people enter what they wanted to, and probably for a different field. But the principle is the same.

Quicky85 commented 7 years ago

Thanks for the information provided.

If any others are interested, I've chosen to use the html button as suggested above instead of using the "paypal_dict". In the sandbox I generated the HTML button and added a "custom" hidden field which takes variables. The notify url for the IPN can also be set in the Sandbox, just like the return and cancel URL.