如何将用户重定向到返回的URL python

时间:2015-06-13 06:27:56

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

所以我测试了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,那真的会帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

我有点自己解决了。将此添加到代码

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>'

知道脚本将页面重定向为魅力。谢谢