Hallo,
ich habe mal angefangen etwas auf die Beine zu stellen. Die Kommunikation mit der Seite scheint zu funktionieren. Allerdings erhalte ich den Fehler 10002 (You do not have permission to make this API call), was laut Beschreibung heißt, dass entweder mein Username, mein Passwort oder meine API-Signature falsch ist. Diese Werte habe ich aber mehrmals überprüft. Ich teste im Sandbox-Modus im IE und bin auch mit meinem Developer-Account in die Paypal-Developer Seite eingeloggt. Die API-Zugangsdaten (Name, Passwort und Signature) sind natürlich die des Händler-Accounts in meinem Developer-Account.
Hier erst mal meine Klasse:
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Net
Public Class clsPaypalAPI
Public APIUsername As String
Public APIPasswort As String
Public APIUnterschrift As String
Public SandboxModus As Boolean
Public LogFile As String
Public lastError As String = ""
Public Sub New(ByVal Username As String, ByVal Passwort As String, ByVal Unterschrift As String, _
ByVal Sandbox As Boolean, Optional ByVal LogFilePath As String = "")
APIUsername = Username
APIPasswort = Passwort
APIUnterschrift = Unterschrift
SandboxModus = Sandbox
LogFile = LogFilePath
End Sub
Private Sub addLog(ByVal Eintrag As String)
Try
Dim FileStream As New FileStream(LogFile, FileMode.Append)
Dim StreamWriter As New StreamWriter(FileStream)
StreamWriter.WriteLine(Format$(Now, "dd.MM.yyyy HH:mm ss") & ": " & Eintrag)
StreamWriter.Close()
FileStream.Close()
Catch ex As Exception
'Nichts tun bei Fehler...
End Try
End Sub
Private Function APIURL(ByVal Methode As String) As String
Dim tmpS As String
If SandboxModus Then
tmpS = "<A href="https://api-3t.sandbox.paypal.com/nvp">https://api-3t.sandbox.paypal.com/nvp</A>"
Else
tmpS = "<A href="https://api-3t.paypal.com/nvp">https://api-3t.paypal.com/nvp</A>"
End If
addURLParameter(tmpS, "USER", APIUsername)
addURLParameter(tmpS, "PWD", APIPasswort)
addURLParameter(tmpS, "SIGNATURE", APIUnterschrift)
addURLParameter(tmpS, "VERSION", "67.0")
addURLParameter(tmpS, "METHOD", Methode)
Return tmpS
End Function
Private Sub addURLParameter(ByRef URL As String, ByVal ParameterName As String, ByVal ParameterWert As String)
If InStr(URL, "?") = 0 Then
URL = URL & "?"
Else
URL = URL & "&"
End If
URL = URL & ParameterName & "=" & HttpContext.Current.Server.UrlEncode(ParameterWert)
End Sub
Private Function APIcall(ByVal URL As String, ByRef Response As NameValueCollection) As Boolean
Response.Clear()
Try
Dim splitURL() As String = Split(URL, "?")
Dim Enc As UTF8Encoding = New System.Text.UTF8Encoding
Dim PostData() As Byte = Enc.GetBytes(splitURL(1))
Dim Request As HttpWebRequest = HttpWebRequest.Create(splitURL(0))
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Request.ContentLength = PostData.Length
Request.GetRequestStream.Write(PostData, 0, PostData.Length)
Dim ResponseReader As New StreamReader(Request.GetResponse.GetResponseStream)
Dim Responses() As String = Split(ResponseReader.ReadToEnd, "&")
ResponseReader.Close()
Dim i1 As Long
For i1 = 0 To UBound(Responses)
Dim PairData() As String = Split(Responses(i1), "=")
If UBound(PairData) = 1 Then
If Trim$(PairData(0)) <> "" Then Response.Add(PairData(0), HttpContext.Current.Server.UrlDecode(PairData(1)))
End If
Next
Return True
Catch ex As Exception
lastError = "Fehler beim aufrufen der Paypal-API: " & ex.Message
Return False
End Try
End Function
Private Function ResponseOk(ByVal Response As NameValueCollection, Optional ByRef Timestamp As Date = #1/1/2000#, _
Optional ByVal ErrorCode As Long = 0, Optional ByRef CorrelationID As String = "", _
Optional ByVal APIVersion As String = "", Optional ByRef APIBuild As String = "") As Boolean
Dim tmpS As String = Response.Item("TIMESTAMP").ToString
If IsDate(tmpS) Then Timestamp = CDate(tmpS)
ErrorCode = 0
CorrelationID = Response.Item("CORRELATIONID").ToString()
APIVersion = Response.Item("VERSION").ToString()
APIBuild = Response.Item("BUILD").ToString()
Select Case Response.Item("ACK").ToString.ToUpper
Case "SUCCESS", "SUCCESS WITH WARNING"
Return True
Case Else '"FAILURE", "FAILURE WITH WARNING"
ErrorCode = Val(Response.Item("L_ERRORCODE0").ToString())
lastError = Response.Item("L_LONGMESSAGE0").ToString()
If lastError = "" Then lastError = Response.Item("L_SHORTMESSAGE0").ToString()
lastError = "Fehler bei Paypal-Anfrage (#" & Trim$(Str$(ErrorCode)) & "): " & lastError
End Select
End Function
Public Function AddressVerify(ByVal EMail As String, ByVal Straße As String, ByVal PLZ As String, _
ByRef EMailOk As Boolean, Optional ByRef AdresseOk As Boolean = False, _
Optional ByRef StraßeOk As Boolean = False, Optional ByRef PLZOk As Boolean = False, _
Optional ByRef Ländercode As String = "", Optional ByRef TOKEN As String = "") As Boolean
lastError = ""
EMailOk = False
AdresseOk = False
StraßeOk = False
PLZOk = False
Ländercode = ""
TOKEN = ""
Dim URL As String = APIURL("AddressVerify")
addURLParameter(URL, "EMAIL", EMail)
addURLParameter(URL, "STREET", Straße)
addURLParameter(URL, "ZIP", PLZ)
Dim Response As New NameValueCollection
If APIcall(URL, Response) Then
If ResponseOk(Response) Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
End Class
Und so teste ich das ganze:
Dim PayPal As New clsPaypalAPI("....", "....", "....", True)
Dim EMailOk As Boolean
Dim AdresseOk As Boolean
Dim StraßeOk As Boolean
Dim PLZOk As Boolean
Dim Ländercode As String = ""
Dim TOKEN As String = ""
If PayPal.AddressVerify("<A href="mailto:bla@blub.de">bla@blub.de</A>", "Teststr. 1", "12345", EMailOk, AdresseOk, StraßeOk, PLZOk, Ländercode, TOKEN) Then
'...
Else
MsgBox(PayPal.lastError)
End If
Habe ich etwas übersehen?
Viele Grüße
Andreas