Search code examples
phppaypalpaypal-ipngoogle-checkout

Google Checkout IPN


I have set up a buy now button which has a set value, and its sending to the Google server properly, and showing the pay now page etc. I am also sending an additional parameter using merchant-private-data or whatever it's called.

The way I am doing all of this is via cURL so there is no way for the user to force a lower charge without me finding out and stopping it. (Validation done on all of the inputs etc).

So since I'm passing all of that data through. I need a way to fetch it all back, and I am currently unsure how to do it.

I have already read the docs, and I just got confused by them.

I know that I need to find out whether it's chargeable yet or not, and if it is then automatically process the charge for them. But is there a way to get it so that that happens, then when I receive the money I can update a database with the extra data I sent to them? If so, how would I do this?

This is my current code:

$fields = array(
            'item_name_1'=>urlencode($name),
            'item_description_1'=>urlencode($name . ' of [xxx]'),
            'item_quantity_1'=>urlencode('1'),
            'item_price_1'=>urlencode($value),
        'item_currency_1'=>urlencode('GBP'),
        '_charset_'=>'utf-8',
    'shopping-cart.merchant-private-data' => '[username]',
    'shopping-cart.items.item-1.digital-content.description' => 'Your account has been upgraded. 


    'shopping-cart.items.item-1.digital-content.email-delivery' => 'true'
             );
$fields_string = "";
foreach($fields as $key=>$value)
        {
    $fields_string .= $key.'='.$value.'&'; 
    }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/8048xxx');
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$result = curl_exec($ch);
curl_close($ch);

for the process towards the Google checkout (when they click buy now button)

and this is the code for the 'processing' which is currently not working.

if(isset($_POST['serial-number'])){
    // new order made
    $sn = $_POST['serial-number'];
    $id = '804xxx';
    $key = 'xxxxx';

    $fields = array('_type'=>'notification-history-request',
                    'serial-number'=>urlencode($sn));

                    $fields_string = "";
                    foreach($fields as $key=>$value) {
                         $fields_string .= $key.'='.$value.'&'; 
                    }

                    rtrim($fields_string, '&');

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, 'https://checkout.google.com/api/checkout/v2/reportsForm/Merchant/8048xxx');
                    curl_setopt($ch, CURLOPT_POST, count($fields));
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode($id . ':' . $key), 'Content-Type: application/xml;charset=UTF-8', 'Accept: application/xml;charset=UTF-8'));
                    $result = curl_exec($ch);
                    if(curn_errno($ch)){
                        $w = fopen('log.text', 'w+');
                        fwrite($w, 'Error: ' . curl_error($ch));
                        fclose($w);
                    } else {
                        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                        $f = fopen('test.txt', 'w+');
                        fwrite($f, 'Status: ' . $status . ' ' . print_r($result, true));
                        fclose($f);
                    }
                    curl_close($ch);
   }

Any ideas?


Solution

  • To get back order status information and merchant-private-data you need to listen to the notifications sent to your callback URL. Please see the docs below on how to integrate with the Checkout Notifications API.

    http://support.google.com/checkout/sell/bin/answer.py?hl=en&answer=70647

    http://support.google.com/checkout/sell/bin/answer.py?hl=en-GB&answer=134463

    https://developers.google.com/checkout/developer/Google_Checkout_XML_API_Notification_API

    When you get back the notification, read your custom data from the merchant-private-data field.

    See this answer for a solution to a similar issue.

    You may also want to have a look at the Checkout sample code libraries for various languages:

    https://developers.google.com/checkout/samplecode