Created
August 19, 2015 06:47
-
-
Save DiKorsch/2cd15f38d0f666cf3a73 to your computer and use it in GitHub Desktop.
PayPal payment with OTP verification form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from paypal.standard.forms import PayPalPaymentsForm as PPPF | |
from django import forms | |
class OTPForm(forms.Form): | |
code = forms.CharField(label = "TOTP Code") | |
def clean_code(self): | |
code = self.cleaned_data['code'] | |
if not check_otp(code): # use libraries like onetimepass | |
raise forms.ValidationError("Please enter a valid TOTP Code!") | |
return code | |
def get_payin_form(self, request): | |
invoice = generate_some() | |
form = PaypalPayInForm(invoice) | |
pp_transfer = PayPalAllowedTransfer( | |
user = request.user, | |
invoice = form.invoice, | |
) | |
pp_transfer.save() | |
return form | |
class PaypalPayInForm(PPPF): | |
amount = forms.IntegerField() | |
def __init__(self, invoice, *args, **kw): | |
self.invoice = invoice | |
pp_settings = self._default_initial() | |
pp_settings.update(kwargs.get("initial", {})) | |
kwargs["initial"] = pp_settings | |
def _default_initial(self): | |
return { | |
"business": settings.PAYPAL_RECEIVER_EMAIL, | |
"amount": "1", | |
"currency_code": "EUR", | |
"item_name": "your item name", | |
"invoice": self.invoice, | |
"notify_url": settings.BASE_URL + reverse("payin:paypal_notify"), | |
"return_url": settings.BASE_URL + reverse("payin:paypal_return"), | |
"cancel_return": settings.BASE_URL + reverse("payin:paypal_cancel"), | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from django.db import models | |
class PayPalAllowedTransfer(models.Model): | |
user = models.ForeignKey(User, editable = False) | |
invoice = models.CharField("Invoice", max_length = 64, editable = False) | |
created_at = models.DateTimeField("Creation Date", auto_now_add = True) | |
used = models.BooleanField("Already used", default = False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
django | |
django-paypal | |
onetimepass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from paypal.standard.ipn.signals import payment_was_successful | |
from paypal.standard.models import ST_PP_COMPLETED | |
# this handles the PayPal IPNs, sender is an ipn object | |
def success(sender, **kwargs): | |
invoice = sender.invoice | |
if sender.payment_status != ST_PP_COMPLETED: return | |
if PayPalAllowedTransfer.objects.filter(invoice = sender.invoice, used = False).exists(): | |
handle_payment_as_sucessfull(sender) | |
else: | |
flag_this_payment_as_not_allowed(sender) | |
payment_was_successful.connect(success) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from forms import OTPForm | |
from my_decorators import render_to # http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/ | |
from django.views.decorators.http import require_POST | |
@render_to("paypal_template.html") | |
@require_POST | |
def paypal(request): | |
form = PaypalPayInVerificationForm(request.POST) | |
return {"form" : form.get_pain_form(request) if form.is_valid else form} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment