urls['schema'] = 'http://checkout.google.com/schema/2'; $this->urls['checkout'] = array( 'live' => 'https://%s:%s@checkout.google.com/api/checkout/v2/merchantCheckout/Merchant/%s', 'test' => 'https://%s:%s@sandbox.google.com/checkout/api/checkout/v2/merchantCheckout/Merchant/%s' ); $this->urls['order'] = array( 'live' => 'https://%s:%s@checkout.google.com/api/checkout/v2/request/Merchant/%s', 'test' => 'https://%s:%s@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/%s' ); $this->urls['button'] = array( 'live' => 'http://checkout.google.com/buttons/checkout.gif', 'test' => 'http://sandbox.google.com/checkout/buttons/checkout.gif' ); $this->settings = $Shopp->Settings->get('GoogleCheckout'); $this->settings['merchant_email'] = $Shopp->Settings->get('merchant_email'); $this->settings['location'] = "en_US"; $base = $Shopp->Settings->get('base_operations'); if ($base['country'] == "GB") $this->settings['location'] = "en_UK"; $this->settings['base_operations'] = $Shopp->Settings->get('base_operations'); $this->settings['currency'] = $this->settings['base_operations']['currency']['code']; if (empty($this->settings['currency'])) $this->settings['currency'] = "USD"; $this->settings['taxes'] = $Shopp->Settings->get('taxrates'); add_action('shopp_save_payment_settings',array(&$this,'saveSettings')); return true; } function actions () { } function checkout () { global $Shopp; if ($Shopp->Cart->data->Totals->total == 0) shopp_redirect($Shopp->link('checkout')); $this->transaction = $this->buildCheckoutRequest($Shopp->Cart); $Response = $this->send($this->urls['checkout']); if (!empty($Response)) { if ($Response->getElement('error')) return $this->error(); $redirect = false; $redirect = $Response->getElementContent('redirect-url'); if ($redirect) { // Empty cart on successful sending the order to Google $Shopp->Cart->unload(); session_destroy(); // Start new cart session, just in case they come back for more $Shopp->Cart = new Cart(); session_start(); shopp_redirect($redirect); } } return false; } function process () { if ($this->authentication()) { // Read incoming request data $data = trim(file_get_contents('php://input')); // Handle notifications $XML = new XMLdata($data); $type = key($XML->data); $serial = $XML->getElementAttr($type,'serial-number'); switch($type) { case "new-order-notification": $this->order($XML); break; case "risk-information-notification": $this->risk($XML); break; case "order-state-change-notification": $this->state($XML); break; case "charge-amount-notification": // Not implemented case "refund-amount-notification": // Not implemented case "chargeback-amount-notification": // Not implemented case "authorization-amount-notification": // Not implemented break; } // Send acknowledgement $this->acknowledge($serial); } exit(); } /** * authcode() * Build a hash code for the merchant id and merchant key */ function authcode ($id,$key) { return sha1($id.$key); } /** * authentication() * Authenticate an incoming request */ function authentication () { if (isset($_GET['merc'])) $merc = $_GET['merc']; if (!empty($this->settings['id']) && !empty($this->settings['key']) && $_GET['merc'] == $this->authcode($this->settings['id'],$this->settings['key'])); return true; header('HTTP/1.0 401 Unauthorized'); die("
"; print_r($_); echo ""; return join("\n",$_); } /** * order() * Handles new order notifications from Google */ function order ($XML) { global $Shopp; $db = DB::get(); // Check if this is a Shopp order or not $origin = $XML->getElementContent('shopping-cart-agent'); if (empty($origin) || substr($origin,0,strpos("/",SHOPP_GATEWAY_USERAGENT)) == SHOPP_GATEWAY_USERAGENT) return true; $buyer = $XML->getElement('buyer-billing-address'); $buyer = $buyer['CHILDREN']; $Customer = new Customer(); $name = $XML->getElement('structured-name'); $Customer->firstname = $buyer['structured-name']['CHILDREN']['first-name']['CONTENT']; $Customer->lastname = $buyer['structured-name']['CHILDREN']['last-name']['CONTENT']; if (empty($name)) { $name = $buyer['contact-name']['CONTENT']; $names = explode(" ",$name); $Customer->firstname = $names[0]; $Customer->lastname = $names[count($names)-1]; } $Customer->email = $buyer['email']['CONTENT']; $Customer->phone = $buyer['phone']['CONTENT']; $Customer->save(); $Billing = new Billing(); $Billing->customer = $Customer->id; $Billing->address = $buyer['address1']['CONTENT']; $Billing->xaddress = $buyer['address2']['CONTENT']; $Billing->city = $buyer['city']['CONTENT']; $Billing->state = $buyer['region']['CONTENT']; $Billing->country = $buyer['country-code']['CONTENT']; $Billing->postcode = $buyer['postal-code']['CONTENT']; $Billing->save(); $shipto = $XML->getElement('buyer-shipping-address'); $shipto = $shipto['CHILDREN']; $Shipping = new Shipping(); $Shipping->customer = $Customer->id; $Shipping->address = $shipto['address1']['CONTENT']; $Shipping->xaddress = $shipto['address2']['CONTENT']; $Shipping->city = $shipto['city']['CONTENT']; $Shipping->state = $shipto['region']['CONTENT']; $Shipping->country = $shipto['country-code']['CONTENT']; $Shipping->postcode = $shipto['postal-code']['CONTENT']; $Shipping->save(); $Purchase = new Purchase(); $Purchase->customer = $Customer->id; $Purchase->billing = $Billing->id; $Purchase->shipping = $Shipping->id; $Purchase->copydata($Customer); $Purchase->copydata($Billing); $Purchase->copydata($Shipping,'ship'); $Purchase->freight = $XML->getElementContent('shipping-cost'); $Purchase->tax = $XML->getElementContent('total-tax'); $Purchase->total = $XML->getElementContent('order-total'); $Purchase->subtotal = $Purchase->total-$Purchase->frieght-$Purchase->tax; $Purchase->gateway = "Google Checkout"; $Purchase->transactionid = $XML->getElementContent('google-order-number'); $Purchase->transtatus = $XML->getElementContent('financial-order-state'); $Purchase->ip = $XML->getElementContent('customer-ip'); $orderdata = $XML->getElement('shopp-order-data'); $data = array(); if (is_array($orderdata) && count($orderdata) > 0) foreach ($orderdata as $input) $data[$input['ATTRS']['name']] = $input['CONTENT']; $Purchase->data = $data; $Purchase->save(); $items = $XML->getElement('item'); if (key($items) === "CHILDREN") $items = array($items); foreach ($items as $item) { $xml = $item['CHILDREN']; $itemdata = $xml['merchant-private-item-data']['CHILDREN']; $inputdata = $itemdata['shopp-item-data-list']['CHILDREN']['shopp-item-data']; $data = array(); if (is_array($inputdata) && count($inputdata) > 0) foreach ($inputdata as $input) $data[$input['ATTRS']['name']] = $input['CONTENT']; $Product = new Product($itemdata['shopp-product-id']['CONTENT']); $Item = new Item($Product,$itemdata['shopp-price-id']['CONTENT'],false,$data); $Item->quantity($xml['quantity']['CONTENT']); $Purchased = new Purchased(); $Purchased->copydata($Item); $Purchased->purchase = $Purchase->id; if (!empty($Purchased->download)) $Purchased->keygen(); $Purchased->save(); if ($Item->inventory) $Item->unstock(); } } function risk ($XML) { $id = $XML->getElementContent('google-order-number'); $Purchase = new Purchase($id,'transactionid'); $Purchase->ip = $XML->getElementContent('ip-address'); $Purchase->card = $XML->getElementContent('partial-cc-number'); $Purchase->save(); } function state ($XML) { $id = $XML->getElementContent('google-order-number'); $state = $XML->getElementContent('new-financial-order-state'); $Purchase = new Purchase($id,'transactionid'); $Purchase->transtatus = $state; $Purchase->save(); if (strtoupper($state) == "CHARGEABLE" && $this->settings['autocharge'] == "on") { $_ = array(''."\n"); $_[] = '
Enter your Google Checkout merchant ID.
Enter your Google Checkout merchant key.
Copy this URL to your Google Checkout integration settings API callback URL.
Select the preferred size and style of the Google Checkout button.