用户不能互相支付 django-paypal

Users can't pay each other django-paypal

我有一家在线商店,用户可以在其中互相付款购买商品,我一直在使用沙盒帐户对其进行测试,但我认为它不起作用。我真的不知道问题出在哪里

这是我的 views.py:

def payment_process(request, trade_id):
    trade = get_object_or_404(Trade, id=trade_id)
    host = request.get_host()

    paypal_dict = {
        'business': trade.seller.email, 
        'amount': Decimal(trade.price),
        'item_name': trade.filename,
        'invoice': str(trade.id),
        'currency_code': 'USD',
        'notify_url': 'https://{}{}'.format(host,
                                           reverse('paypal-ipn')),
        'return_url': 'https://{}{}/{}'.format(host,
                                           *reverse('payment_done', kwargs={'trade_id': trade.id})),
        'cancel_return': 'https://{}{}'.format(host,
                                              reverse('home')),
    }

    form = PayPalPaymentsForm(initial=paypal_dict)
    return render(request, 'payment/payment_process.html', {'trade': trade, 'form': form})


@csrf_exempt
def payment_done(request, trade_id):
    # Do some very important stuff after paying ...
    # It would be really nice if someone can help me with a checker
    messages.success(request, 'Your product is in your inbox now')
    return redirect('trade:inbox')

我的urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    ...
    # Prodbox Payment
    path('payment/process/<int:trade_id>/', payment_views.payment_process, name="payment_process"),
    path('payment/done/<int:trade_id>/', payment_views.payment_done, name="payment_done"),
    # Prodbox packages
    path('paypal/', include('paypal.standard.ipn.urls')),
]

处理支付的模板:

{% extends 'users/base.html' %}

{% block title %}Payment Process | Prodbox {% endblock title %}

{% block content %}
<div class="container row justify-content-center">
    <div class="shadow-lg p-3 mb-5 col-md-8 bg-white rounded m-4 p-4">
        <section>
            <p>Seller: {{ trade.seller.email }}</p>
            <p>Product: {{ trade.thing }}</p>
            <p style="color: #2ecc71;">Price: ${{ trade.price }}</p>
        </section>
        <section>
            <h4>Pay with PayPal</h4>
            {{ form.render }}
        </section>
    </div>
</div>
{% endblock content %}

支付完成后非常非常重要将用户重定向到payment_done视图,(如果我有一个检查器检查支付是否完成就太好了在 运行 完成功能之前)

此外,请注意我强调用户使用他们的 PayPal 电子邮件帐户

为什么它不起作用?!

额外信息(可能没有帮助)

用户模型:

from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager


class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """

    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)


class User(AbstractUser):
    username = None
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField(('email address'), unique=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    objects = CustomUserManager()

    def username(self):
        return f'{self.first_name} {self.last_name}'
    
    def __str__(self):
        return self.email

settings.py:

AUTH_USER_MODEL = 'users.User'
PAYPAL_RECEIVER_EMAIL = 'zeyadshapan2004@gmail.com'

PAYPAL_TEST = False

local_settings.py:

DEBUG = True
ALLOWED_HOSTS = []
PAYPAL_RECEIVER_EMAIL = 'sb-dabm93302277@personal.example.com'
PAYPAL_TEST = True

您说它不起作用,但没有提供有关问题行为及其不起作用原因的信息。

但我认为这无关紧要,因为您使用的是糟糕的集成(django-paypal,基于支付标准)您想要的行为是“非常非常重要”付款人 return.


您应该切换到集成 v2/checkout/orders,有或没有 Checkout-Python-SDK。您的服务器上需要两条路由,一条用于 'Set Up Transaction',一条用于 'Capture Transaction',记录在此处:https://developer.paypal.com/docs/checkout/reference/server-integration/

用于审批的最佳 front-end UI 在这里:https://developer.paypal.com/demo/checkout/#/pattern/server。在调用您的 2 个 django 后端路由(通过获取)的独立 HTML 文件中使其正常工作,然后将其作为 front-end 集成到您的 django 模板和结帐流程中。

对于一个用户向另一个用户付款的功能,使用 payee 对象,在此处记录:https://developer.paypal.com/docs/checkout/integration-features/pay-another-account/