Subversion Repositories munaweb

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
<?php
2
ini_set('include_path', ini_get('include_path') . ':.:../ebay/php');
3
require_once ('keys.php');
4
require_once ('eBaySession.php');
5
 
6
// Generates an indexed URL snippet from the array of item filters
7
function buildURLArray($filterarray)
8
{
9
    $urlfilter = '';
10
    $i = '0';
11
 
12
    // Iterate through each filter in the array
13
 
14
    foreach($filterarray as $itemfilter) {
15
 
16
        // Iterate through each key in the filter
17
 
18
        foreach($itemfilter as $key => $value) {
19
            if (is_array($value)) {
20
                foreach($value as $j => $content) { // Index the key for each value
21
                    $urlfilter.= "&itemFilter($i).$key($j)=$content";
22
                }
23
            }
24
            else {
25
                if ($value != "") {
26
                    $urlfilter.= "&itemFilter($i).$key=$value";
27
                }
28
            }
29
        }
30
 
31
        $i++;
32
    }
33
 
34
    return "$urlfilter";
35
} // End of buildURLArray function
36
 
37
function eBayProductAdjust($query, $quantitySold)
38
{
39
    global $seller;
40
    global $findUrl;
41
    global $findVersion;
42
    global $app;
43
    $result = '';
44
    $resultFlag = true;
45
 
46
    // API request variables
47
    $safequery = urlencode($query); // Make the query URL-friendly
48
 
49
    // Create a PHP array of the item filters you want to use in your request
50
    $filterarray = array(
51
        array(
52
            'name' => 'Seller',
53
            'value' => $seller
54
        ) ,
55
    );
56
 
57
    // Build the indexed item filter URL snippet
58
    $urlfilter = buildURLArray($filterarray);
59
 
60
    // Construct the findItemsByKeywords HTTP GET call
61
    $apicall = "$findUrl";
62
    $apicall.= "?OPERATION-NAME=findItemsByKeywords";
63
    $apicall.= "&SERVICE-VERSION=$findVersion";
64
    $apicall.= "&SECURITY-APPNAME=$app";
65
    $apicall.= "&GLOBAL-ID=EBAY-US";
66
    $apicall.= "&keywords=$safequery";
67
    $apicall.= "&paginationInput.entriesPerPage=1";
68
    $apicall.= "$urlfilter";
69
 
70
    // Load the call and capture the document returned by eBay API
71
    $resp = simplexml_load_file($apicall);
72
 
73
    // Check to see if the request was successful, else print an error
74
    if ($resp->ack == "Success") {
75
        $results = '';
76
        if ($resp->paginationOutput->totalEntries == 1) {
77
            $quantityAvailable = intval(getSingleItemResults($resp->searchResult->item->itemId));
78
            $quantityRemaining = $quantityAvailable - $quantitySold;
79
            if ($quantityRemaining > 0) {
80
                $result = reviseItem($resp->searchResult->item->itemId, $quantityRemaining);
81
            }
82
            else {
83
                $result = endItem($resp->searchResult->item->itemId);
84
            }
85
        }
86
        else {
87
            $result = "Couldn't adjust, found " . $resp->paginationOutput->totalEntries . " results.";
88
        }
89
    }
90
    else {
91
        $result = "Couldn't adjust, findItemsByKeywords call failed.";
92
    }
93
 
94
    $resultFlag = !strpos($result, 'Couldn\'t');
95
    return array($resultFlag, $result);
96
}
97
 
98
// GetSingleItem call to determine available quantity
99
function getSingleItemResults($selectedItemID)
100
{
101
    global $shopUrl;
102
    global $shopVersion;
103
    global $app;
104
    global $compatibilityLevel;
105
 
106
    // Construct the GetSingleItem call
107
    $apicallb = "$shopUrl";
108
    $apicallb.= "?callname=GetSingleItem";
109
    $apicallb.= "&version=$shopVersion";
110
    $apicallb.= "&appid=$app";
111
    $apicallb.= "&itemid=$selectedItemID";
112
    $apicallb.= "&responseencoding=XML";
113
    $apicallb.= "&includeselector=Details";
114
 
115
    // Load the call and capture the document returned by eBay API
116
    $resp = simplexml_load_file($apicallb);
117
 
118
    // Check to see if the response was loaded
119
    if ($resp) {
120
        return ($resp->Item->Quantity - $resp->Item->QuantitySold);
121
    }
122
 
123
    return "0";
124
} // End of getSingleItemResults function
125
 
126
function reviseItem($id, $newQuantity)
127
{
128
    global $token;
129
    global $dev;
130
    global $app;
131
    global $cert;
132
    global $gatewayUrl;
133
    global $compatabilityLevel;
134
 
135
    // Build the request Xml string
136
    $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
137
    $requestXmlBody.= '<ReviseItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
138
    $requestXmlBody.= "<RequesterCredentials><eBayAuthToken>$token</eBayAuthToken></RequesterCredentials>";
139
    $requestXmlBody.= "<Item>";
140
    $requestXmlBody.= "<ItemID>$id</ItemID>";
141
    $requestXmlBody.= "<Quantity>$newQuantity</Quantity>";
142
    $requestXmlBody.= "</Item>";
143
    $requestXmlBody.= '</ReviseItemRequest>';
144
 
145
    // Create a new eBay session with all details pulled in from included keys.php
146
    $session = new eBaySession($token, $dev, $app, $cert, $gatewayUrl, $compatabilityLevel, 0, 'ReviseItem');
147
 
148
    // send the request and get response
149
    $responseXml = $session->sendHttpRequest($requestXmlBody);
150
    if (stristr($responseXml, 'HTTP 404') || $responseXml == '') return "Couldn't adjust quantity for Id $id, reviseItem call failed.";
151
 
152
    // Xml string is parsed and creates a DOM Document object
153
    $responseDoc = new DomDocument();
154
    $responseDoc->loadXML($responseXml);
155
 
156
    // get any error nodes
157
    $errors = $responseDoc->getElementsByTagName('Errors');
158
 
159
    // if there are error nodes
160
    if ($errors->length > 0)
161
    {
162
        $code = $errors->item(0)->getElementsByTagName('ErrorCode');
163
        $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
164
        error_log("reviseItem: " . $code->item(0)->nodeValue . " - " . $shortMsg->item(0)->nodeValue);
165
        return "Couldn't adjust quantity for Id $id, reviseItem call failed.";
166
    }
167
    else {
168
        return "Quantity for Id $id is now $newQuantity.";
169
    }
170
}
171
 
172
function endItem($id)
173
{
174
    global $token;
175
    global $dev;
176
    global $app;
177
    global $cert;
178
    global $gatewayUrl;
179
    global $compatabilityLevel;
180
 
181
    // Build the request Xml string
182
    $requestXmlBody = '<?xml version="1.0" encoding="utf-8" ?>';
183
    $requestXmlBody.= '<EndItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
184
    $requestXmlBody.= "<RequesterCredentials><eBayAuthToken>$token</eBayAuthToken></RequesterCredentials>";
185
    $requestXmlBody.= "<EndingReason>NotAvailable</EndingReason>";
186
    $requestXmlBody.= "<ItemID>$id</ItemID>";
187
    $requestXmlBody.= '</EndItemRequest>';
188
 
189
    // Create a new eBay session with all details pulled in from included keys.php
190
    $session = new eBaySession($token, $dev, $app, $cert, $gatewayUrl, $compatabilityLevel, 0, 'EndItem');
191
 
192
    // send the request and get response
193
    $responseXml = $session->sendHttpRequest($requestXmlBody);
194
    if (stristr($responseXml, 'HTTP 404') || $responseXml == '') return "Couldn't end Id $id, endItem call failed.";
195
 
196
    // Xml string is parsed and creates a DOM Document object
197
    $responseDoc = new DomDocument();
198
    $responseDoc->loadXML($responseXml);
199
 
200
    // get any error nodes
201
    $errors = $responseDoc->getElementsByTagName('Errors');
202
 
203
    // if there are error nodes
204
    if ($errors->length > 0)
205
    {
206
        $code = $errors->item(0)->getElementsByTagName('ErrorCode');
207
        if ($code == "1047") { // auction already closed
208
        	$response2 = addNote($id);
209
            return "Id $id already ended. $response2";
210
        } else {
211
            $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
212
            error_log("endItem: " . $code->item(0)->nodeValue . " - " . $shortMsg->item(0)->nodeValue);
213
            return "Couldn't end Id $id, endItem call failed.";
214
        }
215
    }
216
    else { //no errors
217
    	$response2 = addNote($id);
218
        return "Id $id ended. $response2";
219
    }
220
}
221
 
222
 
223
function addNote($id)
224
{
225
    global $token;
226
    global $dev;
227
    global $app;
228
    global $cert;
229
    global $gatewayUrl;
230
    global $compatabilityLevel;
231
 
232
    // Build the request Xml string
233
    $requestXmlBody = '<?xml version="1.0" encoding="utf-8"?>';
234
    $requestXmlBody .= '<SetUserNotesRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
235
    $requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$token</eBayAuthToken></RequesterCredentials>";
236
    $requestXmlBody .= "<ItemID>$id</ItemID>";
237
    $requestXmlBody .= "<Action>AddOrUpdate</Action>";
238
    $requestXmlBody .= "<NoteText>Sold via Shopify on " . date('Y-m-d') . ".</NoteText>";
239
    $requestXmlBody .= "</SetUserNotesRequest>";
240
 
241
    // Create a new eBay session with all details pulled in from included keys.php
242
    $session = new eBaySession($token, $dev, $app, $cert, $gatewayUrl, $compatabilityLevel, 0, 'SetUserNotes');
243
 
244
    // send the request and get response
245
    $responseXml = $session->sendHttpRequest($requestXmlBody);
246
    if (stristr($responseXml, 'HTTP 404') || $responseXml == '') return "Couldn't add note for Id $id, setUserNotes call failed.";
247
 
248
    // Xml string is parsed and creates a DOM Document object
249
    $responseDoc = new DomDocument();
250
    $responseDoc->loadXML($responseXml);
251
 
252
    // get any error nodes
253
    $errors = $responseDoc->getElementsByTagName('Errors');
254
 
255
    // if there are error nodes
256
    if ($errors->length > 0)
257
    {
258
        $code = $errors->item(0)->getElementsByTagName('ErrorCode');
259
        $shortMsg = $errors->item(0)->getElementsByTagName('ShortMessage');
260
        error_log("setUserNotes: " . $code->item(0)->nodeValue . " - " . $shortMsg->item(0)->nodeValue);
261
        return "Couldn't add note for Id $id, setUserNotes call failed.";
262
    }
263
    else { //no errors
264
        return "Note added for Id $id.";
265
    }
266
}
267
?>