Subversion Repositories munaweb

Rev

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

Rev Author Line No. Line
2 - 1
<?php
157 - 2
/*  (C) 2013 eBay Inc., All Rights Reserved */
2 - 3
/* Licensed under CDDL 1.0 -  http://opensource.org/licenses/cddl1.php */
4
?>
5
<?php
12 - 6
$DEBUG = false;
2 - 7
$capturePath = "captures/";
8
 
9
// be sure include path contains current and shopify directory
75 - 10
ini_set('include_path', ini_get('include_path') . ':.:../php:../../shopify/php');
2 - 11
 
12
// Load general helper classes for eBay SOAP API
13
require_once 'keys.php';
14
require_once 'eBaySOAP.php';
15
require_once 'Shopify.php';
16
 
17
function debug_string_backtrace() {
18
    ob_start();
19
    debug_print_backtrace();
20
    $trace = ob_get_contents();
21
    ob_end_clean();
22
 
23
    // Remove first item from backtrace as it's this function which is redundant.
157 - 24
    $trace = preg_replace('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1);
2 - 25
 
26
    // Renumber backtrace items.
160 - 27
    $trace = preg_replace_callback('/^#(\d+)/m', '\'#\' . ($1 - 1)', $trace);
2 - 28
 
29
    return $trace;
30
}
31
 
32
class eBayPlatformNotificationListener extends eBayPlatformNotifications {
157 - 33
    protected $NotificationSignature;
2 - 34
 
157 - 35
    // Dispatch method to ensure signature validation
36
    public function __call($method, $args) {
37
        $s = "Called with $method";
38
        $this->carp($s);
2 - 39
 
157 - 40
        // Print backtrace and arguments
41
        $this->carp(debug_string_backtrace());
42
        $this->carp("args=" . print_r($args, true));
2 - 43
 
157 - 44
        if ($this->ValidateSignature($args[0])) {
45
            // strip off trailing "Request"
46
            $method = substr($method, 0, -8);
47
            if (method_exists($this, $method)) {
48
                return call_user_func_array(array(
49
                    $this,
50
                    $method
51
                ) , $args);
52
            }
53
            else {
54
                $this->errorLog("Unhandled Event: args=" . print_r($args, true));
55
            }
56
        }
2 - 57
 
157 - 58
        // Today is a good day to die.
59
        die("Death");
60
    }
2 - 61
 
157 - 62
    // Extract Signature for validation later
63
    // Can't validate here because we don't have access to the Timestamp
64
    public function RequesterCredentials($RequesterCredentials) {
65
        $this->NotificationSignature = $RequesterCredentials->NotificationSignature;
66
    }
2 - 67
 
157 - 68
    protected function ValidateSignature($Timestamp) {
69
        // Check for Signature Match
70
        $CalculatedSignature = $this->CalculateSignature($Timestamp);
71
        $NotificationSignature = $this->NotificationSignature;
2 - 72
 
157 - 73
        if ($CalculatedSignature != $NotificationSignature) {
74
            //bugbug                        $this->errorLog("Sig Mismatch: Calc: $CalculatedSignature, Note: $NotificationSignature");
75
            //bugbug                        return false;
76
 
77
        }
78
        else {
79
            $this->carp("Sig Match: $NotificationSignature");
80
        }
2 - 81
 
157 - 82
        // Check that Timestamp is within 10 minutes of now
83
        $tz = date_default_timezone_get();
84
        date_default_timezone_set('UTC');
85
        $then = strtotime($Timestamp);
86
        $now = time();
87
        date_default_timezone_set($tz);
2 - 88
 
157 - 89
        $drift = $now - $then;
90
        $ten_minutes = 60 * 10;
91
        if ($drift > $ten_minutes) {
92
            $this->errorLog("Time Drift is too large: $drift seconds, Note: $NotificationSignature");
93
            return false;
94
        }
95
        else {
96
            $this->carp("Time Drift is okay: $drift seconds");
97
        }
2 - 98
 
157 - 99
        return true;
100
    }
2 - 101
 
157 - 102
    // Arg order is brittle, assumes constant return ordering from eBay
103
    public function GetMemberMessages($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $RecipientUserID, $SellerEIASToken, $MemberMessage, $PaginationResult, $HasMoreItems) {
104
        $ItemID = $MemberMessage
105
            ->MemberMessageExchange
106
            ->Item->ItemID;
107
        $Seller = $MemberMessage
108
            ->MemberMessageExchange
109
            ->Item
110
            ->Seller->UserID;
111
        $Sender = $MemberMessage
112
            ->MemberMessageExchange
113
            ->Question->SenderID;
114
        $this->notificationLog("$NotificationEventName: From $Sender to $Seller regarding Item $ItemID");
2 - 115
 
157 - 116
        return "";
117
    }
2 - 118
 
157 - 119
    public function GetItem($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $RecipientUserID, $BuyerEIASToken, $Item) {
2 - 120
 
157 - 121
        $ItemID = $Item->ItemID;
122
        $Title = $Item->Title;
2 - 123
 
157 - 124
        $UPC = (isset($Item
125
            ->ProductListingDetails
126
            ->UPC) ? $Item
127
            ->ProductListingDetails->UPC : "");
128
        $SKU = (isset($Item->SKU) ? $Item->SKU : "");
129
        $this->notificationLog("$NotificationEventName: ItemId=$ItemID; Title=$Title; SKU=$SKU; UPC=$UPC");
2 - 130
 
157 - 131
        if ($NotificationEventName == "BidReceived") {
132
            $list = shopifyProductAdjust($Title, $SKU, 1);
133
            $this->notificationLog($list[1]);
134
            if ($list[0] == false) {
135
                $this->errorLog($list[1]);
136
            }
137
        }
2 - 138
 
157 - 139
        return "";
140
    }
2 - 141
 
157 - 142
    public function GetItemTransactions($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $SellerName, $BuyerEIASToken, $PaginationResult, $HasMoreItems, $x1, $x2, $x3, $Item, $Transaction) {
2 - 143
 
157 - 144
        $ItemID = $Item->ItemID;
145
        $Title = $Item->Title;
146
        $SKU = (isset($Item->SKU) ? $Item->SKU : "");
147
        $Quantity = (isset($Transaction
148
            ->Transaction
149
            ->QuantityPurchased) ? $Transaction
150
            ->Transaction->QuantityPurchased : "1");
151
        $this->notificationLog("$NotificationEventName: ItemId=$ItemID; Title=$Title; SKU=$SKU; Quantity=$Quantity");
2 - 152
 
157 - 153
        //if ($NotificationEventName == "AuctionCheckoutComplete") {
154
        if ($NotificationEventName == "FixedPriceTransaction") {
155
            $list = shopifyProductAdjust($Title, $SKU, $Quantity);
156
            $this->notificationLog($list[1]);
157
            if ($list[0] == false) {
158
                $this->errorLog($list[1]);
159
            }
160
        }
2 - 161
 
157 - 162
        return "";
163
    }
2 - 164
 
157 - 165
    public function GetFeedback($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $SellerName, $BuyerEIASToken, $Feedback, $x1, $BuyerScore, $PaginationResult, $x2, $x3) {
2 - 166
 
157 - 167
        $ItemID = $Feedback
168
            ->FeedbackDetail->ItemID;
169
        $Type = $Feedback
170
            ->FeedbackDetail->CommentType;
171
        $Role = $Feedback
172
            ->FeedbackDetail->Role;
173
        $Text = $Feedback
174
            ->FeedbackDetail->CommentText;
175
        $this->notificationLog("$Type $Role Feedback for Item $ItemID=$Text");
2 - 176
 
157 - 177
        return "";
178
    }
2 - 179
 
157 - 180
    public function GetMyMessages($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $SellerName, $SenderEIASToken, $Message) {
2 - 181
 
157 - 182
        $Sender = $Message
183
            ->Message->Sender;
184
        $Recipient = $Message
185
            ->Message->RecipientUserID;
186
        $Subject = $Message
187
            ->Message->Subject;
188
        $this->notificationLog("Message from $Sender to $Recipient regarding $Subject");
2 - 189
 
157 - 190
        return "";
191
    }
2 - 192
 
157 - 193
    public function GetBestOffers($Timestamp, $Ack, $CorrelationID, $Version, $Build, $NotificationEventName, $SellerName, $SenderEIASToken, $BestOffer, $BuyItNow) {
2 - 194
 
157 - 195
        $Sender = $BestOffer
196
            ->BestOffer
197
            ->Buyer->UserID;
198
        $BestOfferAmount = $BestOffer
199
            ->BestOffer->Price;
200
        $Title = $BuyItNow->Title;
201
        $OriginalPrice = $BuyItNow->BuyItNowPrice;
202
        $ItemID = $BuyItNow->ItemID;
2 - 203
 
157 - 204
        $this->notificationLog("$NotificationEventName: $Sender offered $BestOfferAmount (originally $OriginalPrice) for $ItemID ($Title)");
2 - 205
 
157 - 206
        return "";
207
    }
2 - 208
 
209
}
210
 
211
// Create and configure session
212
$session = new eBaySession($dev, $app, $cert);
213
 
214
if ($DEBUG) {
157 - 215
    error_log(serialize(apache_request_headers()));
2 - 216
}
217
 
218
$stdin = $GLOBALS['HTTP_RAW_POST_DATA'];
219
if ($DEBUG) {
157 - 220
    error_log($stdin);
2 - 221
}
222
 
223
$action = basename(str_replace('"', '', $_SERVER['HTTP_SOAPACTION']));
224
file_put_contents($capturePath . $action . '.xml', $stdin . "\n\n", FILE_APPEND);
225
if ($DEBUG) {
157 - 226
    error_log('EVENT NAME: ' . $action);
2 - 227
}
228
 
157 - 229
$server = new SOAPServer(null, array(
230
    'uri' => 'urn:ebay:apis:eBLBaseComponents'
231
));
2 - 232
$server->setClass('eBayPlatformNotificationListener', $session, $DEBUG);
233
$server->handle();
234
 
235
?>