【问题标题】:how to deal with returnurl in django-paypal for paypal WPP如何在 django-paypal 中为 paypal WPP 处理 returnurl
【发布时间】:2010-06-27 19:12:59
【问题描述】:

我现在正在使用 django 开发我的网站。在将我的网站与贝宝集成时,我使用可插入应用程序“http://github.com/johnboxall/django-paypal”。虽然文档对“Using PayPal Payments Pro (WPP)”说得很清楚,但我还是有一些疑问,尤其是“returnurl”和“confirm_template”的关系。

#views.py
from paypal.pro.views import PayPalPro

def buy_my_item(request):
   item = {"amt": "10.00",             # amount to charge for item
           "inv": "inventory",         # unique tracking variable paypal
           "custom": "tracking",       # custom tracking variable for you
           "cancelurl": "http://...",  # Express checkout cancel url
           "returnurl": "http://..."}  # Express checkout return url

   kw = {"item": item,                            # what you're selling
         "payment_template": "payment.html",      # template name for payment
         "confirm_template": "confirmation.html", # template name for confirmation
         "success_url": "/success/"}              # redirect location after success

   ppp = PayPalPro(**kw)
   return ppp(request)

当点击贝宝网站上的“继续”按钮时,它会将我重定向回“returnurl”。在这里,是我的问题,我不知道如何处理这个returnurl。在我看来,我还应该写一个函数来让它渲染confirmation.html。我对吗?如果是这样,如何编写这个函数。 非常感谢任何帮助和指示。

【问题讨论】:

    标签: django paypal django-paypal


    【解决方案1】:

    该文档不适用于 django-paypal。简短的回答是您的returnurl 应该是指向您的方法buy_my_item 的任何URL。以下是我在 IRL 工作的一些例子。请注意,我使用 PayPal 的“useraction=commit”选项来减少他们快速结帐的步骤数。

    在你的 urls.py 中:

    url(r'^pay-now/', views.pay_now, name='pay_now'),
    url(r'^purchase-thanks/$', views.purchase_thanks, name='pay_success'),
    url(r'^purchase-cancelled/$', views.purchase_thanks, name='pay_cancel'),
    

    在你的views.py中:

    """ User payment method endpoint for rendering and processing forms. """
    @csrf_exempt
    def pay_now( request ):
        # Override django-paypal library endpoints to include 'useraction=commit'
        # which changed PayPal's review page to be a 'pay now' page.
        # This is ugly but I didn't want to subclass.
        from paypal.pro import views as pro_views
        pro_views.EXPRESS_ENDPOINT = "https://www.paypal.com/webscr?cmd=_express-checkout&useraction=commit&%s"
        pro_views.SANDBOX_EXPRESS_ENDPOINT = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&%s"
    
        # ...because we use 'useraction=commit', there's no need to show the confirm page.
        # So let's change the request to show the confirmation form into a request to
        # approve it. It just so happens that the arguments are the same -- the difference
        # is between the GET and the POST.
        # <input type="hidden" name="token" value="EC-485941126E653491T" id="id_token"/>
        # <input type="hidden" name="PayerID" value="78W69D3FEVWJBC" id="id_PayerID"/>
        if request.method == 'GET' and 'token' in request.GET and 'PayerID' in request.GET:
            request.method = 'POST'
            request.POST = request.GET # Crudely convert GET to POST
    
        item = {
            'amt':           99.99, # Amount to charge for item
            'currencycode':  'usd',
            #'inv':           1, # Unique tracking variable paypal - must be a number.
            #'desc':          'Your product name', # Deprecated by PayPal, don't bother
                                                   # (you'll get the name twice in your statement otherwise)
            'custom':        'custom1', # Custom tracking variable for you. Realistically you have to pass
                                        # this if you're specifying basket contents to PayPal as django-paypal
                                        # won't be given `item_name` in the IPN, only `item_name1` etc.
                                        # which it cannot interpret.
            'cancelurl':     'http://%s%s' % DYNAMIC_URL, reverse('pay_cancel')), # Express checkout cancel url
            'returnurl':     'http://%s%s' % (DYNAMIC_URL, reverse('pay_now')), # Express checkout return url
            'allownote':     0, # Disable "special instructions for seller"
            'l_name0':       'Your product name',
            #'l_number0':    1234,
            #'l_desc0':      'longer description',
            'l_amt0':        99.99,
            'l_qty0':        1,
            'itemamt':       99.99,
            #'taxamt':       0.00,
            #'shippingamt':  0.00,
            #'handlingamt':  0.00,
            #'shipdiscamt':  0.00,
            #'insuranceamt': 0.00,
        }
    
        kw = {
            'item': item,
            'payment_template': 'cms/register.html', # Template name for payment
            'confirm_template': 'cms/paypal-confirmation.html', # Template name for confirmation
            'success_url':      reverse('pay_success'), # Ultimate return URL
        }
    
        ppp = PayPalPro(**kw)
        return ppp(request)
    

    您可能还有很多其他问题,例如“当我进入付款确认页面时,我如何区分 EC 付款和 WPP 付款?”,但我会一直保存直到有人问! django-paypal 还不错,但让它工作起来可能会非常令人沮丧,特别是当你想向你的模板传递额外的值时,并且文档(即使在我见过的 fork 上)也不是很好。

    请注意,此示例指的是 HTTP 而不是 HTTPS URL。在生产中,使用 WPP,您几乎肯定会想要使用 HTTPS。此外,该产品的主要发行版已过时,您需要使用 @csrf_exempt 修补 IPN 端点以使其正常工作。

    【讨论】:

    • 我去问问。如何区分 EC 和 WPP 付款。另外,我需要在这里的任何地方设置ipn端点吗?
    • 哦,当用户在我的网站上确认后,我可以确定已收到付款吗?
    • 如果 request.POST.has_key['email'] 则为 WPP,否则为 EC。您需要找到另一种方法来通过 EC 付款传递用户的电子邮件地址,这可能会很痛苦。是的,您可以确保通过这种方式完成了付款。但是,我倾向于等待 IPN 回电。不,IPN 地址是在您的 PayPal 配置中设置的。这真的很烦人,这意味着您不能动态更改 IPN 端点。如果您使用 PayPal 的网络表单来处理付款,您可以,但谁愿意使用它们?
    • 感谢您的帮助,我发现贝宝很痛苦,但您的帖子很有启发性!
    【解决方案2】:

    大家好,我遇到了同样的问题,根据http://uswaretech.com/blog/2008/11/using-paypal-with-django/,我们需要编写一个视图来处理来自贝宝的响应,所以我使用了 confirm.html 模板,但它没有呈现任何东西......

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 2011-04-17
      • 2021-02-03
      • 2016-03-04
      • 2018-11-07
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多