0

我正在尝试生成加密 URL 以将客户重定向到 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!我尝试使用实时和沙盒并获得相同的结果。这段代码有什么问题?如何使用 ASP.NET 获得有效的 EWP?请不要建议替代方法,例如托管快速结帐或托管按钮。

谢谢你。

4

1 回答 1

0

看起来这段代码毕竟是正确的。它适用于 PayPal,但不适用于 SandBox,可能是因为我没有将证书上传到 SandBox。

于 2013-06-08T07:51:15.300 回答