Subversion Repositories munaweb

Rev

Rev 103 | Rev 157 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*  © 2013 eBay Inc., All Rights Reserved */
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
?>
<?php

$DEBUG = false;
$capturePath = "captures/";

// be sure include path contains current and shopify directory
ini_set('include_path', ini_get('include_path') . ':.:../php:../../shopify/php');

// Load general helper classes for eBay SOAP API
require_once 'keys.php';
require_once 'eBaySOAP.php';
require_once 'Shopify.php';

function debug_string_backtrace() {
    ob_start();
    debug_print_backtrace();
    $trace = ob_get_contents();
    ob_end_clean();

    // Remove first item from backtrace as it's this function which is redundant.
    $trace = preg_replace ('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1);

    // Renumber backtrace items.
    $trace = preg_replace ('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace);

    return $trace;
}

class eBayPlatformNotificationListener extends eBayPlatformNotifications {
        protected $NotificationSignature;

        // Dispatch method to ensure signature validation
        public function __call($method, $args) {
                $s = "Called with $method";
                $this->carp($s);

                // Print backtrace and arguments
                $this->carp(debug_string_backtrace());
                $this->carp("args=" . print_r($args, true));

                if ($this->ValidateSignature($args[0])) {
                        // strip off trailing "Request"
                        $method = substr($method, 0, -8);
                        if (method_exists($this, $method)) {
                                return call_user_func_array(array($this, $method), $args);
                        } else {
                                $this->errorLog("Unhandled Event: args=" . print_r($args, true));
                        }
                }

                // Today is a good day to die.
                die("Death");
        }

        // Extract Signature for validation later
        // Can't validate here because we don't have access to the Timestamp
        public function RequesterCredentials($RequesterCredentials) {
                $this->NotificationSignature = $RequesterCredentials->NotificationSignature;
        }

        protected function ValidateSignature($Timestamp) {
                // Check for Signature Match
                $CalculatedSignature = $this->CalculateSignature($Timestamp);
                $NotificationSignature = $this->NotificationSignature;

                if ($CalculatedSignature != $NotificationSignature) {
//bugbug                        $this->errorLog("Sig Mismatch: Calc: $CalculatedSignature, Note: $NotificationSignature");
//bugbug                        return false;
                } else {
                        $this->carp("Sig Match: $NotificationSignature");
                }

                // Check that Timestamp is within 10 minutes of now
                $tz = date_default_timezone_get();
                date_default_timezone_set('UTC');
                $then = strtotime($Timestamp);
                $now = time();
                date_default_timezone_set($tz);

                $drift = $now - $then;
                $ten_minutes = 60 * 10;
                if ($drift > $ten_minutes) {
                        $this->errorLog("Time Drift is too large: $drift seconds, Note: $NotificationSignature");
                        return false;
                } else {
                        $this->carp("Time Drift is okay: $drift seconds");
                }

                return true;
        }

        // Arg order is brittle, assumes constant return ordering from eBay
        public function GetMemberMessages($Timestamp, $Ack, $CorrelationID,
                                                $Version, $Build, $NotificationEventName,
                                                $RecipientUserID, $SellerEIASToken, $MemberMessage,
                                                $PaginationResult, $HasMoreItems) {
                $ItemID = $MemberMessage->MemberMessageExchange->Item->ItemID;
                $Seller = $MemberMessage->MemberMessageExchange->Item->Seller->UserID;
                $Sender = $MemberMessage->MemberMessageExchange->Question->SenderID;
                $this->notificationLog("$NotificationEventName: From $Sender to $Seller regarding Item $ItemID");

                return "";
        }

        public function GetItem($Timestamp, $Ack, $CorrelationID,
                                $Version, $Build, $NotificationEventName,
                                $RecipientUserID, $BuyerEIASToken, $Item) {

               $ItemID = $Item->ItemID;
           $Title = $Item->Title;

               $UPC = (isset($Item->ProductListingDetails->UPC) ? $Item->ProductListingDetails->UPC : "");
               $SKU = (isset($Item->SKU) ? $Item->SKU : "");
           $this->notificationLog("$NotificationEventName: ItemId=$ItemID; Title=$Title; SKU=$SKU; UPC=$UPC");

                   if ($NotificationEventName == "BidReceived") {
                                $list = shopifyProductAdjust($Title, $SKU, 1);
                $this->notificationLog($list[1]);
                                if ($list[0] == false) {
                  $this->errorLog($list[1]);
                                }
                   }

               return "";
        }

        public function GetItemTransactions($Timestamp, $Ack, $CorrelationID,
                                $Version, $Build, $NotificationEventName,
                                $SellerName, $BuyerEIASToken, $PaginationResult, $HasMoreItems,
                                $x1, $x2, $x3, $Item, $Transaction) {

               $ItemID = $Item->ItemID;
           $Title = $Item->Title;
               $SKU = (isset($Item->SKU) ? $Item->SKU : "");
               $Quantity = (isset($Transaction->Transaction->QuantityPurchased) ? $Transaction->Transaction->QuantityPurchased : "1");
           $this->notificationLog("$NotificationEventName: ItemId=$ItemID; Title=$Title; SKU=$SKU; Quantity=$Quantity");

                   if ($NotificationEventName == "AuctionCheckoutComplete") {
                                $list = shopifyProductAdjust($Title, $SKU, 1);
                $this->notificationLog($list[1]);
                                if ($list[0] == false) {
                  $this->errorLog($list[1]);
                                }
                   }

           return "";
        }

        public function GetFeedback($Timestamp, $Ack, $CorrelationID,
                                $Version, $Build, $NotificationEventName,
                                $SellerName, $BuyerEIASToken, $Feedback,
                                $x1, $BuyerScore, $PaginationResult, $x2, $x3) {

               $ItemID = $Feedback->FeedbackDetail->ItemID;
           $Type = $Feedback->FeedbackDetail->CommentType;
               $Role = $Feedback->FeedbackDetail->Role;
               $Text = $Feedback->FeedbackDetail->CommentText;
           $this->notificationLog("$Type $Role Feedback for Item $ItemID=$Text");

           return "";
        }

        public function GetMyMessages($Timestamp, $Ack, $CorrelationID,
                                $Version, $Build, $NotificationEventName,
                                $SellerName, $SenderEIASToken, $Message) {

               $Sender = $Message->Message->Sender;
           $Recipient = $Message->Message->RecipientUserID;
           $Subject = $Message->Message->Subject;
           $this->notificationLog("Message from $Sender to $Recipient regarding $Subject");

           return "";
        }

        public function GetBestOffers($Timestamp, $Ack, $CorrelationID,
                                $Version, $Build, $NotificationEventName,
                                $SellerName, $SenderEIASToken, $BestOffer, $BuyItNow) {

               $Sender = $BestOffer->BestOffer->Buyer->UserID;
           $BestOfferAmount = $BestOffer->BestOffer->Price;
           $Title = $BuyItNow->Title;
           $OriginalPrice = $BuyItNow->BuyItNowPrice;
               $ItemID = $BuyItNow->ItemID;

           $this->notificationLog("$NotificationEventName: $Sender offered $BestOfferAmount (originally $OriginalPrice) for $ItemID ($Title)");

           return "";
        }

}


// Create and configure session
$session = new eBaySession($dev, $app, $cert);

if ($DEBUG) {
        error_log(serialize(apache_request_headers()));
}

$stdin = $GLOBALS['HTTP_RAW_POST_DATA'];
if ($DEBUG) {
        error_log($stdin);
}

$action = basename(str_replace('"', '', $_SERVER['HTTP_SOAPACTION']));
file_put_contents($capturePath . $action . '.xml', $stdin . "\n\n", FILE_APPEND);
if ($DEBUG) {
        error_log('EVENT NAME: ' . $action);
}

$server = new SOAPServer(null, array('uri'=>'urn:ebay:apis:eBLBaseComponents'));
$server->setClass('eBayPlatformNotificationListener', $session, $DEBUG);
$server->handle();

?>