我正在尝试生成加密的网址,将客户重定向到PayPal进行付款。我已将自己的证书上传到PayPal,下载了PayPal的公共证书,并使用以下代码完成工作:
Dim _vals As String = "cert_id=ID" & vbLf & "cmd=_cart" & vbLf & _
"business=businessemail" & vbLf & _
"upload=1" & vbLf & _
"no_shipping=1" & vbLf & _
"custom=poidccffgd" & vbLf & _
"invoice=oid12345" & vbLf & _
"currency_code=EUR" & vbLf & _
"item_name_1=" & _oname & vbLf & _
"amount_1=" & _curps & vbLf & _
"item_number_1=10" & vbLf & _
"item_name_2=Free Bonus" & vbLf & _
"amount_2=0" & vbLf & _
"discount_rate_cart=30"
Dim _url As String = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&encrypted=" & Server.UrlEncode(EncryptX509(_vals))
Friend Shared Function EncryptX509(aData As String) As String
Dim _signerCert As New X509Certificates.X509Certificate2(MyPrivateCertificateByteArray, "password", X509Certificates.X509KeyStorageFlags.DefaultKeySet)
Dim _recipientCert As New X509Certificates.X509Certificate2(PayPalPublicCertificateByteArray)
'get data
'--------
Dim messageBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(aData)
'sign
'----
Dim content = New System.Security.Cryptography.Pkcs.ContentInfo(messageBytes)
Dim signed = New System.Security.Cryptography.Pkcs.SignedCms(content)
Dim signer = New System.Security.Cryptography.Pkcs.CmsSigner(_signerCert)
signed.ComputeSignature(signer)
Dim signedBytes = signed.Encode()
'encrypt
'-------
Dim content2 = New System.Security.Cryptography.Pkcs.ContentInfo(signedBytes)
Dim envMsg = New System.Security.Cryptography.Pkcs.EnvelopedCms(content2)
Dim recipient = New Pkcs.CmsRecipient(Pkcs.SubjectIdentifierType.IssuerAndSerialNumber, _recipientCert)
envMsg.Encrypt(recipient)
Dim encryptedBytes As Byte() = envMsg.Encode()
'format
'------
Const PKCS7_HEADER As String = "-----BEGIN PKCS7-----"
Const PKCS7_FOOTER As String = "-----END PKCS7-----"
Dim base64 = Convert.ToBase64String(encryptedBytes)
Dim formatted = New StringBuilder()
formatted.Append(PKCS7_HEADER)
formatted.Append(base64)
formatted.Append(PKCS7_FOOTER)
Return formatted.ToString
End Function
然而,当我去生成URL时,它说无法解码证书ID!我尝试使用live和sandbox并获得相同的结果。这段代码有什么问题?如何使用ASP.NET获得可用的EWP?请不要建议其他方法,例如托管的Express Checkout或托管按钮。
谢谢。
答案 0 :(得分:0)
毕竟看起来这段代码是正确的。它与PayPal一起使用,但没有使用SandBox,可能是因为我没有将证书上传到SandBox。