ユーザ用ツール

サイト用ツール


facebook_credits_api:sample_cakephp_model_1

サンプル - cakephp - model(1)

<?php
class Credit extends AppModel {
    var $name = 'Credit';
    var $useDbConfig = 'credit';
    var $useTable = 'credit_orders';

    var $validate = array(
        'order_id' => array(
            'alphanumeric' => array(
                'rule' => 'alphaNumeric',
                'required' => true,
                'message' => 'Alphabets and numbers only'
            )
        ),
        'credit_item_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'required' => true,
                'message' => 'Numbers only'
            )
        ),
        'price' => array(
            'salary' => array(
                'rule' => array('money', 'left'),
                'message' => 'Money format only'
            )
        ),
        'status' => array(
            'alphanumeric' => array(
                'rule' => 'alphaNumeric',
                'required' => true,
                'message' => 'Alphabets and numbers only'
            )
        ),
    );

    var $get_fields = array(
        'CreditItem.id',
        'CreditItem.title',
        'CreditItem.description',
        'CreditItem.image_url',
        'CreditItem.product_url',
        'CreditItem.price',
    );

    public function createData($cons) {
        return $this->save($cons);
    }
    // アイテム一覧作成のために、アイテム情報取得
    public function get() {
        App::import('Model','CreditItem');
        $this->CreditItem = new CreditItem();
        return $this->CreditItem->get();
    }
    // callbackの実処理
    public function creditOrder($func, $request) {
        // ログ出力
        $params = array(
            'func' => $func,
            'request' => $request
        );
        $this->customLog('credits_callback.log', $params);

        // 返り値初期化
        $data = array('content' => array());

        //
        $payload = $request['credits'];
        if ($func == 'payments_status_update') {
            // 購入ボタンが押されたときの処理
            $data = $this->paymentsStatusUpdate($payload);
        } else if ($func == 'payments_get_items') {
            // 確認ダイアログ生成用の情報取得
            $data = $this->paymentsGetItems($payload);
        }
        $data['method'] = $func;
        return $data;
    }
    // 購入ボタンが押されたときの処理
    public function paymentsStatusUpdate($payload) {
        $status = $payload['status'];
        if ($status == 'placed') {
            $next_state = 'settled';
            $data['content']['status'] = $next_state;
            $order_details = stripcslashes($payload['order_details']);
            $item = json_decode($order_details, true);

            // credit_ordersテーブルに購入情報を登録
            $result = $this->insertCreditOrders($item, $next_state);

            // チアポイントの加算処理
            $result = $this->updateCheerPoint($item);
        }
        $data['content']['order_id'] = $payload['order_id'];
        return $data;
    }
    // credit_ordersテーブルに購入情報を登録
    public function insertCreditOrders($item, $next_state) {
        $cons = array(
            'order_id' => $item['order_id'],
            'credit_item_id' => $item['items'][0]['item_id'],
            'price' => $item['items'][0]['price'],
            'status' => $next_state
        );
        if (!$this->createData($cons)) {
            $this->renderErrorJson(array('db' => 'insert_error'));
            return;
        }
        return true;
    }
    // チアポイントの加算処理
    public function updateCheerPoint($item) {
        // facebook IDからuser IDを取得
        $result  = ClassRegistry::init('User')->getUserIdFromFacebookId($item['buyer']);
        $user_id = $result;
        $sign    = 'plus';

        // credit_itemsテーブルから該当商品のチアポイントを取得
        $result  = ClassRegistry::init('CreditItem')->get($item['items'][0]['item_id']);
        $point   = $result['CreditItem'][0]['cheer_point'];
        $kind    = 'cheer_point';

        // チアポイントの加算処理
        $result  = ClassRegistry::init('Point')->updatePoint($user_id, $sign, $point, $kind);
        return $result;
    }
    // 確認ダイアログ生成用の情報取得
    public function paymentsGetItems($payload) {
        $order_info = stripcslashes($payload['order_info']);
        $item = json_decode($order_info, true);
        // 商品価格
        $item['price'] = (int)$item['price'];
        // 商品URL、商品画像URL
        $url_key = array('product_url', 'image_url');
        foreach ($url_key as $key) {
            if (substr($item[$key], 0, 7) != 'http://') {
                $item[$key] = 'http://'.$item[$key];
            }
        }
        // test_mode 確認
        if (isset($payload['test_mode'])) {
            $update_keys = array('title', 'description');
            foreach ($update_keys as $key) {
                $item[$key] = '[Test Mode] '.$item[$key];
            }
        }
        $data['content'] = array($item);
        return $data;
    }
}
facebook_credits_api/sample_cakephp_model_1.txt · 最終更新: 2014/03/06 07:11 by clownclown

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki