2011-09-18 50 views
1

我剛開始熟悉Google Checkout API,但是我有一個問題。在Google Checkout文檔中,提交實際購物車的唯一方法是通過一個通過echo呼叫創建的按鈕,如echo $cart->CheckoutButtonCode("LARGE");這不是我所需要的。我想從我的PHP腳本手動提交我的購物車。Google Checkout PHP提交問題

但是,與PayPal API不同,Google Checkout腳本似乎沒有提交類型功能。經過一些進一步的研究,我注意到HTML的例子發佈到https://sandbox.google.com/checkout/api/checkout/v2/checkout/Merchant/MERCHANT_ID_HERE

我該如何在PHP中做到這一點?我正在使用他們的官方API。這是我迄今所做的:

$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); 

有沒有$cart->submitCart();型功能,所以我該怎麼辦?

回答

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

這應該可以解決您的問題。當你使用PHP API時,我可以推薦PHP demos。在this示例(DigitalUsecase())中演示了CheckoutServer2Server。 (digitalCart.php

CheckoutServer2Server文檔:

/** 
* 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 
*/ 
+0

感謝您詳細的解答。然而,沒有重定向到Google發生... – Pripyat

+0

出現錯誤:[0] => ERR [1] =>服務器內部錯誤 – Pripyat

+0

我正在使用舊的API。它適用於新的。我會盡快給你賞金。 – Pripyat