无法找到如何正确实施django-paypal

时间:2019-03-26 15:21:47

标签: python django django-forms paypal-ipn django-paypal

我知道django paypal系统应该如何工作。我尝试按照此处的说明进行操作:https://django-paypal.readthedocs.io/en/stable/standard/ipn.html

我的问题是我大部分都不了解。这就是我所拥有的:

views.py

from django.core.urlresolvers import reverse
from django.shortcuts import render
from paypal.standard.forms import PayPalPaymentsForm
from .models import Subs
from .forms import SubForm
from django.shortcuts import redirect
from forum.views import home
from django.urls import reverse


def BuyShit(request):
    user = request.user
    if request.method == "POST":
        formbuy = SubForm(request.POST)
        if formbuy.is_valid() and 'buy' in request.POST:
            sub = formbuy.save(commit=False)
            sub.owner = user
            sub.save()
            order_created.delay(sub.id)
            request.session['order_id'] = sub.id
            return redirect(home)

forms.py

from django import forms
from .models import Subs

class SubForm(forms.ModelForm):
    class Meta:
        model = Subs

        fields = ('item','price')

models.py

from django.db import models
from forum.models import Post
from django.contrib.auth.models import User
# Create your models here.

class Subs(models.Model):
    item = models.Charfield(max_length=55, verbose_name='item')
    price = models.Charfield(max_length=55, verbose_name='item')
    owner = models.Charfield(User, max_length=55, verbose_name='item')

我还为Paypal添加了相应的设置和网址:

 url(r'^paypal/', include('paypal.standard.ipn.urls')),

在指向django-paypal的链接上,它们在views.py中具有此功能:

from django.core.urlresolvers import reverse
from django.shortcuts import render
from paypal.standard.forms import PayPalPaymentsForm

def view_that_asks_for_money(request):

    # What you want the button to do.
    paypal_dict = {
        "business": "receiver_email@example.com",
        "amount": "10000000.00",
        "item_name": "name of the item",
        "invoice": "unique-invoice-id",
        "notify_url": request.build_absolute_uri(reverse('paypal-ipn')),
        "return": request.build_absolute_uri(reverse('your-return-view')),
        "cancel_return": request.build_absolute_uri(reverse('your-cancel-view')),
        "custom": "premium_plan",  # Custom command to correlate to some function later (optional)
    }

    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form}
    return render(request, "payment.html", context)

我使用哪一部分?我应该在哪里添加多少钱或标题?

然后有一个hooks.py东西,我真的不明白它是如何工作的:

from paypal.standard.models import ST_PP_COMPLETED
from paypal.standard.ipn.signals import valid_ipn_received

def show_me_the_money(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
        # WARNING !
        # Check that the receiver email is the same we previously
        # set on the `business` field. (The user could tamper with
        # that fields on the payment form before it goes to PayPal)
        if ipn_obj.receiver_email != "receiver_email@example.com":
            # Not a valid payment
            return

        # ALSO: for the same reason, you need to check the amount
        # received, `custom` etc. are all what you expect or what
        # is allowed.

        # Undertake some action depending upon `ipn_obj`.
        if ipn_obj.custom == "premium_plan":
            price = ...
        else:
            price = ...

        if ipn_obj.mc_gross == price and ipn_obj.mc_currency == 'USD':
            ...
    else:
        #...

valid_ipn_received.connect(show_me_the_money)

所有组件似乎都断开了。我对django很熟悉,但这似乎完全不同。 我完全迷路了。而且在线教程太糟糕了。请帮忙!!!我只需要添加一个按钮即可购买单个产品,并成功触发一个函数即可修改相应的模型。听起来很简单...谢谢您的帮助!!!!!

0 个答案:

没有答案