问题描述
我有一个在线商店,用户可以互相付款购买商品,我已经使用沙盒帐户对其进行了测试,但是我认为它不起作用。我真的不知道问题出在哪里
这是我的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,**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,**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),因此您希望它对付款人的回报“非常非常重要”
您应该切换到的集成是集成v2 / checkout / orders,带有或不带有Checkout-Python-SDK。您的服务器上将需要两条路由,一条用于“设置交易”,一条用于“捕获交易”,在此处记录:https://developer.paypal.com/docs/checkout/reference/server-integration/
用于批准的最佳前端UI在这里:https://developer.paypal.com/demo/checkout/#/pattern/server。在将其作为前端集成到django模板和结帐流程中之前,可以在一个独立的HTML文件中很好地运行此文件,该文件调用您的2条django后端路由(通过获取)。
要获得用户向另一个用户付费的功能,请使用payee
对象,在此处记录:https://developer.paypal.com/docs/checkout/integration-features/pay-another-account/