Pripyat
Pripyat

Reputation: 2937

Google Checkout PHP Submission Question

I have just started getting familiar with the Google Checkout API, but there's something I have a question about. On the Google Checkout Documentation, the only way of submitting the actual cart is via a button that is created via an echo call, like so echo $cart->CheckoutButtonCode("LARGE"); This however is not what I need. I want to manually submit my cart from my PHP script.

However, unlike the PayPal API, there appears to be no submit type function in the Google Checkout script. After some further research, I noticed that the HTML examples post their fields to https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/MERCHANT_ID_HERE.

How can I do this in PHP? I am using their official API. This is what I have done so far:

  $merchant_id = $google_merchant_ID;
  $merchant_key = $google_merchant_key;

  if ($enable_Google_Sandbox == 1)
  {
      $server_type = "sandbox";
  }

  $currency = $currency_code;
  $cart = new GoogleCart($merchant_id, $merchant_key, $server_type,
  $currency);

  $shop_cart = $_SESSION['cart'];

  foreach ($shop_cart as $value)
  {    
    $k_product = $value['Product'];
    $k_quantity = $value['Quantity'];
    $k_price = $value['Price'];
    $k_orderID = $_SESSION['order_id'];

    if (isset($_SESSION['Discount']))
    {
        $k_discount = $_SESSION['Discount'];

        $k_price = $k_price - $k_discount;
    }

    $cart_item = new GoogleItem($k_product, 
                           "Some Product",
                           $k_quantity,
                           $k_price);

    $cart_item->SetMerchantItemId(generateProductID());

    $cart->AddItem($cart_item);
  }

  // Specify <edit-cart-url>
  $cart->SetEditCartUrl("http://192.168.100.100:8888/order.php?action=showCart");

  // Specify "Return to xyz" link
  $cart->SetContinueShoppingUrl("http://192.168.100.100:8888/store.php");

  // Request buyer's phone number
  $cart->SetRequestBuyerPhone(false);

There's no $cart->submitCart(); type function, so what do I do?

Upvotes: 1

Views: 564

Answers (1)

Maximilian Hils
Maximilian Hils

Reputation: 6770

list($status, $error) = $cart->CheckoutServer2Server();

That should solve your problem. As you're using the PHP api, i can recommend the PHP demos. CheckoutServer2Server is demonstrated in this example (DigitalUsecase()). (digitalCart.php)

CheckoutServer2Server docs:

/**
 * Submit a server-to-server request.
 * Creates a GoogleRequest object (defined in googlerequest.php) and sends 
 * it to the Google Checkout server.
 * 
 * more info:
 * {@link http://code.google.com/apis/checkout/developer/index.html#alternate_technique}
 * 
 * @return array with the returned http status code (200 if OK) in index 0 
 *               and the redirect url returned by the server in index 1
 */

Upvotes: 2

Related Questions