简体   繁体   中英

Paypal Encrypted Website Payments with ASP.NET

I am trying to generate encrypted URLs to redirect the customer to PayPal for payment. I have uploaded my own certificate to PayPal, downloaded PayPal's public certificate, and use the following code to do the job:

  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

Yet, when I go to generated URL it says cannot decode certificate ID! I tried with both live and sandbox and get the same result. What is the problem with this code? How do I get a working EWP with ASP.NET? Please do not suggest alternative methods such as a hosted Express Checkout, or hosted buttons.

Thank you.

Looks like this code is correct after all. It worked with PayPal, but did not with SandBox, probably cause I have not uploaded the certificate to SandBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM