【问题标题】:How to redirect user to a returned URL python如何将用户重定向到返回的 URL python
【发布时间】:2015-08-29 05:07:05
【问题描述】:

所以我测试了 paypal rest sdk 并使用 python 作为我的 cgi 语言(我也知道它是 2007 年),无论如何我遇到了障碍,因为我无法让它重定向到返回的 URL 页面。

我已经尝试过打印位置方法。它对我不起作用。

# Create Payment Using PayPal Sample
# This sample code demonstrates how you can process a
# PayPal Account based Payment.
# API used: /v1/payments/payment
from paypalrestsdk import Payment
import logging

logging.basicConfig(level=logging.INFO)

# Payment
# A Payment Resource; create one using
# the above types and intent as 'sale'
payment = Payment({
"intent": "sale",

# Payer
# A resource representing a Payer that funds a payment
# Payment Method as 'paypal'
"payer": {
    "payment_method": "paypal"},

# Redirect URLs
"redirect_urls": {
    "return_url": "http://localhost:3000/payment/execute",
    "cancel_url": "http://localhost:3000/"},

# Transaction
# A transaction defines the contract of a
# payment - what is the payment for and who
# is fulfilling it.
"transactions": [{

    # ItemList
    "item_list": {
        "items": [{
            "name": "item",
            "sku": "item",
            "price": "5.00",
            "currency": "USD",
            "quantity": 1}]},

    # Amount
    # Let's you specify a payment amount.
    "amount": {
        "total": "5.00",
        "currency": "USD"},
    "description": "This is the payment transaction description."}]})

 # Create Payment and return status
 if payment.create():
 print("Payment[%s] created successfully" % (payment.id))
  # Redirect the user to given approval url
for link in payment.links:
    if link.method == "REDIRECT":
        # Convert to str to avoid google appengine unicode issue
        # https://github.com/paypal/rest-api-sdk-python/pull/58
        redirect_url = str(link.href)
        print("Redirect for approval: %s" % (redirect_url))
else:
    print("Error while creating payment:")
    print(payment.error)

所以,如果你们是新手,在用户浏览器上抛出 301 或 302 真的会帮到我。谢谢。

【问题讨论】:

    标签: python redirect python-3.x paypal paypal-rest-sdk


    【解决方案1】:

    我自己解决了这个问题。通过将其添加到代码中

    print 'Content-Type: text/html'
    print 'Location: %s' % redirectURL
    print # HTTP says you have to have a blank line between headers and content
    print '<html>'
    print '  <head>'
    print '    <meta http-equiv="refresh" content="0;url=%s" />' %redirectURL
    print '    <title>You are going to be redirected</title>'
    print '  </head>' 
    print '  <body>'
    print '    Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
    print '  </body>'
    print '</html>'
    

    知道脚本会像魅力一样重定向页面。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多