Link to home
Start Free TrialLog in
Avatar of jonpwatson5
jonpwatson5

asked on

C# / VB.NET Paypal Response

Hi.

I've found some code on the internet that is used for updating my database after customers haev paid via paypal on my site.

The variables all send to paypal ok, and seem to send back i think, but i cant get this code working i have found.

I wanted a C# one, but could only get a VB one that looked good, but is generating some errors.

Warning      1      variable 'response' conflicts with property 'response' in the base class 'Page' and should be declared 'Shadows'.      OnlineAccounts\Paid.aspx.vb      16      50

Error      5      Type 'IBuySpy.TransactionDB' is not defined.      OnlineAccounts\Paid.aspx.vb      212      50

Imports System.Net
Imports System.IO
Imports System.Web.Mail


Partial Class OnlineAccounts_Paid
    Inherits System.Web.UI.Page

    Private CmdString As String, objHttp As String, OrderID As String, Txn_id As String, Payment_status As String, Receiver_email As String, _
        Item_name As String, Item_number As String, Quantity As String, Invoice As String, [Custom] As String, Payment_gross As String, _
        Payer_email As String, Pending_reason As String, Payment_date As String, Payment_fee As String, Txn_type As String, First_name As String, _
        Last_name As String, Address_street As String, Address_city As String, Address_state As String, Address_zip As String, Address_country As String, _
        Address_status As String, Payer_status As String, Payer_id As String, Payment_type As String, Notify_version As String, Verify_sign As String, _
        Subscr_date As String, Period1 As String, Period2 As String, Period3 As String, Amount1 As String, Amount2 As String, _
        Amount3 As String, Recurring As String, Reattempt As String, Retry_at As String, Recur_times As String, Username As String, _
        Password As String, Subscr_id As String, response As String



    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim stringPost As String = Request.Form.ToString()
        ' assign posted variables to local variables 
        Txn_id = Request.Form("txn_id")
        Receiver_email = Request.Form("receiver_email")
        Item_name = Request.Form("item_name")
        Item_number = Request.Form("item_number")
        Quantity = Request.Form("quantity")
        Invoice = Request.Form("invoice")
        [Custom] = Request.Form("custom")
        Payment_status = Request.Form("payment_status")
        Pending_reason = Request.Form("pending_reason")
        If Payment_status <> "Pending" Then
            Pending_reason = " "
        End If
        Payment_date = Request.Form("payment_date")
        Payment_fee = Request.Form("payment_fee")
        Payment_gross = Request.Form("payment_gross")
        Txn_type = Request.Form("txn_type")
        First_name = Request.Form("first_name")
        Last_name = Request.Form("last_name")
        Address_street = Request.Form("address_street")
        Address_city = Request.Form("address_city")
        Address_state = Request.Form("address_state")
        Address_zip = Request.Form("address_zip")
        Address_country = Request.Form("address_country")
        Address_status = Request.Form("address_status")
        Payer_email = Request.Form("payer_email")
        Payer_status = Request.Form("payer_status")
        Payer_id = Request.Form("payer_id")
        Payment_type = Request.Form("payment_type")
        Notify_version = Request.Form("notify_version")
        Verify_sign = Request.Form("verify_sign")

        ' post to paypal and await response : use: "https://www.paypal.com/cgi-bin/webscr" for real; 
        ' use: "http://www.eliteweaver.co.uk/testing/ipntest.php" to test; 

        Dim httpWebRequest As HttpWebRequest = DirectCast(WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), HttpWebRequest)
        httpWebRequest.Method = "POST"
        ' length plus 21 because &cmd=_notify-validate is 21 chars long 
        httpWebRequest.ContentLength = stringPost.Length + 21
        httpWebRequest.ContentType = "application/x-www-form-urlencoded"
        Dim streamWriter As StreamWriter = Nothing
        streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        stringPost = stringPost + "&cmd=_notify-validate"
        streamWriter.Write(stringPost)
        streamWriter.Close()
        Dim httpWebResponse As HttpWebResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
        Using streamReader As New StreamReader(httpWebResponse.GetResponseStream())
            response = streamReader.ReadToEnd()
            streamReader.Close()
        End Using

        ' Step 1c: Process the response from PayPal. 
        If httpWebResponse.StatusCode <> HttpStatusCode.OK Then
            ' an error has occurred 
            MailUsTheOrder("Status Error: " + httpWebResponse.StatusCode)
        Else
            Select Case response
                Case "VERIFIED"
                    ' check for new version of paypal if different send notify email 
                    If Notify_version <> "1.4" Then
                        Dim mailObj As New MailMessage()
                        mailObj.From = "CJC BodyCare"
                        mailObj.[To] = Receiver_email
                        mailObj.Subject = "Paypal Version Change"
                        mailObj.Body = "I see a new version of PayPal Notify IPN Service!!! Go check the PayPal site for updates!" + Chr(10) + "I currently see version: " + Notify_version
                        mailObj.BodyFormat = MailFormat.Html
                        SmtpMail.Send(mailObj)
                    End If
                    '**************************************************** 
                    ' still to do 
                    '****************************************************** 
                    ' check that Txn_id has not been previously processed 
                    ' check that Receiver_email is an email address in your PayPal account 
                    ' process payment 
                    '****************************************************** 


                    ' check that Payment_status=Completed 
                    Select Case Payment_status
                        Case "Completed"
                            'The payment has been completed and the funds are successfully in your account balance 
                            '************************** 
                            ' Perform steps 2-5 above. 
                            ' Continue with automation processing if all steps succeeded. 
                            '************************** 


                            If Receiver_email = "pay@yourdomain.com" Then
                                Select Case Txn_type
                                    Case "web_accept", "cart"
                                        'The payment was sent by your customer via the Web Accept feature. 
                                        'This payment was sent by your customer via the Shopping Cart feature 
                                        MailUsTheOrder("PROCESS ME: The order was completed successfully.")
                                        Exit Select
                                    Case "send_money"
                                        'This payment was sent by your customer from the PayPal website, using the "Send Money" tab 
                                        MailUsTheOrder("PROCESS ME: Somebody sent us money!")
                                        Exit Select
                                    Case "subscr_signup"
                                        'This IPN is for a subscription sign-up 
                                        MailUsTheOrder("PROCESS ME: Subscription signup.")
                                        Exit Select
                                    Case "subscr_cancel"
                                        'This IPN is for a subscription cancellation 
                                        MailUsTheOrder("PROCESS ME: Subscription cancellation.")
                                        Exit Select
                                    Case "subscr_failed"
                                        'This IPN is for a subscription payment failure 
                                        MailUsTheOrder("FAILURE: Subscription failed.")
                                        Exit Select
                                    Case "subscr_payment"
                                        'This IPN is for a subscription payment 
                                        MailUsTheOrder("COOL: We got cash!")
                                        Exit Select
                                    Case "subscr_eot"
                                        'This IPN is for a subscription's end of term 
                                        MailUsTheOrder("WHAT IS THIS? Subscription end of term.")
                                        Exit Select
                                End Select
                                Select Case Address_status
                                    Case "confirmed"
                                        'Customer provided a Confirmed Address 
                                        Exit Select
                                    Case "unconfirmed"
                                        'Customer provided an Unconfirmed Address 
                                        Exit Select
                                End Select
                                Select Case Payer_status
                                    Case "verified"
                                        'Customer has a Verified U.S. PayPal account 
                                        Exit Select
                                    Case "unverified"
                                        'Customer has an Unverified U.S. PayPal account 
                                        Exit Select
                                    Case "intl_verified"
                                        'Customer has a Verified International PayPal account 
                                        Exit Select
                                    Case "intl_unverified"
                                        'Customer has an Unverified International PayPal account 
                                        Exit Select
                                End Select
                                Select Case Payment_type
                                    Case "echeck"
                                        'This payment was funded with an eCheck 
                                        Exit Select
                                    Case "instant"
                                        'This payment was funded with PayPal balance, credit card, or Instant Transfer 
                                        Exit Select
                                End Select
                            Else
                                MailUsTheOrder("WEIRD: Someone is notifying us that the payments were received by someone else???")
                            End If
                            Exit Select
                        Case "Pending"
                            'The payment is pending - see the "pending reason" variable below for more information. Note: You will receive another instant payment notification when the payment becomes "completed", "failed", or "denied" 
                            Select Case Pending_reason
                                Case "echeck"
                                    ' The payment is pending because it was made by an eCheck, which has not yet cleared 
                                    Exit Select
                                Case "intl"
                                    'The payment is pending because you, the merchant, hold an international account and do not have a withdrawal mechanism. You must manually accept or deny this payment from your Account Overview 
                                    Exit Select
                                Case "verify"
                                    'The payment is pending because you, the merchant, are not yet verified. You must verify your account before you can accept this payment 
                                    Exit Select
                                Case "address"
                                    'The payment is pending because your customer did not include a confirmed shipping address and you, the merchant, have your Payment Receiving Preferences set such that you want to manually accept or deny each of these payments. To change your preference, go to the "Preferences" section of your "Profile" 
                                    Exit Select
                                Case "upgrade"
                                    'The payment is pending because it was made via credit card and you, the merchant, must upgrade your account to Business or Premier status in order to receive the funds 
                                    Exit Select
                                Case "unilateral"
                                    'The payment is pending because it was made to an email address that is not yet registered or confirmed 
                                    Exit Select
                                Case "other"
                                    'The payment is pending for an "other" reason. For more information, contact customer service 
                                    Exit Select
                            End Select
                            MailUsTheOrder("PENDING: Order is waiting to be processed.")
                            Exit Select
                        Case "Failed"
                            'The payment has failed. This will only happen if the payment was made from your customer's bank account 
                            MailUsTheOrder("FAILED: This only happens if the payment was made from our customer's bank account.")
                            Exit Select
                        Case "Denied"
                            'You, the merchant, denied the payment. This will only happen if the payment was previously pending due to one of the "pending reasons" 
                            MailUsTheOrder("DENIED: We denied this payment.")
                            Exit Select
                    End Select
                    ' add transaction to database 
                    Dim TransactionSystem As New IBuySpy.TransactionDB()
                    TransactionSystem.AddTransaction(Txn_id, Receiver_email, Item_name, Item_number, Quantity, Invoice, _
                            [Custom], Payment_status, Pending_reason, Payment_date, Payment_fee, Payment_gross, _
                            Txn_type, First_name, Last_name, Address_street, Address_city, Address_state, _
                            Address_zip, Address_country, Address_status, Payer_email, Payer_status, Payer_id, _
                            Payment_type, Notify_version, Verify_sign)
                    Exit Select
                Case "INVALID"
                    ' Possible fraud. Log for investigation or an error 
                    MailUsTheOrder("INVALID: Possible fraud. Log for investigation or an error")
                    Exit Select
                Case Else
                    ' error 
                    MailUsTheOrder("Default: error: Response is: " + response)
                    Exit Select
            End Select
        End If
    End Sub
    Private Sub MailUsTheOrder(ByVal TagMsg As String)
        Dim mailObj As New MailMessage()
        mailObj.From = "sales@yourdomain.com"
        mailObj.Subject = TagMsg
        mailObj.Body = TagMsg + "<br>" + "<br>"
        mailObj.[To] = Receiver_email
        mailObj.BodyFormat = MailFormat.Html
        mailObj.Priority = MailPriority.High
        mailObj.Body = mailObj.Body + "Order ID: " + OrderID + "<br>" + "Transaction ID: " + Txn_id + "<br>" + "Transaction Type:" + Txn_type + "<br>" + "Payment Type: " + Payment_type + "<br>" + "Payment Status: " + Payment_status + "<br>" + "Pending Reason: " + Pending_reason + "<br>" + "Payment Date: " + Payment_date + "<br>" + "Receiver Email: " + Receiver_email + "<br>" + "Invoice: " + Invoice + "<br>" + "Item Number: " + Item_number + "<br>" + "Item Name: " + Item_name + "<br>" + "Quantity: " + Quantity + "<br>" + "Custom: " + [Custom] + "<br>" + "Payment Gross: " + Payment_gross + "<br>" + "Payment Fee: " + Payment_fee + "<br>" + "Payer Email: " + Payer_email + "<br>" + "First Name: " + First_name + "<br>" + "Last Name: " + Last_name + "<br>" + "Street Address: " + Address_street + "<br>" + "City: " + Address_city + "<br>" + "State: " + Address_state + "<br>" + "Zip Code: " + Address_zip + "<br>" + "Country: " + Address_country + "<br>" + "Address Status: " + Address_status + "<br>" + "Payer Status: " + Payer_status + "<br>" + "Verify Sign: " + Verify_sign + "<br>" + "Subscriber Date: " + Subscr_date + "<br>" + "Period 1: " + Period1 + "<br>" + "Period 2: " + Period2 + "<br>" + "Period 3: " + Period3 + "<br>" + "Amount 1: " + Amount1 + "<br>" + "Amount 2: " + Amount2 + "<br>" + "Amount 3: " + Amount3 + "<br>" + "Recurring: " + Recurring + "<br>" + "Reattempt: " + Reattempt + "<br>" + "Retry At: " + Retry_at + "<br>" + "Recur Times: " + Recur_times + "<br>" + "UserName: " + Username + "<br>" + "Password: " + Password + "<br>" + "Subscriber ID: " + Subscr_id + "<br>" + "Notify Version: " + Notify_version + "<br>"
        SmtpMail.Send(mailObj)
    End Sub


End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ALaRiva
ALaRiva
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial