merchant_id = $id;
$this->merchant_key = $key;
if($server_type == "sandbox")
// changed from https://sandbox.google.com/ on 17 Jan 2007
$this->server_url = "https://sandbox.google.com/checkout/";
else
$this->server_url = "https://checkout.google.com/";
$this->schema_url = "http://checkout.google.com/schema/2";
$this->base_url = $this->server_url."cws/v2/Merchant/" .
$this->merchant_id;
$this->checkout_url = $this->base_url . "/checkout";
$this->checkout_diagnose_url = $this->base_url . "/checkout/diagnose";
$this->request_url = $this->base_url . "/request";
$this->request_diagnose_url = $this->base_url . "/request/diagnose";
$this->response = $response;
if(strpos(__FILE__, ':') !== false)
$path_delimiter = ';';
else
$path_delimiter = ':';
ini_set('include_path', ini_get('include_path').$path_delimiter.'.');
require_once('xml-processing/xmlparser.php');
$this->xml_parser = new XmlParser($response);
$this->root = $this->xml_parser->GetRoot();
$this->data = $this->xml_parser->GetData();
}
function HttpAuthentication($headers) {
if(isset($headers['Authorization'])) {
$auth_encode = $headers['Authorization'];
$auth = base64_decode(substr($auth_encode,
strpos($auth_encode, " ") + 1));
$compare_mer_id = substr($auth, 0, strpos($auth,":"));
$compare_mer_key = substr($auth, strpos($auth,":")+1);
} else {
return false;
}
if($compare_mer_id != $this->merchant_id ||
$compare_mer_key != $this->merchant_key)
return false;
return true;
}
function SendChargeOrder($google_order, $amount='', $message_log) {
global $config;
$postargs = "
schema_url.
"\" google-order-number=\"". $google_order. "\">";
if ($amount != '') {
$postargs .= "" . $amount . "";
}
$postargs .= "";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendRefundOrder($google_order, $amount, $reason, $comment, $message_log) {
global $config;
$postargs = "
schema_url.
"\" google-order-number=\"". $google_order. "\">
". $reason . "
".htmlentities($amount)."
". htmlentities($comment) . "
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendCancelOrder($google_order, $reason, $comment, $message_log) {
$postargs = "
schema_url.
"\" google-order-number=\"". $google_order. "\">
". htmlentities($reason) . "
". htmlentities($comment) . "
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendTrackingData($google_order, $carrier, $tracking_no, $message_log) {
$postargs = "
schema_url .
"\" google-order-number=\"". $google_order . "\">
". htmlentities($carrier) . "
". $tracking_no . "
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendMerchantOrderNumber($google_order, $merchant_order, $message_log) {
$postargs = "
schema_url .
"\" google-order-number=\"". $google_order . "\">
" . $merchant_order .
"
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendBuyerMessage($google_order, $message, $send_mail="true",$message_log) {
$postargs = "
schema_url .
"\" google-order-number=\"". $google_order . "\">
" . $message . "
" . $send_mail . "
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendProcessOrder($google_order, $message_log) {
$postargs = "
schema_url .
"\" google-order-number=\"". $google_order. "\"/> ";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendDeliverOrder($google_order, $carrier, $tracking_no, $send_mail = "true", $message_log) {
$postargs = "
schema_url .
"\" google-order-number=\"". $google_order . "\">
". htmlentities($carrier) . "
". $tracking_no . "
". $send_mail . "
";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendArchiveOrder($google_order, $message_log) {
$postargs = "
schema_url.
"\" google-order-number=\"". $google_order. "\"/>";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function SendUnarchiveOrder($google_order, $message_log) {
$postargs = "
schema_url."\" google-order-number=\"".
$google_order. "\"/>";
return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
$postargs, $message_log);
}
function ProcessMerchantCalculations($merchant_calc) {
$result = $merchant_calc->GetXML();
echo $result;
}
function GetAuthenticationHeaders() {
$headers = array();
$headers[] = "Authorization: Basic ".base64_encode(
$this->merchant_id.':'.$this->merchant_key);
$headers[] = "Content-Type: application/xml";
$headers[] = "Accept: application/xml";
return $headers;
}
function SendReq($url, $header_arr, $postargs, $message_log) {
global $config;
// Get the curl session object
$session = curl_init($url);
// Set the POST options.
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_HTTPHEADER, $header_arr);
curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
if($config['proxy']==1){
curl_setopt ($session, CURLOPT_PROXY, $config['proxyHost'].":".$config['proxyPort']);
}
// Do the POST and then close the session
$response = curl_exec($session);
if (curl_errno($session)) {
die(curl_error($session));
} else {
curl_close($session);
}
// Get HTTP Status code from the response
$status_code = array();
preg_match('/\d\d\d/', $response, $status_code);
// Check for errors
switch( $status_code[0] ) {
case 200:
// Success
break;
case 503:
die('Error 503: Service unavailable.');
break;
case 403:
die('Error 403: Forbidden.');
break;
case 400:
die('Error 400: Bad request.');
break;
default:
echo $response;
die('Error :' . $status_code[0]);
}
fwrite($message_log, sprintf("\n\r%s:- %s\n",date("D M j G:i:s T Y"),
$response));
}
function SendAck() {
$acknowledgment = "" .
"schema_url . "\"/>";
echo $acknowledgment;
}
}
?>