ChargeController.php 1.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/10/16
 * Time: 13:38
 */

namespace app\charge\controller;
use cmf\controller\HomeBaseController;
use think\Db;

class ChargeController extends HomeBaseController
{

    function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        require_once EXTEND_PATH . '/Charge.php';
    }

    public function charge()
    {
        $request = request();
        if ($request->isAjax()) {
            $info = Db::name('charge')->where(['id' => $request->param('id')])->find();
            $pay = new \Payment($request->param('id'), session('user.openid'), '充值', 1/*$info['denomination'] * 100*/);
            $this->success('微信充值', '', $pay->pay());
        }
    }

    public function notify()
    {
        $pay = new \Payment();
        $return = $pay->handleNotify();
        if (!empty($return)) {
            $info = Db::name('charge')->where(['id'=>$return['attach']])->find();
            $balance = $info['denomination'] + $info['bonus'];
            Db::startTrans();
            if (Db::name('user')->where(['openid'=>$return['openid']])->setInc('balance', $balance)) {
                $data = [
                    'user_id' => Db::name('user')->where(['openid'=>$return['openid']])->value('id'),
                    'type' => 1,
                    'cost' => $balance,
                    'create_time' => time()
                ];
                if (Db::name('my_wallet')->insert($data)) {
                    Db::commit();
                    echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                    exit();
                } else {
                    Db::rollback();
                }
            } else {
                Db::rollback();
            }
        }
    }
}