source: branches/feature-module-paygent/data/downloads/module/mdl_paygent/jp/co/ks/merchanttool/connectmodule/entity/ReferenceResponseDataImpl.php @ 15162

Revision 15162, 10.0 KB checked in by naka, 17 years ago (diff)

ペイジェント決済モジュール

Line 
1<?php
2/**
3 * PAYGENT B2B MODULE
4 * ReferenceResponseDataImpl.php
5 *
6 * Copyright (C) 2007 by PAYGENT Co., Ltd.
7 * All rights reserved.
8 */
9
10include_once("jp/co/ks/merchanttool/connectmodule/exception/PaygentB2BModuleConnectException.php");
11include_once("jp/co/ks/merchanttool/connectmodule/exception/PaygentB2BModuleException.php");
12include_once("jp/co/ks/merchanttool/connectmodule/util/CSVWriter.php");
13include_once("jp/co/ks/merchanttool/connectmodule/util/CSVTokenizer.php");
14include_once("jp/co/ks/merchanttool/connectmodule/util/HttpsRequestSender.php");
15include_once("jp/co/ks/merchanttool/connectmodule/util/StringUtil.php");
16include_once("jp/co/ks/merchanttool/connectmodule/entity/ResponseData.php");
17
18/**
19 * Æ‰ïŒn‰ž“š“d•¶ˆ—ƒNƒ‰ƒX
20 *
21 * @version $Revision: 1.4 $
22 * @author $Author: t-mori $
23 */
24
25
26    /**
27     * s”ԍ†iƒwƒbƒ_[•”j= "1"
28     */
29    define("ReferenceResponseDataImpl__LINENO_HEADER", "1");
30
31    /**
32     * s”ԍ†iƒf[ƒ^ƒwƒbƒ_[•”j", "2"
33     */
34    define("ReferenceResponseDataImpl__LINENO_DATA_HEADER", "2");
35
36    /**
37     * s”ԍ†iƒf[ƒ^•”j", "3"
38     */
39    define("ReferenceResponseDataImpl__LINENO_DATA", "3");
40
41    /**
42     * s”ԍ†iƒgƒŒ[ƒ‰[•”j", "4"
43     */
44    define("ReferenceResponseDataImpl__LINENO_TRAILER", "4");
45
46    /**
47     * ƒŒƒR[ƒh‹æ•ª ˆÊ’u", 0
48     */
49    define("ReferenceResponseDataImpl__LINE_RECORD_DIVISION", 0);
50
51    /**
52     * ƒwƒbƒ_[•” ˆ—Œ‹‰Ê ˆÊ’u 1
53     */
54    define("ReferenceResponseDataImpl__LINE_HEADER_RESULT", 1);
55
56    /**
57     * ƒwƒbƒ_[•” ƒŒƒXƒ|ƒ“ƒXƒR[ƒh ˆÊ’u", 2
58     */
59    define("ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_CODE", 2);
60
61    /**
62     * ƒwƒbƒ_[•” ƒŒƒXƒ|ƒ“ƒXÚ× ˆÊ’u", 3
63     */
64    define("ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_DETAIL", 3);
65
66    /**
67     * ƒgƒŒ[ƒ‰[•” ƒf[ƒ^Œ” ˆÊ’u", 1
68     */
69    define("ReferenceResponseDataImpl__LINE_TRAILER_DATA_COUNT", 1);
70
71    /**
72     * ‰üs•¶Žš
73     */
74    define("ReferenceResponseDataImpl__LINE_SEPARATOR", "\r\n");
75
76class ReferenceResponseDataImpl extends ResponseData {
77    /** ˆ—Œ‹‰Ê */
78    var $resultStatus;
79
80    /** ƒŒƒXƒ|ƒ“ƒXƒR[ƒh */
81    var $responseCode;
82
83    /** ƒŒƒXƒ|ƒ“ƒXÚ× */
84    var $responseDetail;
85
86    /** ƒf[ƒ^ƒwƒbƒ_[ */
87    var $dataHeader;
88
89    /** ƒf[ƒ^ */
90    var $data;
91
92    /** Œ»Ý‚ÌIndex */
93    var $currentIndex;
94
95    /**
96     * ƒRƒ“ƒXƒgƒ‰ƒNƒ^
97     */
98    function ReferenceResponseDataImpl() {
99        $this->dataHeader = array();
100        $this->data = array();
101        $this->currentIndex = 0;
102    }
103
104    /**
105     * data ‚𕪉ð
106     *
107     * @param data
108     * @return mixed TRUE:¬Œ÷A‘¼FƒGƒ‰[ƒR[ƒh
109     */
110    function parse($body) {
111
112        $csvTknzr = new CSVTokenizer(CSVTokenizer__DEF_SEPARATOR,
113            CSVTokenizer__DEF_ITEM_ENVELOPE);
114
115        // •ÛŽƒf[ƒ^‚ð‰Šú‰»
116        $this->data = array();
117
118        // Œ»ÝˆÊ’u‚ð‰Šú‰»
119        $this->currentIndex = 0;
120       
121        // ƒŠƒUƒ‹ƒgî•ñ‚̏‰Šú‰»
122        $this->resultStatus = "";
123        $this->responseCode = "";
124        $this->responseDetail = "";
125
126        $lines = split(ReferenceResponseDataImpl__LINE_SEPARATOR, $body);
127        foreach($lines as $i => $line) {
128            $lineItem = $csvTknzr->parseCSVData($line);
129
130            if (0 < count($lineItem)) {
131                if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
132                        == ReferenceResponseDataImpl__LINENO_HEADER) {
133                    // ƒwƒbƒ_[•”‚̍s‚̏ꍇ
134                    if (ReferenceResponseDataImpl__LINE_HEADER_RESULT < count($lineItem)) {
135                        // ˆ—Œ‹‰Ê‚ðÝ’è
136                        $this->resultStatus = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESULT];
137                    }
138                    if (ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_CODE < count($lineItem)) {
139                        // ƒŒƒXƒ|ƒ“ƒXƒR[ƒh‚ðÝ’è
140                        $this->responseCode = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_CODE];
141                    }
142                    if (ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_DETAIL < count($lineItem)) {
143                        // ƒŒƒXƒ|ƒ“ƒXÚ×‚ðÝ’è
144                        $this->responseDetail = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_DETAIL];
145                    }
146                } else if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
147                        == ReferenceResponseDataImpl__LINENO_DATA_HEADER) {
148                    // ƒf[ƒ^ƒwƒbƒ_[•”‚̍s‚̏ꍇ
149                    $this->dataHeader = array();
150
151                    for ($i = 1; $i < count($lineItem); $i++) {
152                        // ƒf[ƒ^ƒwƒbƒ_[‚ðÝ’èiƒŒƒR[ƒh‹æ•ª‚͏œ‚­j
153                        $this->dataHeader[] = $lineItem[$i];
154                    }
155                } else if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
156                        == ReferenceResponseDataImpl__LINENO_DATA) {
157                    // ƒf[ƒ^•”‚̍s‚̏ꍇ
158                    // ƒf[ƒ^ƒwƒbƒ_[•”‚ªŠù‚É“WŠJÏ‚Ý‚Å‚ ‚鎖‚ð‘z’è
159                    $map = array();
160
161                    if (count($this->dataHeader) == (count($lineItem) - 1)) {
162                        // ƒf[ƒ^ƒwƒbƒ_[”‚ƁAƒf[ƒ^€–ڐ”iƒŒƒR[ƒh‹æ•ªœ‚­j‚͈ê’v
163                        for ($i = 1; $i < count($lineItem); $i++) {
164                            // ‘Ήž‚·‚éƒf[ƒ^ƒwƒbƒ_[‚ð Key ‚ɁAMap‚֐ݒè
165                            $map[$this->dataHeader[$i - 1]] = $lineItem[$i];
166                        }
167                    } else {
168                        // ƒf[ƒ^ƒwƒbƒ_[”‚ƁAƒf[ƒ^€–ڐ”‚ªˆê’v‚µ‚È‚¢ê‡
169                        $sb = PaygentB2BModuleException__OTHER_ERROR . ": ";
170                        $sb .= "Not Mutch DataHeaderCount=";
171                        $sb .= "" . count($this->dataHeader);
172                        $sb .= " DataItemCount:";
173                        $sb .= "" . (count($lineItem) - 1);
174                        trigger_error($sb, E_USER_WARNING);
175                        return PaygentB2BModuleException__OTHER_ERROR;
176                    }
177
178                    if (0 < count($map)) {
179                        // Map ‚ªÝ’肳‚ê‚Ä‚¢‚éê‡
180                        $this->data[] = $map;
181                    }
182                } else if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
183                        == ReferenceResponseDataImpl__LINENO_TRAILER) {
184                    // ƒgƒŒ[ƒ‰[•”‚̍s‚̏ꍇ
185                    if (ReferenceResponseDataImpl__LINE_TRAILER_DATA_COUNT < count($lineItem)) {
186                        // ƒf[ƒ^ƒTƒCƒY
187                    }
188                }
189            }
190        }
191
192        if (StringUtil::isEmpty($this->resultStatus)) {
193            // ˆ—Œ‹‰Ê‚ª ‹ó•¶Žš ‚à‚µ‚­‚Í null ‚̏ꍇ
194            trigger_error(PaygentB2BModuleConnectException__KS_CONNECT_ERROR
195                 . ": resultStatus is Nothing.", E_USER_WARNING);
196            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
197        }
198        return true;
199    }
200
201    /**
202     * data ‚𕪉𠃊ƒUƒ‹ƒgî•ñ‚̂݁A•Ï”‚ɐݒè
203     *
204     * @param body
205     * @return mixed TRUE:¬Œ÷A‘¼FƒGƒ‰[ƒR[ƒh
206     */
207    function parseResultOnly($body) {
208
209        $csvTknzr = new CSVTokenizer(CSVTokenizer__DEF_SEPARATOR,
210            CSVTokenizer__DEF_ITEM_ENVELOPE);
211        $line = "";
212
213        // •ÛŽƒf[ƒ^‚ð‰Šú‰»
214        $this->data = array();
215
216        // Œ»ÝˆÊ’u‚ð‰Šú‰»
217        $this->currentIndex = 0;
218       
219        // ƒŠƒUƒ‹ƒgî•ñ‚̏‰Šú‰»
220        $this->resultStatus = "";
221        $this->responseCode = "";
222        $this->responseDetail = "";
223
224        $lines = split(ReferenceResponseDataImpl__LINE_SEPARATOR, $body);
225        foreach($lines as $i => $line) {
226            $lineItem = $csvTknzr->parseCSVData($line);
227
228            if (0 < count($lineItem)) {
229                if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
230                        == ReferenceResponseDataImpl__LINENO_HEADER) {
231                    // ƒwƒbƒ_[•”‚̍s‚̏ꍇ
232                    if (ReferenceResponseDataImpl__LINE_HEADER_RESULT < count($lineItem)) {
233                        // ˆ—Œ‹‰Ê‚ðÝ’è
234                        $this->resultStatus = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESULT];
235                    }
236                    if (ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_CODE < count($lineItem)) {
237                        // ƒŒƒXƒ|ƒ“ƒXƒR[ƒh‚ðÝ’è
238                        $this->responseCode = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_CODE];
239                    }
240                    if (ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_DETAIL < count($lineItem)) {
241                        // ƒŒƒXƒ|ƒ“ƒXÚ×‚ðÝ’è
242                        $this->responseDetail = $lineItem[ReferenceResponseDataImpl__LINE_HEADER_RESPONSE_DETAIL];
243                    }
244                }
245            }
246        }
247       
248        if (StringUtil::isEmpty($this->resultStatus)) {
249            // ˆ—Œ‹‰Ê‚ª ‹ó•¶Žš ‚à‚µ‚­‚Í null ‚̏ꍇ
250            trigger_error(PaygentB2BModuleConnectException__KS_CONNECT_ERROR
251                . ": resultStatus is Nothing.", E_USER_WARNING);
252            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
253        }
254       
255        return true;
256    }
257
258    /**
259     * ŽŸ‚̃f[ƒ^‚ðŽæ“¾
260     *
261     * @return Map
262     */
263    function resNext() {
264        $map = null;
265
266        if ($this->hasResNext()) {
267
268            $map = $this->data[$this->currentIndex];
269
270            $this->currentIndex++;
271        }
272
273        return $map;
274    }
275
276    /**
277     * ŽŸ‚̃f[ƒ^‚ª‘¶Ý‚·‚é‚©”»’è
278     *
279     * @return boolean true=‘¶Ý‚·‚é false=‘¶Ý‚µ‚È‚¢
280     */
281    function hasResNext() {
282        $rb = false;
283
284        if ($this->currentIndex < count($this->data)) {
285            $rb = true;
286        }
287
288        return $rb;
289    }
290
291    /**
292     * resultStatus ‚ðŽæ“¾
293     *
294     * @return String
295     */
296    function getResultStatus() {
297        return $this->resultStatus;
298    }
299
300    /**
301     * responseCode ‚ðŽæ“¾
302     *
303     * @return String
304     */
305    function getResponseCode() {
306        return $this->responseCode;
307    }
308
309    /**
310     * responseDetail ‚ðŽæ“¾
311     *
312     * @return String
313     */
314    function getResponseDetail() {
315        return $this->responseDetail;
316    }
317
318    /**
319     * ƒf[ƒ^Œ”‚ðŽæ“¾
320     *
321     * @param data InputStream
322     * @return int -1:ƒGƒ‰[
323     */
324    function getDataCount($body) {
325        $ri = 0;
326        $strCnt = null;
327       
328        $csvTknzr = new CSVTokenizer(CSVTokenizer__DEF_SEPARATOR,
329            CSVTokenizer__DEF_ITEM_ENVELOPE);
330        $line = "";
331
332        $lines = split(ReferenceResponseDataImpl__LINE_SEPARATOR, $body);
333        foreach($lines as $i => $line) {
334            $lineItem = $csvTknzr->parseCSVData($line);
335
336            if (0 < count($lineItem)) {
337                if ($lineItem[ReferenceResponseDataImpl__LINE_RECORD_DIVISION]
338                        == ReferenceResponseDataImpl__LINENO_TRAILER) {
339                    // ƒgƒŒ[ƒ‰[•”‚̍s‚̏ꍇ
340                    if (ReferenceResponseDataImpl__LINE_TRAILER_DATA_COUNT < count($lineItem)) {
341                        // ƒf[ƒ^Œ”‚ðŽæ“¾ while‚©‚甲‚¯‚é
342                        if (StringUtil::isNumeric($lineItem[ReferenceResponseDataImpl__LINE_TRAILER_DATA_COUNT])) {
343                            $strCnt = $lineItem[ReferenceResponseDataImpl__LINE_TRAILER_DATA_COUNT];
344                        }
345                        break;
346                    }
347                }
348            }
349        }
350
351        if ($strCnt != null && StringUtil::isNumeric($strCnt)) {
352            $ri = intval($strCnt);
353        } else {
354            return PaygentB2BModuleException__OTHER_ERROR;      //ƒGƒ‰[
355        }
356
357        return $ri;
358    }
359
360    /**
361     * CSV ‚ðì¬
362     *
363     * @param resBody
364     * @param resultCsv String
365     * @return boolean trueF¬Œ÷A‘¼FƒGƒ‰[ƒR[ƒh
366     */
367    function writeCSV($body, $resultCsv) {
368        $rb = false;
369
370        // CSV ‚ð 1s‚¸‚o—Í
371        $csvWriter = new CSVWriter($resultCsv);
372        if ($csvWriter->open() === false) {
373            // ƒtƒ@ƒCƒ‹ƒI[ƒuƒ“ƒGƒ‰[
374            trigger_error(PaygentB2BModuleException__CSV_OUTPUT_ERROR
375                . ": Failed to open CSV file.", E_USER_WARNING);
376            return PaygentB2BModuleException__CSV_OUTPUT_ERROR;
377        }
378
379        $lines = split(ReferenceResponseDataImpl__LINE_SEPARATOR, $body);
380        foreach($lines as $i => $line) {
381            if (!$csvWriter->writeOneLine($line)) {
382                // ‘‚«ž‚ß‚È‚©‚Á‚½ê‡
383                trigger_error(PaygentB2BModuleException__CSV_OUTPUT_ERROR
384                    . ": Failed to write to CSV file.", E_USER_WARNING);
385                return PaygentB2BModuleException__CSV_OUTPUT_ERROR;
386            }
387        }
388
389        $csvWriter->close();
390
391        $rb = true;
392
393        return $rb;
394    }
395
396}
397
398?>
Note: See TracBrowser for help on using the repository browser.