In this article I am going to look at what you need to do to process the returned response from the Google Checkout API.
This article assumes you have read the first article and by the point of reading this article have managed to pass an order into Google Checkout, and got through the process to the thank you screen.
NOTE
This article also assumes that they are no errors in anything, and all API transactions Google Checkout sends baack are received and processed withut any problems.
I will address some problems you may encounter in my next article.
So how do you process the response from Google Checkout.
Prerequisite
Before we begin please make sure that you have the following be going on.
- An understanding of Classic ASP and how XML works
- Both a Google Checkout Account and Google Checkout Sandbox Account
- A Copy of the Classic ASP Sample Code - Get a Copy Here
Your website should already be at the point where all product information is passed to Google Checkout and you can complete a transaction with any problems.
Step 1 - Check your settings
The first thing to do is to make sure that you are actually asking Google Checkout to send you some sort of response.
- Make sure that the For Extra security... tick box is unchecked (if you leave this checked you will get URL errors when posting your order form to Google)
- Make sue the XML option is selected for the Callback Method (this means Google will send XML to your callback page and not name/value pairs)
- Enter your url of where your callback.asp page will be hosted. (when testing in sandbox this URL does not have to be SSL secured and can use the normal HTTP protocol
Step 2 - Catching the response
We are asking Google Checkout to pass us XML as a response so we need to do something with this. Again we are going to make our life a bit easier and make use of the Google Checkout code sample. We coudl how ever just parse the parent/child node from the XML and do what ever we like.
This will be covered in a later article around using .Net to send and receive sales to Google Checkout.
What do we get from Google
Google Checkout sends mainly two types of response to our callback.asp page.
- the new-order-notification - the first response letting us know there is a new Google Checkout order and order number.
- the order-state-change-notification - this is sent everytime the status of the order changes, such as the card being charged, etc
This is not to say these are the only notifications sent from Google, as in addition to the above there are also notifications sent for
- risk-information-notification - any information Google Receives about the risk of doing this order they will pass on.
- charge-amount-notification - the amount charged to the card
- chargeback-amount-notifcation - the amount that is placed on the card as chargeback
- refund-amount-notification - any refund amount that has been issued
- authorization-amount-notification - the actual amount that was authorized for payment
- merchant-calculation-callback - the calculation the merchant has asked for and the response
Now to make life a whole lot easier we are going to only implement the top 2 points of the API calls, as we are really only interested in
- The fact that we have a new order and what the Google Checkout Order Number is
- What is the status of the payment of this order.
The main file that we are going to use to process new orders is the callback.asp file which is provided in the sample source code from Google.
CallBack.asp
Inside this file there are two things that we want to predominantly do
- Receive the first response from Google, this will contain the Google Order Number.
- Receive the status of the buyers payment, such as PROCESSING, CHARGED or what ever.
Ok, so how do we do it?
Firstly inside this file there is a function called ProcessNewOrderNotification this function simply pulls in the XML response from Google and parses it out into an array called MyNewOrder.
Inside this function we simply loop through the array and pull out what we want. Again out lovely friends at Google have already done this for you.
You will see a For Next loop which passed the array elements out and puts these into a variable called MyNewOrder
We then just need to carryout some type of action to capture the information we want. So lets say we want to capture the Google Checkout order number and update our website with this information.
Here we simply take the variable MyNewOrder.GoogleOrdeNumber and a value we need at this point to identify this order on our own system, let use the unique merchant-item-id we passed in from our form and call the MyItem.MerchantItemId
All you do then is pass these values to somewhere you want to do something with them, if you simply are going to pass these to another page for processing your would do.
- Create a variable with the url string containing the location of the page for processing i.e "https://yourdomain.com/processneworder.asp?GoogleOrderNumber=" & MyNewOrder.GoogleOrderNumber & "&ItemID=" & MyItem.MerchantItemId
- Then simply redirect the response to your new page, jsut after the SendAck command with the current sub routine.
For Example
At around line 92 you enter the following code
Dim PostingURLString
PostingURLString = "https://yourdomain.com/processneworder.asp?GoogleOrderNumber=" & MyNewOrder.GoogleOrderNumber & "&username=" & MyItem.MerchantItemId & "&licensetype=" & left(MyItem.Name,5)
Then at around line 183 your do you redirection to your processing page
Response.Redirect(PostingURL)
You can take any of the values from the array and XMl response from Google, but in this example we are only taking the basic information we need.
- The Google Order Numder for our new order
- Our unique identifier to allow us to link the new order number with our order
Step 3 - the ongoing status
Up to this point we now are able to send the order to Google and we have updated our website to reflect this fact that our order send from our form, has now been received by Google Checkout and we have been given an order number.
The next thing we want to be concerned with is the ongoing status of the order and the most important thing, when have we received our money.
For this we are concerned with the ProcessOrderStateChangeNotification function, this function lets us track the payment process of the order and take the necessary action when an order is processed.
Again Google has taken care of the leg work for this case, and has passed the XML response sent from the Google Checkout API into an array, so all we really want to know is that when have we been paid, for this we call upon the MyOrderState.NewFinancialOrderState variable and simply use the code provided, and when this variable matches the payment status we want we do something.
On around line 255 of callback.asp you will see a select statement with all the possible options already taken card of, so all we need to do is simply add our custom action to the right choice.
For Example
Lets say we only care when we get paid all we do is add your action in the select statement for when the case matches CHARGED. So again we could pass this fact to a different page to process our order, and as the Google Order numbers as unique we can use the order number to pass to our website to provide a unique identifier to process this order.
This is as follows
On around line 252 we code
Dim UpdateURLString
UpdateURLString = "https://yourdomain.com/PageToUpdateOrderPaymentHistory.asp?GoogleOrderNumber=" & GoogleOrderNumber & "&GoogleStatus=" & MyOrderState.NewFinancialOrderState & "&GoogleReason=" & Reason
In this example we are passing the Google Order Number, the Payment Status and the Reason to our chosen URL.
Again we the can simply do a redirection to pass this information to our website to inform it of the new payment status.
So within the CHARGED section of the select statement your simply enter - line 273ish
Response.Redirect(UpdateURLString)
This example only passes information to our website on the new order and then when a successful payment is made, you could keep your website up to date with the payment status by simply putting the response.redirect statement within each case of the select statement. The details in the URL will change and allow you to update your order status on your website.
NOTE - the UpdateURLString must be created outside the Select Statement
This blog is aimed at giving you a basic insight into how to pass and retrieve an order using Google Checkout, there are a lot more options but using this blog you will be able to pass and create the order using Google.
Happy Coding.
Any Questions Just let me know

4 comments:
Hello,
All very interesting but how do I know if Google is hitting my callback page?
Thanks,
Dave
There are 2 ways to check that Goolge is hitting your callback page. Firstly Check in the integrration console under the tools tab, and if Google has encountered any problems, they will be listed here. Secondly if you find no mention of any problems, them on you callback page you need to write out a test response to a text file. Simply run an operation at the top of your page to write a line to a text file, that way if google hits this page your event will fire, and you can output the information google sends back.
this document contain, "At around line 92 you enter the following code..".
so tell me where we can get source code to see the line 92.?
You can get the source code from the Google Code website, the one for the Checkout is http://code.google.com/apis/checkout/ If you need any more hlep jsut let me know.
Happy coding
Sean
Post a Comment