PayPal REST API是PayPal提供的现代支付接口,允许开发者通过HTTP请求与PayPal系统交互,处理支付、退款、订阅等交易。Classic ASP(Active Server Pages)是微软早期的服务器端脚本技术,虽然较老但仍在使用。
由于Classic ASP是较老的技术,而PayPal REST API是现代API,主要挑战包括:
首先需要确保服务器支持HTTPS请求和JSON处理。
<%
' 设置PayPal API端点
Const PAYPAL_API_SANDBOX = "https://api.sandbox.paypal.com"
Const PAYPAL_API_LIVE = "https://api.paypal.com"
' 选择环境
Dim paypalApiUrl
paypalApiUrl = PAYPAL_API_SANDBOX ' 测试环境
' paypalApiUrl = PAYPAL_API_LIVE ' 生产环境
' 你的PayPal客户端ID和密钥
Const CLIENT_ID = "你的客户端ID"
Const CLIENT_SECRET = "你的客户端密钥"
%>
PayPal REST API使用OAuth 2.0认证,需要先获取访问令牌。
<%
Function GetPayPalAccessToken()
Dim http, url, postData, responseText, json
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
url = paypalApiUrl & "/v1/oauth2/token"
' 准备请求数据
postData = "grant_type=client_credentials"
' 设置请求头
http.Open "POST", url, False
http.setRequestHeader "Authorization", "Basic " & Base64Encode(CLIENT_ID & ":" & CLIENT_SECRET)
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Accept", "application/json"
' 发送请求
http.Send postData
' 处理响应
responseText = http.responseText
' 解析JSON响应 - 需要JSON解析库或自定义函数
Set json = ParseJson(responseText)
GetPayPalAccessToken = json("access_token")
Set http = Nothing
End Function
' Base64编码函数
Function Base64Encode(text)
Dim xml, node
Set xml = Server.CreateObject("MSXML2.DOMDocument")
Set node = xml.createElement("b64")
node.DataType = "bin.base64"
node.nodeTypedValue = Stream_StringToBinary(text)
Base64Encode = node.text
Set node = Nothing
Set xml = Nothing
End Function
Function Stream_StringToBinary(text)
Dim stream
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = 2 ' adTypeText
stream.Charset = "utf-8"
stream.Open
stream.WriteText text
stream.Position = 0
stream.Type = 1 ' adTypeBinary
stream.Position = 0
Stream_StringToBinary = stream.Read
Set stream = Nothing
End Function
%>
获取访问令牌后,可以创建支付请求。
<%
Function CreatePayPalPayment(amount, currency, description, returnUrl, cancelUrl)
Dim http, url, postData, responseText, json, accessToken
' 获取访问令牌
accessToken = GetPayPalAccessToken()
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
url = paypalApiUrl & "/v1/payments/payment"
' 准备JSON请求数据
postData = "{"
postData = postData & """intent"":""sale"","
postData = postData & """payer"":{""payment_method"":""paypal""},"
postData = postData & """transactions"":[{"
postData = postData & """amount"":{"
postData = postData & """total"":""" & amount & ""","
postData = postData & """currency"":""" & currency & """"
postData = postData & "},"
postData = postData & """description"":""" & description & """"
postData = postData & "}],"
postData = postData & """redirect_urls"":{"
postData = postData & """return_url"":""" & returnUrl & ""","
postData = postData & """cancel_url"":""" & cancelUrl & """"
postData = postData & "}"
postData = postData & "}"
' 设置请求头
http.Open "POST", url, False
http.setRequestHeader "Authorization", "Bearer " & accessToken
http.setRequestHeader "Content-Type", "application/json"
http.setRequestHeader "Accept", "application/json"
' 发送请求
http.Send postData
' 处理响应
responseText = http.responseText
' 解析JSON响应
Set json = ParseJson(responseText)
' 返回支付链接
For Each link In json("links")
If link("rel") = "approval_url" Then
CreatePayPalPayment = link("href")
Exit For
End If
Next
Set http = Nothing
End Function
%>
用户批准后执行支付:
<%
Function ExecutePayPalPayment(paymentId, payerId)
Dim http, url, postData, responseText, json, accessToken
' 获取访问令牌
accessToken = GetPayPalAccessToken()
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
url = paypalApiUrl & "/v1/payments/payment/" & paymentId & "/execute"
' 准备JSON请求数据
postData = "{""payer_id"":""" & payerId & """}"
' 设置请求头
http.Open "POST", url, False
http.setRequestHeader "Authorization", "Bearer " & accessToken
http.setRequestHeader "Content-Type", "application/json"
http.setRequestHeader "Accept", "application/json"
' 发送请求
http.Send postData
' 处理响应
responseText = http.responseText
' 解析JSON响应
Set json = ParseJson(responseText)
' 返回支付状态
ExecutePayPalPayment = json("state")
Set http = Nothing
End Function
%>
Classic ASP没有内置JSON解析器,需要添加JSON解析功能:
<%
' 简单的JSON解析函数
Function ParseJson(jsonText)
Dim dict, matches, match, key, value, i
Set dict = Server.CreateObject("Scripting.Dictionary")
' 使用正则表达式提取键值对
Dim regex
Set regex = New RegExp
regex.Pattern = """([^""]+)""\s*:\s*(""[^""]*""|\d+|true|false|null)"
regex.Global = True
Set matches = regex.Execute(jsonText)
For Each match In matches
key = Mid(match.SubMatches(0), 1)
value = match.SubMatches(1)
' 去除字符串值的引号
If Left(value, 1) = """" Then
value = Mid(value, 2, Len(value) - 2)
End If
dict.Add key, value
Next
Set ParseJson = dict
Set regex = Nothing
End Function
%>
<%
' 创建支付示例
Dim paymentUrl, amount, currency, description, returnUrl, cancelUrl
amount = "10.00"
currency = "USD"
description = "测试支付"
returnUrl = "http://yourdomain.com/success.asp"
cancelUrl = "http://yourdomain.com/cancel.asp"
paymentUrl = CreatePayPalPayment(amount, currency, description, returnUrl, cancelUrl)
If paymentUrl <> "" Then
Response.Redirect paymentUrl
Else
Response.Write "创建支付失败"
End If
%>
如果遇到太多技术限制,可以考虑: