PayPal NVP API Example
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web
Public Partial Class ExpressCheckoutNVP
Inherits System.Web.UI.Page
Protected Sub checkoutButton_Click(ByVal sender As Object, ByVal e As EventArgs)
01 Dim requestString As StringBuilder = InitializeRequest("SetExpressCheckout")
02 requestString.Append("&AMT=" + Decimal.Parse(amountTextBox.Text).ToString("f"))
03 Dim basePath As String = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, String.Empty) + Request.ApplicationPath
04 requestString.Append("&RETURNURL=" + basePath + "/Sample/ExpressCheckoutNVP.aspx")
05 requestString.Append("&CANCELURL=" + basePath + "/Default.aspx")
06 Dim token As String
07 Dim dummy As String
08 If Post(requestString.ToString(), token, dummy) Then
Session("OrderTotal") = amountTextBox.Text
09 Response.Redirect(Globals.PayPalUrl + "webscr?cmd=_express-checkout&token=" + token)
Else
messageLabel.Text = "SetExpressCheckout Failed"
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Request.QueryString.[Get]("token") IsNot Nothing Then
10 Dim token As String
11 Dim payerID As String
12 Dim requestString As StringBuilder = InitializeRequest("GetExpressCheckoutDetails")
13 requestString.Append("&TOKEN=" + HttpUtility.UrlEncode(Request.QueryString.[Get]("token")))
14 If Post(requestString.ToString(), token, payerID) Then
15 requestString = InitializeRequest("DoExpressCheckoutPayment")
16 requestString.Append("&TOKEN=" + HttpUtility.UrlEncode(token))
17 requestString.Append("&AMT=" + HttpUtility.UrlEncode(Decimal.Parse(Session("OrderTotal").ToString()).ToString("f")))
18 requestString.Append("&PAYMENTACTION=Sale")
19 requestString.Append("&PAYERID=" + HttpUtility.UrlEncode(payerID))
20 If Post(requestString.ToString(), token, payerID) Then
messageLabel.Text = "DoExpressCheckoutPayment Successful"
Else
messageLabel.Text = "DoExpressCheckoutPayment Failed"
End If
Else
messageLabel.Text = "GetExpressCheckoutDetails Failed"
End If
End If
End Sub
21 Private Shared Function InitializeRequest(ByVal method As String) As StringBuilder
22 Dim requestString As New StringBuilder()
23 requestString.Append("METHOD=" + HttpUtility.UrlEncode(method))
24 requestString.Append("&USER=" + HttpUtility.UrlEncode(Globals.UserName))
25 requestString.Append("&PWD=" + HttpUtility.UrlEncode(Globals.Password))
26 requestString.Append("&SIGNATURE=" + HttpUtility.UrlEncode(Globals.Signature))
27 requestString.Append("&VERSION=" + HttpUtility.UrlEncode(Globals.Version))
28 Return requestString
End Function
29 Public Function Post(ByVal request As String, ByRef token As String, ByRef payerID As String) As Boolean
30 token = String.Empty
31 payerID = String.Empty
32 Dim webResponse As HttpWebResponse
Try
33 Dim webRequest As HttpWebRequest = TryCast(WebRequest.Create(Globals.ApiUrl), HttpWebRequest)
34 webRequest.Method = "POST"
35 webRequest.ContentType = "application/x-www-form-urlencoded"
36 webRequest.ContentLength = request.Length
37 Dim writer As New StreamWriter(webRequest.GetRequestStream())
38 writer.Write(request)
39 writer.Close()
40 webResponse = TryCast(webRequest.GetResponse(), HttpWebResponse)
41 If Not webRequest.HaveResponse Then
42 Throw New Exception()
End If
If webResponse.StatusCode <> HttpStatusCode.OK AndAlso webResponse.StatusCode <> HttpStatusCode.Accepted Then
Throw New Exception()
End If
Catch ex As Exception
Dim test As String = ex.Message
43 Return False
End Try
44 Dim reader As New StreamReader(webResponse.GetResponseStream())
45 Dim responseString As String = reader.ReadToEnd()
46 reader.Close()
47 Dim success As Boolean = False
48 Dim ampersand As Char() = {"&"c}
49 Dim pairs As String() = responseString.Split(ampersand)
50 Dim equalsign As Char() = {"="c}
51 For i As Integer = 0 To pairs.Length - 1
52 Dim pair As String() = pairs(i).Split(equalsign)
53 If pair(0).ToLower() = "ack" AndAlso HttpUtility.UrlDecode(pair(1)).ToLower() <> "failure" Then
54 success = True
End If
55 If pair(0).ToLower() = "token" Then
56 token = HttpUtility.UrlDecode(pair(1))
End If
57 If pair(0).ToLower() = "payerid" Then
58 payerID = HttpUtility.UrlDecode(pair(1))
End If
Next
59 Return success
End Function
End Class
Encore Class Library Example
Imports System
Imports Encore.PayPal.Nvp
Partial Public Class ExpressCheckoutClass
Inherits System.Web.UI.Page
Protected Sub checkoutButton_Click(ByVal sender As Object, ByVal e As EventArgs)
01 Dim ppSet As New NvpSetExpressCheckout()
02 ppSet.Add(NvpSetExpressCheckout.Request._AMT, Decimal.Parse(amountTextBox.Text).ToString("f"))
03 Dim basePath As String = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, String.Empty) + Request.ApplicationPath
04 ppSet.Add(NvpSetExpressCheckout.Request._RETURNURL, basePath + "/Sample/ExpressCheckoutClass.aspx")
05 ppSet.Add(NvpSetExpressCheckout.Request._CANCELURL, basePath + "/Default.aspx")
06 If ppSet.Post() Then
Session("OrderTotal") = amountTextBox.Text
07 Response.Redirect(ppSet.RedirectUrl)
Else
messageLabel.Text = "SetExpressCheckout Failed"
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Request.QueryString.[Get]("token") IsNot Nothing Then
08 Dim ppGet As New NvpGetExpressCheckoutDetails()
09 ppGet.Add(NvpGetExpressCheckoutDetails.Request.TOKEN, Request.QueryString.[Get]("token"))
10 If ppGet.Post() Then
11 Dim ppPay As New NvpDoExpressCheckoutPayment()
12 ppPay.Add(NvpDoExpressCheckoutPayment.Request._TOKEN, ppGet.[Get](NvpGetExpressCheckoutDetails.Response.TOKEN))
13 ppPay.Add(NvpDoExpressCheckoutPayment.Request._PAYERID, ppGet.[Get](NvpGetExpressCheckoutDetails.Response.PAYERID))
14 ppPay.Add(NvpDoExpressCheckoutPayment.Request._AMT, Decimal.Parse(Session("OrderTotal").ToString()).ToString("f"))
15 ppPay.Add(NvpDoExpressCheckoutPayment.Request._PAYMENTACTION, NvpPaymentActionCodeType.Sale)
16 If ppPay.Post() Then
messageLabel.Text = "DoExpressCheckoutPayment Successful"
Else
messageLabel.Text = "DoExpressCheckoutPayment Failed"
End If
Else
messageLabel.Text = "GetExpressCheckoutDetails Failed"
End If
End If
End Sub
End Class