<?php
namespace Vip\Lib;

use Dever;

class Pay
{
	/**
	 * 支付成功后，调取的接口
	 *
	 * @return mixed
	 */
	public function act_api($param = array())
	{
        $send = $param ? $param : Dever::preInput('pay_');
        $key = md5('vip_buy');
        ksort($send);
        $send['signature'] = md5($key . '&' . http_build_query($send));
        $signature = Dever::input('signature');
        if ($send['signature'] == $signature) {
            $product_id = $send['pay_product_id'];
            $uid = $send['pay_uid'];
            $cash = $send['pay_cash'];
            $order_id = $send['pay_order_id'];
            $status = $send['pay_status'];
            $msg = $send['pay_msg'];

            $order = Dever::db('vip/order')->one(array('order_id' => $order_id, 'uid' => $uid));

            if ($send['pay_status'] == 2 && $order) {
                
                $update = array();
                $update['where_id'] = $order['id'];
                $update['status'] = 2;

                Dever::db('vip/order')->update($update);

                if ($order['type'] != 1) {
                    return;
                }

                if ($order['buy_id'] > 0) {
                    $buy = Dever::db('vip/time')->one($order['buy_id']);
                    if ($buy) {

                        $member = Dever::db('vip/member')->one(array('mobile' => $order['mobile'], 'cate_id' => $order['cate_id']));

                        $insert['cate_id'] = $order['cate_id'];
                        $insert['mobile'] = $order['mobile'];
                        $insert['uid'] = $order['uid'];
                        $time = time();
                        if ($member) {
                            if ($time > $member['end']) {
                                $member['end'] = $time;
                            }
                            $insert['end'] = Dever::load('vip/lib/member')->getEnd($buy['time'], $buy['time_type'], $member['end']);
                            $insert['where_id'] = $member['id'];
                            Dever::db('vip/member')->update($insert);
                        } else {
                            $insert['end'] = Dever::load('vip/lib/member')->getEnd($buy['time'], $buy['time_type'], $time);
                            $insert['start'] = $time;
                            Dever::db('vip/member')->insert($insert);
                        }

                        # 发消息
                        /*
                        if (Dever::project('message')) {
                            Dever::load('message/lib/data')->push(-1, $uid, '购买提醒', '购买成功，您获得了 '.$buy['name'].' 的会员资格！', 11);
                        }

                        # 发短信
                        if (Dever::project('sms')) {
                            $send = array();
                            $send['name'] = $buy['name'];
                            Dever::load('sms/api.send', 'buy_member', $order['mobile'], $send);
                        }
                        */
                    }
                }
            } else {
                Dever::db('vip/order')->update(array('where_id' => $order['id'], 'status' => 3));
            }
        }
	}
}
