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

Revision 22568, 18.1 KB checked in by pineray, 11 years ago (diff)

#2134 支払い方法に関する処理を SC_Helper_Payment へ集約.

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