source: branches/version-2_13-dev/data/class/pages/shopping/LC_Page_Shopping_Payment.php @ 23027

Revision 23027, 18.0 KB checked in by yomoro, 11 years ago (diff)

#1506 決済処理中でストップした際に在庫とポイントが差し引かれてしまう 対応。一旦こちらの形でコミットさせていただきます。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
25
26/**
27 * 支払い方法選択 のページクラス.
28 *
29 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
33class LC_Page_Shopping_Payment extends LC_Page_Ex
34{
35    /** フォームパラメーターの配列 */
36    var $objFormParam;
37
38    /** 会員情報のインスタンス */
39    var $objCustomer;
40
41    /**
42     * Page を初期化する.
43     *
44     * @return void
45     */
46    function init()
47    {
48        parent::init();
49        $this->tpl_onload = 'fnCheckInputPoint();';
50        $this->tpl_title = 'お支払方法・お届け時間等の指定';
51        $masterData = new SC_DB_MasterData_Ex();
52        $this->arrPref = $masterData->getMasterData('mtb_pref');
53    }
54
55    /**
56     * Page のプロセス.
57     *
58     * @return void
59     */
60    function process()
61    {
62        parent::process();
63        $this->action();
64        $this->sendResponse();
65    }
66
67    /**
68     * Page のアクション.
69     *
70     * @return void
71     */
72    function action()
73    {
74        //決済処理中ステータスのロールバック
75        SC_Helper_Purchase_Ex::checkSessionPendingOrder();
76        SC_Helper_Purchase_Ex::checkDbMyPendignOrder();
77        SC_Helper_Purchase_Ex::checkDbAllPendingOrder();
78       
79        $objSiteSess = new SC_SiteSession_Ex();
80        $objCartSess = new SC_CartSession_Ex();
81        $objPurchase = new SC_Helper_Purchase_Ex();
82        $objCustomer = new SC_Customer_Ex();
83        $objFormParam = new SC_FormParam_Ex();
84        $objDelivery = new SC_Helper_Delivery_Ex();
85
86        $this->is_multiple = $objPurchase->isMultiple();
87
88        // カートの情報を取得
89        $this->arrShipping = $objPurchase->getShippingTemp($this->is_multiple);
90
91        $this->tpl_uniqid = $objSiteSess->getUniqId();
92        $cart_key = $objCartSess->getKey();
93        $this->cartKey = $cart_key;
94        $objPurchase->verifyChangeCart($this->tpl_uniqid, $objCartSess);
95
96        // 配送業者を取得
97        $this->arrDeliv = $objDelivery->getList($cart_key);
98        $this->is_single_deliv = $this->isSingleDeliv($this->arrDeliv);
99
100        // 会員情報の取得
101        if ($objCustomer->isLoginSuccess(true)) {
102            $this->tpl_login = '1';
103            $this->tpl_user_point = $objCustomer->getValue('point');
104            $this->name01 = $objCustomer->getValue('name01');
105            $this->name02 = $objCustomer->getValue('name02');
106        }
107
108        // 戻り URL の設定
109        // @deprecated 2.12.0 テンプレート直書きに戻した
110        $this->tpl_back_url = '?mode=return';
111
112        $arrOrderTemp = $objPurchase->getOrderTemp($this->tpl_uniqid);
113        // 正常に受注情報が格納されていない場合はカート画面へ戻す
114        if (SC_Utils_Ex::isBlank($arrOrderTemp)) {
115            SC_Response_Ex::sendRedirect(CART_URLPATH);
116            SC_Response_Ex::actionExit();
117        }
118
119        // カート内商品の妥当性チェック
120        $this->tpl_message = $objCartSess->checkProducts($cart_key);
121        if (strlen($this->tpl_message) >= 1) {
122            SC_Response_Ex::sendRedirect(CART_URLPATH);
123            SC_Response_Ex::actionExit();
124        }
125
126        /*
127         * 購入金額の取得
128         * ここでは送料を加算しない
129         */
130        $this->arrPrices = $objCartSess->calculate($cart_key, $objCustomer);
131
132        // お届け日一覧の取得
133        $this->arrDelivDate = $objPurchase->getDelivDate($objCartSess, $cart_key);
134
135        switch ($this->getMode()) {
136            /*
137             * 配送業者選択時のアクション
138             * モバイル端末以外の場合は, JSON 形式のデータを出力し, ajax で取得する.
139             */
140            case 'select_deliv':
141                $this->setFormParams($objFormParam, $arrOrderTemp, true, $this->arrShipping);
142                $objFormParam->setParam($_POST);
143                $this->arrErr = $objFormParam->checkError();
144                if (SC_Utils_Ex::isBlank($this->arrErr)) {
145                    $deliv_id = $objFormParam->getValue('deliv_id');
146                    $arrSelectedDeliv = $this->getSelectedDeliv($objCartSess, $deliv_id);
147                    $arrSelectedDeliv['error'] = false;
148                } else {
149                    $arrSelectedDeliv = array('error' => true);
150                    $this->tpl_mainpage = 'shopping/select_deliv.tpl'; // モバイル用
151                }
152
153                if (SC_Display_Ex::detectDevice() != DEVICE_TYPE_MOBILE) {
154                    echo SC_Utils_Ex::jsonEncode($arrSelectedDeliv);
155                    SC_Response_Ex::actionExit();
156                } else {
157                    $this->arrPayment = $arrSelectedDeliv['arrPayment'];
158                    $this->arrDelivTime = $arrSelectedDeliv['arrDelivTime'];
159                }
160                break;
161
162            // 登録処理
163            case 'confirm':
164                // パラメーター情報の初期化
165                $this->setFormParams($objFormParam, $_POST, false, $this->arrShipping);
166
167                $deliv_id = $objFormParam->getValue('deliv_id');
168                $arrSelectedDeliv = $this->getSelectedDeliv($objCartSess, $deliv_id);
169                $this->arrPayment = $arrSelectedDeliv['arrPayment'];
170                $this->arrDelivTime = $arrSelectedDeliv['arrDelivTime'];
171                $this->img_show = $arrSelectedDeliv['img_show'];
172
173                $this->arrErr = $this->lfCheckError($objFormParam, $this->arrPrices['subtotal'], $this->tpl_user_point);
174
175                if (empty($this->arrErr)) {
176                    $this->saveShippings($objFormParam, $this->arrDelivTime);
177                    $this->lfRegistData($this->tpl_uniqid, $objFormParam->getDbArray(), $objPurchase, $this->arrPayment);
178
179                    // 正常に登録されたことを記録しておく
180                    $objSiteSess->setRegistFlag();
181
182                    // 確認ページへ移動
183                    SC_Response_Ex::sendRedirect(SHOPPING_CONFIRM_URLPATH);
184                    SC_Response_Ex::actionExit();
185                }
186
187                break;
188
189            // 前のページに戻る
190            case 'return':
191
192                // 正常な推移であることを記録しておく
193                $objSiteSess->setRegistFlag();
194
195                $url = null;
196                if ($this->is_multiple) {
197                    $url = MULTIPLE_URLPATH . '?from=multiple';
198                } elseif ($objCustomer->isLoginSuccess(true)) {
199                    if ($this->cartKey == PRODUCT_TYPE_DOWNLOAD) {
200                        $url = CART_URLPATH;
201                    } else {
202                        $url = DELIV_URLPATH;
203                    }
204                } else {
205                    $url = SHOPPING_URL . '?from=nonmember';
206                }
207
208                SC_Response_Ex::sendRedirect($url);
209                SC_Response_Ex::actionExit();
210                break;
211
212            default:
213                // FIXME 前のページから戻ってきた場合は別パラメーター(mode)で処理分岐する必要があるのかもしれない
214                $this->setFormParams($objFormParam, $arrOrderTemp, false, $this->arrShipping);
215
216                if (!$this->is_single_deliv) {
217                    $deliv_id = $objFormParam->getValue('deliv_id');
218                } else {
219                    $deliv_id = $this->arrDeliv[0]['deliv_id'];
220                }
221
222                if (!SC_Utils_Ex::isBlank($deliv_id)) {
223                    $objFormParam->setValue('deliv_id', $deliv_id);
224                    $arrSelectedDeliv = $this->getSelectedDeliv($objCartSess, $deliv_id);
225                    $this->arrPayment = $arrSelectedDeliv['arrPayment'];
226                    $this->arrDelivTime = $arrSelectedDeliv['arrDelivTime'];
227                    $this->img_show = $arrSelectedDeliv['img_show'];
228                }
229                break;
230        }
231
232        // モバイル用 ポストバック処理
233        if (SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE
234            && SC_Utils_Ex::isBlank($this->arrErr)) {
235            $this->tpl_mainpage = $this->getMobileMainpage($this->is_single_deliv, $this->getMode());
236        }
237
238        $this->arrForm = $objFormParam->getFormParamList();
239    }
240
241    /**
242     * パラメーターの初期化を行い, 初期値を設定する.
243     *
244     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
245     * @param array $arrParam 設定する値の配列
246     * @param boolean $deliv_only deliv_id チェックのみの場合 true
247     * @param array $arrShipping 配送先情報の配列
248     */
249    function setFormParams(&$objFormParam, $arrParam, $deliv_only, &$arrShipping)
250    {
251        $this->lfInitParam($objFormParam, $deliv_only, $arrShipping);
252        $objFormParam->setParam($arrParam);
253        $objFormParam->convParam();
254    }
255
256    /**
257     * パラメーター情報の初期化を行う.
258     *
259     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
260     * @param boolean $deliv_only 必須チェックは deliv_id のみの場合 true
261     * @param array $arrShipping 配送先情報の配列
262     * @return void
263     */
264    function lfInitParam(&$objFormParam, $deliv_only, &$arrShipping)
265    {
266        $objFormParam->addParam('配送業者', 'deliv_id', INT_LEN, 'n', array('EXIST_CHECK', 'MAX_LENGTH_CHECK', 'NUM_CHECK'));
267        $objFormParam->addParam('ポイント', 'use_point', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK', 'ZERO_START'));
268        $objFormParam->addParam('その他お問い合わせ', 'message', LTEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
269        $objFormParam->addParam('ポイントを使用する', 'point_check', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'), '2');
270
271        if ($deliv_only) {
272            $objFormParam->addParam('お支払い方法', 'payment_id', INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
273        } else {
274            $objFormParam->addParam('お支払い方法', 'payment_id', INT_LEN, 'n', array('EXIST_CHECK', 'MAX_LENGTH_CHECK', 'NUM_CHECK'));
275
276            foreach ($arrShipping as $val) {
277                $objFormParam->addParam('お届け時間', 'deliv_time_id' . $val['shipping_id'], INT_LEN, 'n', array('MAX_LENGTH_CHECK', 'NUM_CHECK'));
278                $objFormParam->addParam('お届け日', 'deliv_date' . $val['shipping_id'], STEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
279            }
280        }
281
282        $objFormParam->setParam($arrShipping);
283        $objFormParam->convParam();
284    }
285
286    /**
287     * 入力内容のチェックを行なう.
288     *
289     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
290     * @param integer $subtotal 購入金額の小計
291     * @param integer $max_point 会員の保持ポイント
292     * @return array 入力チェック結果の配列
293     */
294    function lfCheckError(&$objFormParam, $subtotal, $max_point)
295    {
296        // 入力データを渡す。
297        $arrForm =  $objFormParam->getHashArray();
298        $objErr = new SC_CheckError_Ex($arrForm);
299        $objErr->arrErr = $objFormParam->checkError();
300
301        if (USE_POINT === false) {
302            return $objErr->arrErr;
303        }
304
305        if ($arrForm['point_check'] == '1') {
306            $objErr->doFunc(array('ポイントを使用する', 'point_check'), array('EXIST_CHECK'));
307            $objErr->doFunc(array('ポイント', 'use_point'), array('EXIST_CHECK'));
308            if ($max_point == '') {
309                $max_point = 0;
310            }
311            // FIXME mobile 互換のため br は閉じない...
312            if ($arrForm['use_point'] > $max_point) {
313                $objErr->arrErr['use_point'] = '※ ご利用ポイントが所持ポイントを超えています。<br>';
314            }
315            if (($arrForm['use_point'] * POINT_VALUE) > $subtotal) {
316                $objErr->arrErr['use_point'] = '※ ご利用ポイントがご購入金額を超えています。<br>';
317            }
318            // ポイント差し引き後の決済方法チェック
319            $objPayment = new SC_Helper_Payment_Ex();
320            $arrPayments = $objPayment->get($arrForm['payment_id']);
321            if ($arrPayments['rule_max'] > $subtotal - $arrForm['use_point'] * POINT_VALUE) {
322                $objErr->arrErr['use_point'] = '※ 選択した支払方法では、ポイントは'.($subtotal - $arrPayments['rule_max']).'ポイントまでご利用いただけます。<br>';
323            }
324        }
325
326        return $objErr->arrErr;
327    }
328
329    /**
330     * 配送情報を保存する.
331     *
332     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
333     * @param array $arrDelivTime 配送時間の配列
334     */
335    function saveShippings(&$objFormParam, $arrDelivTime)
336    {
337        $deliv_id = $objFormParam->getValue('deliv_id');
338
339        /* TODO
340         * SC_Purchase::getShippingTemp() で取得して,
341         * リファレンスで代入すると, セッションに添字を追加できない?
342         */
343        foreach (array_keys($_SESSION['shipping']) as $key) {
344            $shipping_id = $_SESSION['shipping'][$key]['shipping_id'];
345            $time_id = $objFormParam->getValue('deliv_time_id' . $shipping_id);
346            $_SESSION['shipping'][$key]['deliv_id'] = $deliv_id;
347            $_SESSION['shipping'][$key]['time_id'] = $time_id;
348            $_SESSION['shipping'][$key]['shipping_time'] = $arrDelivTime[$time_id];
349            $_SESSION['shipping'][$key]['shipping_date'] = $objFormParam->getValue('deliv_date' . $shipping_id);
350        }
351    }
352
353    /**
354     * 受注一時テーブルへ登録を行う.
355     *
356     * @param integer $uniqid 受注一時テーブルのユニークID
357     * @param array $arrForm フォームの入力値
358     * @param SC_Helper_Purchase $objPurchase SC_Helper_Purchase インスタンス
359     * @param array $arrPayment お支払い方法の配列
360     * @return void
361     */
362    function lfRegistData($uniqid, $arrForm, &$objPurchase, $arrPayment)
363    {
364        $arrForm['order_temp_id'] = $uniqid;
365        $arrForm['update_date'] = 'CURRENT_TIMESTAMP';
366
367        if ($arrForm['point_check'] != '1') {
368            $arrForm['use_point'] = 0;
369        }
370
371        foreach ($arrPayment as $payment) {
372            if ($arrForm['payment_id'] == $payment['payment_id']) {
373                $arrForm['charge'] = $payment['charge'];
374                $arrForm['payment_method'] = $payment['payment_method'];
375                break;
376            }
377        }
378        $objPurchase->saveOrderTemp($uniqid, $arrForm);
379    }
380
381    /**
382     * 配送業者IDから, 支払い方法, お届け時間の配列を取得する.
383     *
384     * 結果の連想配列の添字の値は以下の通り
385     * - 'arrDelivTime' - お届け時間の配列
386     * - 'arrPayment' - 支払い方法の配列
387     * - 'img_show' - 支払い方法の画像の有無
388     *
389     * @param SC_CartSession $objCartSess SC_CartSession インスタンス
390     * @param integer $deliv_id 配送業者ID
391     * @return array 支払い方法, お届け時間を格納した配列
392     */
393    function getSelectedDeliv(&$objCartSess, $deliv_id)
394    {
395        $arrResults = array();
396        $arrResults['arrDelivTime'] = SC_Helper_Delivery_Ex::getDelivTime($deliv_id);
397        $total = $objCartSess->getAllProductsTotal($objCartSess->getKey());
398        $payments_deliv = SC_Helper_Delivery_Ex::getPayments($deliv_id);
399        $objPayment = new SC_Helper_Payment_Ex();
400        $payments_total = $objPayment->getByPrice($total);
401        $arrPayment = array();
402        foreach ($payments_total as $payment) {
403            if (in_array($payment['payment_id'], $payments_deliv)) {
404                $arrPayment[] = $payment;
405            }
406        }
407        $arrResults['arrPayment'] = $arrPayment;
408        $arrResults['img_show'] = $this->hasPaymentImage($arrResults['arrPayment']);
409
410        return $arrResults;
411    }
412
413    /**
414     * 支払い方法の画像があるかどうか.
415     *
416     * @param array $arrPayment 支払い方法の配列
417     * @return boolean 支払い方法の画像がある場合 true
418     */
419    function hasPaymentImage($arrPayment)
420    {
421        foreach ($arrPayment as $val) {
422            if (!SC_Utils_Ex::isBlank($val['payment_image'])) {
423                return true;
424            }
425        }
426
427        return false;
428    }
429
430    /**
431     * 配送業者が1社のみかどうか.
432     *
433     * @param array $arrDeliv 配送業者の配列
434     * @return boolean 配送業者が1社のみの場合 true
435     */
436    function isSingleDeliv($arrDeliv)
437    {
438        if (count($arrDeliv) == 1) {
439            return true;
440        } else {
441            return false;
442        }
443    }
444
445    /**
446     * モバイル用テンプレートのパスを取得する.
447     *
448     * @param boolean $is_single_deliv 配送業者が1社の場合 true
449     * @param string $mode フォームパラメーター 'mode' の文字列
450     * @return string モバイル用テンプレートのパス
451     */
452    function getMobileMainpage($is_single_deliv = true, $mode)
453    {
454        switch ($mode) {
455            case 'select_deliv':
456                return 'shopping/payment.tpl';
457
458            case 'confirm':
459            case 'return':
460            default:
461                if ($is_single_deliv) {
462                    return 'shopping/payment.tpl';
463                } else {
464                    return 'shopping/select_deliv.tpl';
465                }
466                break;
467        }
468    }
469}
Note: See TracBrowser for help on using the repository browser.