Asked by:
Multiple Subs in one page with the same Name? How is this possible?

Question
-
User207797918 posted
I'm trying to convert a VB.NET app (sample code from Authorize.net) to ASP.NET. In this VB.NET code I see three Subs with the same name being used. How does this work? The code is below and the sub name is PopulateSubscription().
Private Sub PopulateSubscription(ByVal request As ARBCreateSubscriptionRequest)
Private Sub PopulateSubscription(ByVal request As ARBUpdateSubscriptionRequest)
Private Sub PopulateSubscription(ByVal request As ARBCancelSubscriptionRequest)Does the "As blablablaRequest" have anything to do with it? Are there any documentation pages someone can point me to so I can understand how this works? thanks! :D
' This is the URL of the API server.
' You must give this a valid value to successfully execute this sample code.
Private _apiUrl As String = "https://api.authorize.net/xml/v1/request.api"
' This is the user's payment gateway account login name used for transaction processing.
' You must give this a valid value to successfully execute this sample code.
Private _userLoginName As String = "myLoginName"
' This is the transaction key associated with the account.
' You must give this a valid value to successfully execute this sample code.
Private _transactionKey As String = "myTransactionKey"
' This will be set by CreateSubscription and used for UpdateSubscription
' and CancelSubscription
Private _subscriptionId As String
' Controls whether the XML request and response are written to the console.
Private _dumpXml As Boolean
' ----------------------------------------------------------------------------------------
' The main entry point for the application.
' ----------------------------------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim bResult As Boolean = True
MessageLabel.Text &= "<br />" & ("API server: " + _apiUrl)
If bResult Then
' Set this to true if you want to see the XML requests and responses dumped
' to the console window. Set to false otherwise.
_dumpXml = False
' Create a new subscription
MessageLabel.Text &= "<br />" & "This call to ARBCreateSubscription should be successful."
MessageLabel.Text &= "<br />" & "If you receive an duplicate subscription error change the Amount being sent to the server."
bResult = CreateSubscription(False)
' This example will generate an error because a duplicate subscription
' is being created.
'MessageLabel.Text &= "<br />" & "This call to ARBCreateSubscription shows how to handle errors."
'CreateSubscription(False)
' This example will generate an error because the Amount is missing, causing
' a schema validatation error to occur on the server side.
'MessageLabel.Text &= "<br />" & "This call to ARBCreateSubscription shows how to process ErrorResponse."
'CreateSubscription(True)
' Update the subscription that was just created
'If (bResult AndAlso (Not (_subscriptionId) Is Nothing)) Then
' MessageLabel.Text &= "<br />" & "This call to ARBUpdateSubscription should be successful."
' bResult = UpdateSubscription()
' ' Cancel the subscription that was just created
' If bResult Then
' MessageLabel.Text &= "<br />" & "This call to ARBCancelSubscription should be successful."
' bResult = CancelSubscription()
' End If
'End If
End If
MessageLabel.Text &= "<br />" & "Request finished (successfully or unsuccessfully)."
End Sub
' ----------------------------------------------------------------------------------------
' Create a new ARB Subscription.
' ----------------------------------------------------------------------------------------
Private Function CreateSubscription(ByVal forceXmlError As Boolean) As Boolean
Dim bResult As Boolean = True
MessageLabel.Text &= "<br />" & "Create subscription"
' This is the API interface object.
Dim createSubscriptionRequest As ARBCreateSubscriptionRequest = New ARBCreateSubscriptionRequest
' Populate the subscription request with test data
PopulateSubscription(createSubscriptionRequest)
If forceXmlError Then
createSubscriptionRequest.merchantAuthentication = Nothing
End If
' The response type will normally be ARBCreateSubscriptionResponse.
' However, in the case of an error such as an XML parsing error, the response
' type will be ErrorResponse.
Dim response As Object = Nothing
Dim xmldoc As XmlDocument = Nothing
bResult = PostRequest(createSubscriptionRequest, xmldoc)
If bResult Then
bResult = ProcessXmlResponse(xmldoc, response)
End If
If bResult Then
ProcessResponse(response)
End If
If Not bResult Then
MessageLabel.Text &= "<br />" & "An unexpected error occurred processing this request."
End If
Return bResult
End Function
' ----------------------------------------------------------------------------------------
' Update an existing ARB subscription using the subscription ID returned by the create.
' ----------------------------------------------------------------------------------------
Private Function UpdateSubscription() As Boolean
Dim bResult As Boolean = True
MessageLabel.Text &= "<br />" & "Update subscription"
' This is the API interface object.
Dim updateSubscriptionRequest As ARBUpdateSubscriptionRequest = New ARBUpdateSubscriptionRequest
' Populate the subscription request with test data
PopulateSubscription(updateSubscriptionRequest)
' The response type will normally be ARBUpdateSubscriptionResponse.
' However, in the case of an error such as an XML parsing error, the response
' type will be ErrorResponse.
Dim response As Object = Nothing
Dim xmldoc As XmlDocument = Nothing
bResult = PostRequest(updateSubscriptionRequest, xmldoc)
If bResult Then
bResult = ProcessXmlResponse(xmldoc, response)
End If
If bResult Then
ProcessResponse(response)
End If
If Not bResult Then
MessageLabel.Text &= "<br />" & "An unexpected error occurred processing this request."
End If
Return bResult
End Function
' ----------------------------------------------------------------------------------------
' Cancel an existing ARB subscription using the subscription ID returned by the create.
' ----------------------------------------------------------------------------------------
Private Function CancelSubscription() As Boolean
Dim bResult As Boolean = True
MessageLabel.Text &= "<br />" & "Cancel subscription"
' This is the API interface object.
Dim cancelSubscriptionRequest As ARBCancelSubscriptionRequest = New ARBCancelSubscriptionRequest
' Populate the subscription request with test data
PopulateSubscription(cancelSubscriptionRequest)
' The response type will normally be ARBCancelSubscriptionRequest.
' However, in the case of an error such as an XML parsing error, the response
' type will be ErrorResponse.
Dim response As Object = Nothing
Dim xmldoc As XmlDocument = Nothing
bResult = PostRequest(cancelSubscriptionRequest, xmldoc)
If bResult Then
bResult = ProcessXmlResponse(xmldoc, response)
End If
If bResult Then
ProcessResponse(response)
End If
If Not bResult Then
MessageLabel.Text &= "<br />" & "An unexpected error occurred processing this request."
End If
Return bResult
End Function
' ----------------------------------------------------------------------------------------
' Fill in the given request with test data to create a new subscription.
' ----------------------------------------------------------------------------------------
Private Sub PopulateSubscription(ByVal request As ARBCreateSubscriptionRequest)
Dim subscr As ARBSubscription = New ARBSubscription
Dim creditCard As CreditCard = New CreditCard
subscr.name = "Sample subscription"
creditCard.cardNumber = "4111111111111111"
creditCard.expirationDate = "2008-08" ' Required format is YYYY-MM
subscr.payment = New Payment
subscr.payment.item = creditCard
subscr.billTo = New NameAndAddress
subscr.billTo.firstName = "John"
subscr.billTo.lastName = "Smith"
' Create a subscription that is 12 monthly payments starting on Mar 15, 2007
' with 1 month free (trial period).
subscr.paymentSchedule = New PaymentSchedule
subscr.paymentSchedule.startDate = "2008-12-15" ' Required format is YYYY-MM-DD
subscr.paymentSchedule.totalOccurrences = 12
subscr.paymentSchedule.totalOccurrencesSpecified = True
' Free 1-month trial
subscr.paymentSchedule.trialOccurrences = 1
subscr.paymentSchedule.trialOccurrencesSpecified = True
subscr.trialAmount = 0
subscr.trialAmountSpecified = True
subscr.amount = 10.28
subscr.amountSpecified = True
subscr.paymentSchedule.interval.length = 1
subscr.paymentSchedule.interval.unit = SubscriptionUnitType.months
subscr.paymentSchedule.intervalSpecified = True
' Include authentication information
PopulateMerchantAuthentication(CType(request, ANetApiRequest))
request.subscription = subscr
End Sub
' ----------------------------------------------------------------------------------------
' Fill in the given request with test data used to update the subscription.
' ----------------------------------------------------------------------------------------
Private Sub PopulateSubscription(ByVal request As ARBUpdateSubscriptionRequest)
Dim subscr As ARBSubscription = New ARBSubscription
Dim creditCard As CreditCard = New CreditCard
request.subscriptionId = _subscriptionId
' For this sample just update the credit card information.
creditCard.cardNumber = "4111111111111111"
creditCard.expirationDate = "2010-08" ' Required format is YYYY-MM
subscr.payment = New Payment
subscr.payment.item = creditCard
' Include authentication information
PopulateMerchantAuthentication(CType(request, ANetApiRequest))
request.subscription = subscr
End Sub
' ----------------------------------------------------------------------------------------
' Fill in the given request with test data used to cancel the subscription.
' ----------------------------------------------------------------------------------------
Private Sub PopulateSubscription(ByVal request As ARBCancelSubscriptionRequest)
Dim creditCard As CreditCard = New CreditCard
request.subscriptionId = _subscriptionId
' Include authentication information
PopulateMerchantAuthentication(CType(request, ANetApiRequest))
End Sub
' ----------------------------------------------------------------------------------------
' Fill in the merchant authentication. This data is required for all API methods.
' ----------------------------------------------------------------------------------------
Private Sub PopulateMerchantAuthentication(ByVal request As ANetApiRequest)
request.merchantAuthentication = New MerchantAuthentication
request.merchantAuthentication.name = _userLoginName
request.merchantAuthentication.transactionKey = _transactionKey
request.refId = "Sample"
End Sub
' ----------------------------------------------------------------------------------------
' Send the request to the API server and load the response into an XML document.
' An XmlSerializer is used to form the XML used in the request to the API server.
' The response from the server is also XML. An XmlReader is used to process the
' response stream from the API server so that it can be loaded into an XmlDocument.
'
' Returns true if successful, false if not. If true then the specified XmlDoc will contain the
' response received from the API server.
' ----------------------------------------------------------------------------------------
Private Function PostRequest(ByVal apiRequest As Object, ByRef xmldoc As XmlDocument) As Boolean
Dim bResult As Boolean = False
Dim serializer As XmlSerializer
Dim wr As WebRequest
wr = WebRequest.Create(_apiUrl)
xmldoc = Nothing
Try
Dim webRequest As HttpWebRequest = CType(wr, HttpWebRequest)
webRequest.Method = "POST"
webRequest.ContentType = "text/xml"
webRequest.KeepAlive = True
' Serialize the request
serializer = New XmlSerializer(apiRequest.GetType)
Dim writer As XmlWriter = New XmlTextWriter(webRequest.GetRequestStream, Encoding.UTF8)
serializer.Serialize(writer, apiRequest)
writer.Close()
If _dumpXml Then
Console.WriteLine("" & vbCrLf & vbCrLf)
Dim consoleOutput As StreamWriter = New StreamWriter(Console.OpenStandardOutput)
consoleOutput.AutoFlush = True
serializer.Serialize(consoleOutput, apiRequest)
consoleOutput.Close()
Console.WriteLine("" & vbCrLf & vbCrLf)
End If
' Get the response
Dim webResponse As WebResponse = webRequest.GetResponse
' Load the response from the API server into an XmlDocument
xmldoc = New XmlDocument
xmldoc.Load(XmlReader.Create(webResponse.GetResponseStream))
If _dumpXml Then
Dim settings As XmlWriterSettings = New XmlWriterSettings
settings.Indent = True
settings.Encoding = Encoding.ASCII
Dim consoleWriter As XmlWriter = XmlWriter.Create(Console.Out, settings)
xmldoc.WriteTo(consoleWriter)
consoleWriter.Close()
Console.WriteLine("" & vbCrLf & vbCrLf)
End If
bResult = True
Catch ex As Exception
MessageLabel.Text &= "<br />" & (ex.GetType.ToString + (": " + ex.Message))
bResult = False
End Try
Return bResult
End Function
' ----------------------------------------------------------------------------------------
' Deserialize the given XML document into the correct object type using the root
' node to determine the type of output object.
'
' For any given API request the response can be one of two types:
' ErorrResponse or [methodname]Response.
' For example, the ARBCreateSubscriptionRequest would normally result in a response of
' ARBCreateSubscriptionResponse. This is also the name of the root node of the response.
' This name can be used to deserialize the response into local objects.
'
' param name="xmldoc"
' This is the XML document to process. It holds the response from the API server.
'
' param name="apiResponse"
' This will hold the deserialized object of the appropriate type.
'
'Returns: True if successful, false if not.
' ----------------------------------------------------------------------------------------
Private Function ProcessXmlResponse(ByVal xmldoc As XmlDocument, ByRef apiResponse As Object) As Boolean
Dim bResult As Boolean = True
Dim serializer As XmlSerializer
apiResponse = Nothing
Try
' Use the root node to determine the type of response object to create.
Select Case (xmldoc.DocumentElement.Name)
Case "ARBCreateSubscriptionResponse"
serializer = New XmlSerializer(GetType(ARBCreateSubscriptionResponse))
apiResponse = CType(serializer.Deserialize(New StringReader(xmldoc.DocumentElement.OuterXml)), ARBCreateSubscriptionResponse)
Case "ARBUpdateSubscriptionResponse"
serializer = New XmlSerializer(GetType(ARBUpdateSubscriptionResponse))
apiResponse = CType(serializer.Deserialize(New StringReader(xmldoc.DocumentElement.OuterXml)), ARBUpdateSubscriptionResponse)
Case "ARBCancelSubscriptionResponse"
serializer = New XmlSerializer(GetType(ARBCancelSubscriptionResponse))
apiResponse = CType(serializer.Deserialize(New StringReader(xmldoc.DocumentElement.OuterXml)), ARBCancelSubscriptionResponse)
Case "ErrorResponse"
serializer = New XmlSerializer(GetType(ErrorResponse))
apiResponse = CType(serializer.Deserialize(New StringReader(xmldoc.DocumentElement.OuterXml)), ErrorResponse)
Case Else
MessageLabel.Text &= "<br />" & ("Unexpected type of object: " + xmldoc.DocumentElement.Name)
bResult = False
End Select
Catch ex As Exception
bResult = False
apiResponse = Nothing
MessageLabel.Text &= "<br />" & (ex.GetType.ToString + (": " + ex.Message))
End Try
Return bResult
End Function
' ----------------------------------------------------------------------------------------
' Determine the type of the response object and process accordingly.
' Since this is just sample code the only processing being done here is to write a few
' bits of information to the console window.
' ----------------------------------------------------------------------------------------
Private Sub ProcessResponse(ByVal response As Object)
' Every response is based on ANetApiResponse so you can always do this sort of type casting.
Dim baseResponse As ANetApiResponse = CType(response, ANetApiResponse)
' Write the results to the console window
MessageLabel.Text &= "<br />" & "Result: "
MessageLabel.Text &= "<br />" & baseResponse.messages.resultCode.ToString
' If the result code is "Ok" then the request was successfully processed.
If (baseResponse.messages.resultCode = Messages.MessageType.IsOk) Then
' CreateSubscription is the only method that returns additional data.
If (response.GetType.Equals(GetType(ARBCreateSubscriptionResponse))) Then
Dim createResponse As ARBCreateSubscriptionResponse = CType(response, ARBCreateSubscriptionResponse)
_subscriptionId = createResponse.subscriptionId
MessageLabel.Text &= "<br />" & ("Subscription ID: " + _subscriptionId)
End If
Else
' Write error messages to console window
Dim i As Integer = 0
Do While (i < baseResponse.messages.messageList.Length)
MessageLabel.Text &= "<br />" & ("[" _
+ (baseResponse.messages.messageList(i).code _
+ ("] " + baseResponse.messages.messageList(i).text)))
i = (i + 1)
Loop
End If
End Sub
Wednesday, June 25, 2008 8:12 PM
All replies
-
User397347636 posted
These are just method overloads. You can have methods with the same name provided that the parameter list is different.
You can see this in numerous places in the .NET Framework: e.g., many of the System.String methods are overloaded.
Wednesday, June 25, 2008 8:40 PM