Part 3 - Integrating Plug 'N Pay with Storefront Shopping Cart

by Erik Lane 17. March 2006 09:06

This is the last post from a short “How-To“ on integrating Plung 'N Pay (PNP) payment processing service into The Storefront.net 6.0 (SF6) shopping cart.

On to step #4:  Handling the Results of the Transaction.  Every payment processing service uses different technology and different methods of integration.  I'm going to describing how I did it with PNP.  The SF6 documentation says there are four basic categories of tasks you will need to accomplish.

This is the code that I’m using to accomplish the four tasks:

            Public Overloads Sub sendRequest()

            Dim strPNPUrl As String

            Dim strResponse As String

            strPNPUrl = String.Format(

MyBase.URI, MyBase.MerchantID, MyBase.UserID, MyBase.m_oOrder.BillAddress.Name, MyBase.m_oOrder.OrderPayment.CreditCardNumber, MyBase.m_oOrder.BillAddress.TotalBilled, MyBase.m_oOrder.OrderPayment.ExpireMonth, Right(MyBase.m_oOrder.OrderPayment.ExpireYear, 2))

strResponse = m_objPlugNPay.doTransaction

(

String.Empty, strPNPUrl, String.Empty, String.Empty

)

            setResponse(strResponse)

        End Sub

Task #1:  You will need a way to access the information stored for your payment processor in the PaymentProcessors table of the database.

The CProcessorBase class contains properties that gives you the access you need.  As I mentioned just a minute a ago, the call to fillProcessorInfo() populates these properties.  The SF6 documentation gives a full list of properties but below are the ones I'm using for PNP:  URI, MerchantID, and UserID.  I’m using these to tell me where to post my transaction to PNP and provide the authentication.

 

Task #2:  You will need a way to access information about the contents of the order being processed.

You can access information about the order by accessing properties of the objOrder object that was passed to the constructor.  The objOrder has two properties that contain the information we need about our order:  BillAddress and OrderPayment.  I’m using these to get the specifics about the credit card and amount to bill the card.

Task #3:  You will need a way to access information about the customer placing the order.

The BillAddress property returns an object of type StoreFront.SystemBase.AddressOrder, which contains those useful properties and you can see how I’m using them for PNP.

Task #4:  You will need to handle the response from the processor and record it in the store’s database.

The StoreFront.SystemBase namespace contains a class designed specifically for handling the results of payment processor transactions: CProcessorResponse. A CProcessorResponse object called m_objProcessResponse is a member of our base class so it's already available within your processor class. We’ll pass the results of the transaction returned from PNP to our overloaded setResponse method so we can parse the transaction, set the properties of m_objProcessResponse, and insert the response into the database.

       Public Overloads Sub setResponse(ByVal PNPResponse As String)

            Dim arResponses As Array, htResults As Hashtable

m_objprocessorresponse.OrderID = m_oOrder.UID

m_objProcessorResponse.ErrorLocation = "PlugNPay"

m_objProcessorResponse.FullResponse = PNPResponse

            arResponses = Split(PNPResponse, "&")

            htResults = GetResults(arResponses)

            With m_objProcessorResponse

.ActionCode = CStr(htResults.Item("FinalStatus"))

                .AuthorizationNo = CStr(htResults.Item("auth-code"))

                .AVSResult = CStr(htResults.Item("avs-code"))

                .CVVResult = CStr(htResults.Item("cvvresp"))

                .ErrorMessage = CStr(htResults.Item("MErrMsg"))

                .MerchTransNo = CStr(htResults.Item("orderID"))

                .OrderDate = CStr(htResults.Item("auth_date"))

                .Success = CBool(htResults.Item("FinalStatus") = "success")

            End With

            ' Track all responses

            If m_objProcessorResponse.Success Then

                m_objProcessorResponse.ErrorMessage = String.Empty

                ProcessorResponseAccess.InsertProcessorResponse(m_objProcessorResponse)

            Else

                ErrorMessage = m_objprocessorresponse.ErrorMessage

                ProcessorResponseAccess.InsertProcessorResponse(m_objProcessorResponse)

                Throw New SystemBase.AppException.PaymentGatewayException(ErrorMessage)

            End If

        End Sub

This is the private method GetResults to parse the PNP transaction.  I’m parsing the response according the PNP documentation.

        Private Function GetResults(ByVal PNPResponses As Array) As Hashtable

            Dim htResults As New Hashtable

            Dim i As Integer, pos As Integer

            Dim key As String, val As String

            For i = 0 To UBound(PNPResponses)

                PNPResponses(i) = Replace(PNPResponses(i), "+", " ")

                PNPResponses(i) = Replace(PNPResponses(i), "%20", " ")

                PNPResponses(i) = Replace(PNPResponses(i), "%21", "!")

                PNPResponses(i) = Replace(PNPResponses(i), "%23", "#")

                PNPResponses(i) = Replace(PNPResponses(i), "%24", "$")

                PNPResponses(i) = Replace(PNPResponses(i), "%25", "%")

                PNPResponses(i) = Replace(PNPResponses(i), "%26", "&")

                PNPResponses(i) = Replace(PNPResponses(i), "%27", "'")

                PNPResponses(i) = Replace(PNPResponses(i), "%28", "(")

                PNPResponses(i) = Replace(PNPResponses(i), "%29", ")")

                PNPResponses(i) = Replace(PNPResponses(i), "%2c", ",")

                PNPResponses(i) = Replace(PNPResponses(i), "%2d", "-")

                PNPResponses(i) = Replace(PNPResponses(i), "%2e", ".")

                PNPResponses(i) = Replace(PNPResponses(i), "%3a", ":")

                PNPResponses(i) = Replace(PNPResponses(i), "%40", "@")

                pos = InStr(1, PNPResponses(i), "=")

                If (pos > 1) Then

                    key = Left(PNPResponses(i), pos - 1)

                    val = Mid(PNPResponses(i), pos + 1)

                    htResults.Item(key) = val

                End If

            Next

            Return htResults

        End Function

 

Step 5: Indicating the Success of a Transaction

The final step in processing a transaction is to indicate to SF6 whether or not the transaction was a success or failure.  I also use this step to add the transaction to the database.  The SF6 documentation says all you have to do is set the ErrorMessage property of the m_objProcessorResponse object.  It (SF6) will know an error occurred if the ErrorMessage is not Empty or NULL.  I just throw an exception with the ErrorMessage.

            ' Track all responses

            If m_objProcessorResponse.Success Then

                m_objProcessorResponse.ErrorMessage = String.Empty

                ProcessorResponseAccess.InsertProcessorResponse(m_objProcessorResponse)

            Else

                ErrorMessage = m_objprocessorresponse.ErrorMessage

                ProcessorResponseAccess.InsertProcessorResponse(m_objProcessorResponse)

                Throw New SystemBase.AppException.PaymentGatewayException(ErrorMessage)

            End If

The steps required to process a transaction have been completed. The existing SF6 logic will pick up from this point, allowing the user to complete their order (if the transaction processed successfully), or returning them to the payments.aspx page and displaying the error message returned by the processing service.

 

Giddy Up!

Tags:
Comments are closed