2009-10-17 114 views
0

我在处理订单时看到了来自Google checkout的XML响应here如何解析来自Google结帐的XML响应?

我也看到了,我已经下面复制谷歌responsehandlerdemo.php文件。

我想不通的地方,我应该插入我会用它来更新我的店铺库存水平的代码。有人能指点我吗?我也不太清楚如何找到当我收到respohnse时命令的内容。

<?php 



    chdir(".."); 
    require_once('library/googleresponse.php'); 
    require_once('library/googlemerchantcalculations.php'); 
    require_once('library/googleresult.php'); 
    require_once('library/googlerequest.php'); 

    define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'googleerror.log'); 
    define('RESPONSE_HANDLER_LOG_FILE', 'googlemessage.log'); 
    $merchant_id = ""; // Your Merchant ID 
    $merchant_key = ""; // Your Merchant Key 
    $server_type = "sandbox"; // change this to go live 
    $currency = 'USD'; // set to GBP if in the UK 

    $Gresponse = new GoogleResponse($merchant_id, $merchant_key); 

    $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency); 

    //Setup the log file 
    $Gresponse->SetLogFiles(RESPONSE_HANDLER_ERROR_LOG_FILE, 
            RESPONSE_HANDLER_LOG_FILE, L_ALL); 

    // Retrieve the XML sent in the HTTP POST request to the ResponseHandler 
    $xml_response = isset($HTTP_RAW_POST_DATA)? 
       $HTTP_RAW_POST_DATA:file_get_contents("php://input"); 
    if (get_magic_quotes_gpc()) { 
    $xml_response = stripslashes($xml_response); 
    } 
    list($root, $data) = $Gresponse->GetParsedXML($xml_response); 
    $Gresponse->SetMerchantAuthentication($merchant_id, $merchant_key); 

    $status = $Gresponse->HttpAuthentication(); 
    if(! $status) { 
    die('authentication failed'); 
    } 

    /* Commands to send the various order processing APIs 
    * Send charge order : $Grequest->SendChargeOrder($data[$root] 
    * ['google-order-number']['VALUE'], <amount>); 
    * Send process order : $Grequest->SendProcessOrder($data[$root] 
    * ['google-order-number']['VALUE']); 
    * Send deliver order: $Grequest->SendDeliverOrder($data[$root] 
    * ['google-order-number']['VALUE'], <carrier>, <tracking-number>, 
    * <send_mail>); 
    * Send archive order: $Grequest->SendArchiveOrder($data[$root] 
    * ['google-order-number']['VALUE']); 
    * 
    */ 

    switch ($root) { 
    case "request-received": { 
    break; 
} 
case "error": { 
    break; 
} 
case "diagnosis": { 
    break; 
} 
case "checkout-redirect": { 
    break; 
} 
case "merchant-calculation-callback": { 
    // Create the results and send it 
    $merchant_calc = new GoogleMerchantCalculations($currency); 

    // Loop through the list of address ids from the callback 
    $addresses = get_arr_result($data[$root]['calculate']['addresses']['anonymous-address']); 
    foreach($addresses as $curr_address) { 
    $curr_id = $curr_address['id']; 
    $country = $curr_address['country-code']['VALUE']; 
    $city = $curr_address['city']['VALUE']; 
    $region = $curr_address['region']['VALUE']; 
    $postal_code = $curr_address['postal-code']['VALUE']; 

    // Loop through each shipping method if merchant-calculated shipping 
    // support is to be provided 
    if(isset($data[$root]['calculate']['shipping'])) { 
     $shipping = get_arr_result($data[$root]['calculate']['shipping']['method']); 
     foreach($shipping as $curr_ship) { 
     $name = $curr_ship['name']; 
     //Compute the price for this shipping method and address id 
     $price = 12; // Modify this to get the actual price 
     $shippable = "true"; // Modify this as required 
     $merchant_result = new GoogleResult($curr_id); 
     $merchant_result->SetShippingDetails($name, $price, $shippable); 

     if($data[$root]['calculate']['tax']['VALUE'] == "true") { 
      //Compute tax for this address id and shipping type 
      $amount = 15; // Modify this to the actual tax value 
      $merchant_result->SetTaxDetails($amount); 
     } 

     if(isset($data[$root]['calculate']['merchant-code-strings'] 
      ['merchant-code-string'])) { 
      $codes = get_arr_result($data[$root]['calculate']['merchant-code-strings'] 
       ['merchant-code-string']); 
      foreach($codes as $curr_code) { 
      //Update this data as required to set whether the coupon is valid, the code and the amount 
      $coupons = new GoogleCoupons("true", $curr_code['code'], 5, "test2"); 
      $merchant_result->AddCoupons($coupons); 
      } 
     } 
     $merchant_calc->AddResult($merchant_result); 
     } 
    } else { 
     $merchant_result = new GoogleResult($curr_id); 
     if($data[$root]['calculate']['tax']['VALUE'] == "true") { 
     //Compute tax for this address id and shipping type 
     $amount = 15; // Modify this to the actual tax value 
     $merchant_result->SetTaxDetails($amount); 
     } 
     $codes = get_arr_result($data[$root]['calculate']['merchant-code-strings'] 
      ['merchant-code-string']); 
     foreach($codes as $curr_code) { 
     //Update this data as required to set whether the coupon is valid, the code and the amount 
     $coupons = new GoogleCoupons("true", $curr_code['code'], 5, "test2"); 
     $merchant_result->AddCoupons($coupons); 
     } 
     $merchant_calc->AddResult($merchant_result); 
    } 
    } 
    $Gresponse->ProcessMerchantCalculations($merchant_calc); 
    break; 
} 
case "new-order-notification": { 
    $Gresponse->SendAck(); 
    break; 
} 
case "order-state-change-notification": { 
    $Gresponse->SendAck(); 
    $new_financial_state = $data[$root]['new-financial-order-state']['VALUE']; 
    $new_fulfillment_order = $data[$root]['new-fulfillment-order-state']['VALUE']; 

    switch($new_financial_state) { 
    case 'REVIEWING': { 
     break; 
    } 
    case 'CHARGEABLE': { 
     //$Grequest->SendProcessOrder($data[$root]['google-order-number']['VALUE']); 
     //$Grequest->SendChargeOrder($data[$root]['google-order-number']['VALUE'],''); 
     break; 
    } 
    case 'CHARGING': { 
     break; 
    } 
    case 'CHARGED': { 
     break; 
    } 
    case 'PAYMENT_DECLINED': { 
     break; 
    } 
    case 'CANCELLED': { 
     break; 
    } 
    case 'CANCELLED_BY_GOOGLE': { 
     //$Grequest->SendBuyerMessage($data[$root]['google-order-number']['VALUE'], 
     // "Sorry, your order is cancelled by Google", true); 
     break; 
    } 
    default: 
     break; 
    } 

    switch($new_fulfillment_order) { 
    case 'NEW': { 
     break; 
    } 
    case 'PROCESSING': { 
     break; 
    } 
    case 'DELIVERED': { 
     break; 
    } 
    case 'WILL_NOT_DELIVER': { 
     break; 
    } 
    default: 
     break; 
    } 
    break; 
} 
case "charge-amount-notification": { 
    //$Grequest->SendDeliverOrder($data[$root]['google-order-number']['VALUE'], 
    // <carrier>, <tracking-number>, <send-email>); 
    //$Grequest->SendArchiveOrder($data[$root]['google-order-number']['VALUE']); 
    $Gresponse->SendAck(); 
    break; 
} 
case "chargeback-amount-notification": { 
    $Gresponse->SendAck(); 
    break; 
} 
case "refund-amount-notification": { 
    $Gresponse->SendAck(); 
    break; 
} 
case "risk-information-notification": { 
    $Gresponse->SendAck(); 
    break; 
} 
default: 
    $Gresponse->SendBadRequestStatus("Invalid or not supported Message"); 
    break; 
    } 
/* In case the XML API contains multiple open tags 
    with the same value, then invoke this function and 
    perform a foreach on the resultant array. 
    This takes care of cases when there is only one unique tag 
    or multiple tags. 
    Examples of this are "anonymous-address", "merchant-code-string" 
    from the merchant-calculations-callback API 
*/ 
function get_arr_result($child_node) { 
    $result = array(); 
    if(isset($child_node)) { 
    if(is_associative_array($child_node)) { 
     $result[] = $child_node; 
    } 
    else { 
    foreach($child_node as $curr_node){ 
     $result[] = $curr_node; 
    } 
    } 
} 
return $result; 
} 

    /* Returns true if a given variable represents an associative array */ 
    function is_associative_array($var) { 
    return is_array($var) && !is_numeric(implode('', array_keys($var))); 
    } 
?> 

我假设,我想这样做在下面的块,但我真的只是不知道在所有的如何去解析XML,这是我从来没有使用PHP做过的事情。

case "new-order-notification": { 
     $Gresponse->SendAck(); 
     break;" 
    } 

回答

1

好像你在正确的轨道上:你想要当接收到以减小库存,所以在其他地方就可以,如果你的库存去立即显示该项目是脱销归零。

至于你的其他问题,看起来像$ data [$ root]数组的每个括号级别可以让你在XML结构中的另一个级别,就像你的示例代码的商人回调计算案例一样。

整个反应似乎是在$的数据,所以我只是看一下例子响应和闲逛的你在找什么呀。也许可以使用一些echo语句来查看在不同位置出现['VALUE']的内容。

完全是一个启发式的答案,但希望它帮助。 ;)

+0

如果没有别的,你就会回应我的想法,但不一定有足够的信心去捅捅,而不从其他人那里听到。非常感谢。 – 2009-10-18 01:02:31

1

我不会做很多PHP,但是您使用的是XML解析器吗?

我不能完全从你的代码告诉,但如果你没有它将使你的生活变得更轻松。

http://php.net/manual/en/book.xml.php

+0

这是谷歌的示例代码...它似乎是内部解析XML:$ Gresponse-> GetParsedXML($ xml_response) 我只是不知道如何指定我正在寻找的标签和使用PHP解析器获取所述标签的值。 – 2009-10-17 18:04:33

0

约翰说,但确认这里的答案是$数据是关键。

例如,订单号是可以使用以下被回显值:

$数据[$根] [ '谷歌阶数'] [ 'VALUE']

...等等。