我正在以表格形式集成SagePay投递,这需要通过SagePay API为每个交易请求一个唯一的会话密钥。我已经成功获取了获取密钥的代码(在后面使用代码),如下所示:-
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myData As String = "{""vendorName"": ""myvendorname""}"
Dim mySessionKey As String
myReq = HttpWebRequest.Create("https://pi-live.sagepay.com/api/v1/merchant-session-keys")
myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("apikey:apipassword")))
myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
'Response.Write(myText)
mySessionKey = "{ merchantSessionKey: '" & Mid(myText, 65, 36) & "' }"
End Sub
这将导致产生以下字符串(mySessionKey),其格式为完成交易所需的格式(每次GUID都不相同):-
{vendorSessionKey:'F42164DA-4A10-4060-AD04-F6101821EFC3'}
然后,需要在javascript块中将以上内容注入.aspx页,如下所示:-
<body>
<div id="main">
<h1>My Test Shop</h1>
<form>
<h2>Payment Details</h2>
<div id="sp-container"></div>
<div id="submit-container">
<input type="submit"/>
</div>
</form>
</div>
<script>
sagepayCheckout({ merchantSessionKey: 'F42164DA-4A10-4060-AD04-F6101821EFC3' }).form();
</script>
</body>
我不知道如何将其动态注入到aspx页面中。
任何积分,深表感谢。
答案 0 :(得分:0)
我认为应该这样做:
将其置于您的视图状态
ViewState(“ merchantSessionKey”)= ....
然后输入代码:
<script>
sagepayCheckout(<%= ViewState("merchantSessionKey") %>).form();
</script>
或者如果您在页面级别将其设为公共变量(现在该变量在方法本地):
<script>
sagepayCheckout(<%= mySessionKey %>).form();
</script>