Javascript 在php中运行python paypal支付网关
我有一个python脚本,我想用php调用它 我一直在尝试使用exec和ajax来实现,但是我没有运气 我正在使用XAMMP,它有一个内部服务器&dreamweaver来编码 Python(.py)Javascript 在php中运行python paypal支付网关,javascript,php,python,Javascript,Php,Python,我有一个python脚本,我想用php调用它 我一直在尝试使用exec和ajax来实现,但是我没有运气 我正在使用XAMMP,它有一个内部服务器&dreamweaver来编码 Python(.py) 从uvm.settings\u读取器导入get\u nodeid\u设置 从mod_python导入apache 从mod_python导入util 从mod_python导入会话 从uvm导入uvm 导入cgi 将simplejson导入为json 从日期时间导入日期时间 导入URL库 导入url
从uvm.settings\u读取器导入get\u nodeid\u设置
从mod_python导入apache
从mod_python导入util
从mod_python导入会话
从uvm导入uvm
导入cgi
将simplejson导入为json
从日期时间导入日期时间
导入URL库
导入urllib2
随机输入
导入时间
#PayPal沙盒URL。此URL的IP地址以及www.paypalobjects.com对应的IP地址必须添加到捕获门户、已传递的主机和已传递的列出的服务器地址中
PAYPAL_URL=”https://www.sandbox.paypal.com"
#按钮应由接收付款的用户创建。必须在按钮中设置此页面的URL以接收通知
#对于cancelCheckot通知:http://[Untangle box IP]/capture/custom\un]/custom.py/cancelCheckout
#对于finish checkount通知:http://[Untangle box IP]/capture/custom\un]/custom.py/finishCheckout
按钮ID=“VMSV6QK9LA3HS”
#接收付款的用户必须将“自动返回到网站付款”设置为“开”
#并设置类似于完成签出通知的返回URL:http://[Untangle box IP]/capture/custom\un]/custom.py/finishCheckout
#付款数据传输必须设置为On,并且必须在下面使用生成的身份令牌
PDT_IDENTITY_TOKEN='m_F6TLH32GWYLQ65NPQKDVKNDCDHX6PJFGBYCZU5F0YNCFJQRN4ISNLG'
#一般说明:当此自定义页面处于活动状态时,请注意本地目录上WebUI中的管理操作可能会覆盖
#如果这些用户是在管理员在WebUI中执行操作时创建的,则此页面将创建新用户
def buyNowButton(url,buttonid):
retVal=''%url
retVal+=''
retVal+=''%buttonid
retVal+=''%url
retVal+=''%url
retVal+=''
返回返回
def loginForm(appid、主机、uri、用户=None、密码=None):
page=“”
第+=“”页
如果不是用户:
页面+=“用户名”
其他:
页面+=“用户名”%user
如果不是密码:
页面+=“密码”
其他:
页面+=“密码”%Password
第+=“”页
第+=“”页
第+=“”页
页面+=“登录
”
第+=“”页
返回页
#在自定义捕获门户zip文件中包含custom.py文件时
#您可以完全控制捕获和身份验证过程。
#当捕获门户截获流量并且需要捕获页面时
#显示时,我们的主处理程序将调用您的索引函数并
#向您传递一组可用于构建自定义页面的参数:
#
#req-这是apache请求对象
#rawpath-这是存储自定义文件的完整路径
#它将包括后面的反斜杠
#例如:/usr/share/untangle/web/capture/custom_6/
#网页-这是图像和其他内容的相对路径
#可以指定链接内容,并包括尾随反斜杠
#例如:/capture/custom_6/
#appid-为捕获门户的每个实例分配一个唯一的appid
#必须作为参数包含到自定义\u处理程序中的值
#因此mod_python脚本知道使用哪个实例
#例:6
#主机-这是原始HTTP请求产生的主机
#在捕获页面显示中
#例如:www.yahoo.com
#uri-这是从原始HTTP请求生成的路径
#在捕获页面显示中
#例如:/some/page/或/something/content.html
#
#在下面的示例中,我们创建了一个简单的页面,其中包含酒店
#客人可以输入姓名和房间号。请注意,POST处理程序
#设置为父捕获门户处理程序,并传递appid,以便
#知道捕获门户的哪个实例正在调用。我们的例子没有
#需要原始路径,但我们确实使用网页来包含一个漂亮的图像
#和页面图标。我们将原始主机和uri隐藏在表单中,以便
#身份验证后将用户重定向到其原始目标\
def索引(req、rawpath、Web页、appid、主机、uri):
会话=会话。会话(req)
如果session.is_new():
会话['rawpath']=rawpath
会话['webpath']=webpath
会话['appid']=appid
会话['host']=主机
会话['uri']=uri
session.save()
page=“”
第+=“”页
第+=“”页
页面+=“登录或购买1天通行证”
page+=loginForm(appid、主机、uri)
页面+=“
”
页面+=buyNowButton(贝宝URL,按钮ID)
第+=“”页
第+=“”页
req.content\u type=“text/html”
请求写入(第页)
def取消签出(req):
会话=会话。会话(req)
如果不是session.is_new():
索引(请求、会话['rawpath']、会话['webpath']、会话['appid']、会话['host']、会话['uri'])
其他:
page=“取消签出”
req.content\u type=“text/html”
请求写入(第页)
def generatePassword(长度):
返回“”。连接(映射(lambda x:random.choice('ABCDEFGHIJKLMNOPRSTUVXYZ0123456789'),范围(长度)))
def finishCheckout(要求):
page=“签出已完成”
params=util.FieldStorage(req)
tx=params.get(“tx”,“”)
如果len(tx)>0:
url=“%s/cgi-bin/webscr”%PAYPAL\u url
data=urllib.urlencode({'tx':tx,'at':PDT\u IDENTITY\u TOKEN,'cmd':'u notify-synch'})
结果=urllib2.urlopen(url,数据)
html=results.read()
页面+='
';
如果html.find('SUCCESS')!=-1:
values=html.split('\n')
对于值中的值:
if value.find('payer_email')!=-1:
零件=值。拆分('='))
电子邮件=零件[透镜(零件)-1]
email=urllib.unquote(email.decode('utf8'))
context=Uvm().getUvmContext()
如果不是上下文:
引发异常(“无法获取uvm上下文”)
地方灾难
from uvm.settings_reader import get_nodeid_settings
from mod_python import apache
from mod_python import util
from mod_python import Session
from uvm import Uvm
import cgi
import simplejson as json
from datetime import datetime
import urllib
import urllib2
import random
import time
# PayPal sandbox URL.This URL's IP address along with www.paypalobjects.com corresponding IP address must be added into Captive Portal, Passed Hosts, Pass Listed Server Addresses
PAYPAL_URL="https://www.sandbox.paypal.com"
#the button should be created by the user which receives the payment The URL of this page must be set in the button to receive notifications
#For the cancelCheckot notification: http://[Untangle box IP]/capture/custom_[n]/custom.py/cancelCheckout
#For the finish checkount notification: http://[Untangle box IP]/capture/custom_[n]/custom.py/finishCheckout
BUTTON_ID="VMSV6QK9LA3HS"
# The user which receives the payments must set Auto Return to Website Payments to On
# and set the return URL similar to the finish checkout notification: http://[Untangle box IP]/capture/custom_[n]/custom.py/finishCheckout
# Payment Data Transfer must be set to On and the generated identity token must be used below
PDT_IDENTITY_TOKEN='m_f6twlH32gWmylq65npQkDvKNDCdhX6PjfGBycZu5f0ynCfjQrN4iSLNLG'
# General Note: While this custom page is active be aware that admin actions in the WebUI on Local Directory can overwrite
# new users created by this page if those users are created while the admin is performing actions in the WebUI
def buyNowButton( url, buttonid):
retVal ='<form action="%s/cgi-bin/webscr" method="post" target="_top">' % url
retVal +='<input type="hidden" name="cmd" value="_s-xclick">'
retVal +='<input type="hidden" name="hosted_button_id" value="%s">' % buttonid
retVal +='<input type="image" src="%s/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">' % url
retVal +='<img alt="" border="0" src="%s/en_US/i/scr/pixel.gif" width="1" height="1">' % url
retVal +='</form>'
return retVal
def loginForm(appid, host, uri, user=None, password=None):
page = "<FORM AUTOCOMPLETE='OFF' METHOD='POST' ACTION='/capture/handler.py/custom_handler?appid=" + appid + "'>"
page += "<TABLE BORDER=0 CELLPADDING=8>"
if not user:
page += "<TR><TD>Username</TD><TD><INPUT WIDTH=80 TYPE='text' NAME='username' ID='username'></INPUT></TD></TR>"
else:
page += "<TR><TD>Username</TD><TD><INPUT WIDTH=80 TYPE='text' NAME='username' ID='username' value='%s'></INPUT></TD></TR>" % user
if not password:
page += "<TR><TD>Password</TD><TD><INPUT WIDTH=80 TYPE='password' NAME='pwd' ID='pwd'></INPUT></TD></TR>"
else:
page += "<TR><TD>Password</TD><TD><INPUT WIDTH=80 TYPE='password' NAME='pwd' ID='pwd' value='%s'></INPUT></TD></TR>" % password
page += "</TABLE>"
page += "<INPUT TYPE='hidden' NAME='host' ID='host' VALUE='" + host + "'></INPUT>"
page += "<INPUT TYPE='hidden' NAME='uri' ID='uri' VALUE='" + uri + "'></INPUT>"
page += "<P><BUTTON TYPE='submit' NAME='submit' ID='submit' TITLE='Login' value='Login'>Login</BUTTON></P>"
page += "</FORM>"
return page
# When you include a custom.py file in your custom captive portal zip file
# you get full control over the capture and authentication process.
# When the captive portal intercepts traffic and a capture page needs to
# be displayed, our main handler will call your index function and will
# pass you a bunch of parameters you can use to build your custom page:
#
# req - this is the apache request object
# rawpath - this is the full path to where the custom files are stored
# and it will include the trailing backslash
# e.g.: /usr/share/untangle/web/capture/custom_6/
# webpath - this is the relative path from which images and other
# linked content can be specified and includes trailing backslash
# e.g.: /capture/custom_6/
# appid - each instance of captive portal is assigned a unique appid
# value that you MUST include as a parameter to the custom_handler
# so the mod_python scripts know which instance to work with
# e.g.: 6
# host - this is the host from the original HTTP request that resulted
# in the captive page display
# e.g.: www.yahoo.com
# uri - this is the path from the original HTTP request that resulted
# in the captive page display
# e.g.: /some/page/or/something/content.html
#
# In our example below we create a simple page with a form where the hotel
# guest can enter their name and room number. Note that the POST handler
# is set to the parent captive portal handler, and we pass the appid so it
# knows which instance of captive portal is calling. Our example doesn't
# need the rawpath but we do use the webpath to include a spiffy image
# and page icon. We hide the original host and uri in the form so we can
# redirect the user to their original destination after authentication.\
def index(req,rawpath,webpath,appid,host,uri):
session = Session.Session(req)
if session.is_new():
session['rawpath']=rawpath
session['webpath']=webpath
session['appid']=appid
session['host']=host
session['uri']=uri
session.save()
page = "<HTML><HEAD>"
page += "<META http-equiv='Content-Type' content='text/html; charset=UTF-8' />"
page += "<body>"
page += "<h2>Login or buy 1 Day pass<h2>"
page += loginForm(appid,host,uri)
page += "<br/><br/>"
page += buyNowButton(PAYPAL_URL, BUTTON_ID)
page += "<body>"
page += "</html>"
req.content_type = "text/html"
req.write(page)
def cancelCheckout(req):
session = Session.Session(req)
if not session.is_new():
index(req,session['rawpath'],session['webpath'],session['appid'],session['host'],session['uri'])
else:
page = "<html><body><H1>Cancel checkout </H1></body></html>"
req.content_type = "text/html"
req.write(page)
def generatePassword(length):
return ''.join(map(lambda x:random.choice('ABCDEFGHIJKLMNOPRSTUVXYZ0123456789'), range(length)))
def finishCheckout(req):
page = "<html><body><H1>Checkout finished</H1></body></html>"
params = util.FieldStorage(req)
tx=params.get("tx","")
if len(tx) > 0:
url = "%s/cgi-bin/webscr" % PAYPAL_URL
data = urllib.urlencode({'tx': tx,'at':PDT_IDENTITY_TOKEN,'cmd':'_notify-synch'})
results = urllib2.urlopen(url, data)
html = results.read()
page +='<br/>';
if html.find('SUCCESS') != -1:
values = html.split('\n')
for value in values:
if value.find('payer_email') != -1:
parts = value.split('=')
email = parts[len(parts)-1]
email = urllib.unquote(email).decode('utf8')
context = Uvm().getUvmContext()
if not context:
raise Exception("The uvm context could not be obtained")
localDirectory = context.localDirectory()
if not localDirectory:
raise Exception("The uvm node manager could not locate the local directory instance")
pwd = generatePassword(6)
userToRegister={"email": "%s"%email, "firstName":"", "lastName":"", "password":"%s"%pwd, "username":"%s"%email, "expirationTime":int((time.time() + 86400)*1000), "javaClass":"com.untangle.uvm.LocalDirectoryUser"}
if not localDirectory.userExists(userToRegister):
localDirectory.addUser(userToRegister)
page += "<b>Login credentials</b> <br/><br/>"
page += "Username:<b>%s</b><br/>" % email
page += "Password:<b>%s</b><br/><br/>" % pwd
page += "Save the credentials and press the login button below to access the Internet<br/><br/>"
else:
updatedUser={"email": "%s"%email, "username":"%s"%email, "expirationTime":int((time.time() + 86400)*1000), "javaClass":"com.untangle.uvm.LocalDirectoryUser"}
localDirectory.updateUser(updatedUser)
session = Session.Session(req)
try:
page += loginForm(session['appid'],session['host'],session['uri'], email, pwd)
except KeyError:
pass
else:
page += html
req.content_type = "text/html"
req.write(page)
# When our parent post handler gets the form defined above, it will call
# your handler function passing many of the same arguments described above.
def handler(req,rawpath,webpath,appid, loginMap={}):
# get the network address of the client
address = req.get_remote_host(apache.REMOTE_NOLOOKUP,None)
# grab the form fields from the post data
uname=req.form['username'].value
pwd = req.form['pwd'].value
host = req.form['host'].value
uri = req.form['uri'].value
# first we get the uvm context
context = Uvm().getUvmContext()
if not context:
raise Exception("The uvm context could not be obtained")
# now we use the uvm context to get the captive portal node instance
# note that we pass the appid so we get a reference to the correct instance
capture = context.nodeManager().node(long(appid))
if not capture:
raise Exception("The uvm node manager could not locate the capture node instance")
# we also want the node settings so we can check for a custom redirect target
settings = get_nodeid_settings(long(appid))
if (settings == None):
raise Exception("Unable to load capture node settings")
captureAuth = capture.userAuthenticate(address, uname, pwd)
# if a redirect URL is configured we send the user to that location
if (len(settings['redirectUrl']) != 0) and (settings['redirectUrl'].isspace() == False):
target = str(settings['redirectUrl'])
# no redirect URL is configured so send the user to the originally requested page
else:
# if the host or uri are empty we just return a simple success page
if ((host == 'Empty') or (uri == 'Empty')):
page = "<HTML><HEAD><TITLE>Login Success</TITLE></HEAD><BODY><H1>Login Success</H1></BODY></HTML>"
return(page)
else:
target = str("http://" + host + uri)
util.redirect(req, target)