public function actionConfirms()
 {
     $models = new GoodsOrder();
     //实例化订单
     $students = new Students();
     //实例化学生
     $request = yii::$app->request->post();
     $session = yii::$app->session->get('userinfo');
     $user_id = $session['user_id'];
     $user = $students->getStuDetails($user_id);
     $Realpay = $request['costTotal'] - $request['money'];
     /*生成订单*/
     $models->order_sn = abs('110' . uniqid(time()));
     $models->user_id = $user_id;
     $models->user_name = $user['stu_name'];
     $models->user_phone = $session['user_phone'];
     $models->merchant_id = $request['mer_id'];
     $models->merchant_name = $request['mer_name'];
     $models->order_amount = $Realpay;
     $models->order_addtime = time();
     $models->order_price = $request['costTotal'];
     $res = $models->save();
     $order_id = Yii::$app->db->getLastInsertID();
     $type = 'MER_GOODS';
     if ($res) {
         $this->redirect(['/alipay/index', 'order_id' => $order_id, 'type' => $type]);
     } else {
         $this->error('支付失败');
     }
 }
 public function actionSaveorder()
 {
     $user_id = 1;
     $order_status = Yii::$app->request->get('order_status');
     if ($order_status != 3 && $order_status != 6) {
         $this->error('订单状态异常', ['consumption/order']);
     }
     //$order = new GoodsOrder;
     $order = GoodsOrder::find()->where(['merchant_id' => $user_id])->one();
     $order->order_status = $order_status;
     if ($order->save()) {
         $this->success('订单状态修改成功', ['consumption/order']);
     } else {
         $this->error('订单状态修改失败', ['consumption/order']);
     }
 }
 /**
  * 支付宝服务器异步通知页面
  */
 public function actionNotifyUrl()
 {
     $this->enableCsrfValidation = false;
     require_once '../../vendor/alipay/lib/alipay_core.function.php';
     require_once '../../vendor/alipay/lib/alipay_md5.function.php';
     require_once '../../vendor/alipay/lib/alipay_notify.class.php';
     require_once '../../vendor/alipay/lib/alipay_submit.class.php';
     $alipay_config = Yii::$app->params['alipay']['alipay_config'];
     //计算得出通知验证结果
     $alipayNotify = new \AlipayNotify($alipay_config);
     $verify_result = $alipayNotify->verifyNotify();
     if ($verify_result) {
         //验证成功
         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         //请在这里加上商户的业务逻辑程序代
         //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
         //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
         //商户订单号
         $out_trade_no = $_POST['out_trade_no'];
         $type = substr($out_trade_no, 0, 3);
         //支付宝交易号
         $trade_no = $_POST['trade_no'];
         //交易状态
         $trade_status = $_POST['trade_status'];
         if ($type == '110') {
             //更改订单状态
             $orders = GoodsOrder::sn($out_trade_no);
             $time = time();
             $order_id = $orders->order_id;
             $sql1 = "update fin_goods_order set order_status = '4',order_pay_time = '{$time}' where order_id = {$order_id}";
             $user_id = $orders->user_id;
             $order_amount = $orders->order_amount;
             $order_price = $orders->order_price;
             $coin = $order_price - $order_amount;
             //给用户减金币
             $student = Students::findOne($user_id);
             $money = $student->stu_money - $coin;
             $stu_id = $student->stu_id;
             $sql2 = "update fin_students set stu_money = '{$money}' where stu_id = '{$stu_id}'";
             //生成记录
             $sql3 = "insert into fin_payment(user_id,payment_type,payment_addtime,payment_money,payment_note,payment_way) values('{$user_id}','1','{$time}','{$coin}','商家订单','2')";
             $connection = \Yii::$app->db;
             $transaction = $connection->beginTransaction();
             try {
                 $connection->createCommand($sql1)->execute();
                 $connection->createCommand($sql2)->execute();
                 $connection->createCommand($sql3)->execute();
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollBack();
                 echo $e->getMessage();
             }
         } elseif ($type == '100') {
             //更改订单状态
             $orders = PayOrder::sn($out_trade_no);
             $time = time();
             $order_id = $orders->order_id;
             $sql1 = "update fin_pay_order set order_status = '4',order_pay_time = '{$time}' where order_id = {$order_id}";
             $user_id = $orders->user_id;
             $coin = $orders->order_price;
             //给用户添加金币
             $student = Students::findOne($user_id);
             $money = $student->stu_money + $coin;
             $stu_id = $student->stu_id;
             $sql2 = "update fin_students set stu_money = '{$money}' where stu_id = '{$stu_id}'";
             //生成记录
             $sql3 = "insert into fin_payment(user_id,payment_type,payment_addtime,payment_money,payment_note,payment_way) values('{$user_id}','1','{$time}','{$coin}','充值金币','2')";
             $connection = \Yii::$app->db;
             $transaction = $connection->beginTransaction();
             try {
                 $connection->createCommand($sql1)->execute();
                 $connection->createCommand($sql2)->execute();
                 $connection->createCommand($sql3)->execute();
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollBack();
                 echo $e->getMessage();
             }
         }
         if ($_POST['trade_status'] == 'TRADE_FINISHED') {
             //判断该笔订单是否在商户网站中已经做过处理
             //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
             //如果有做过处理,不执行商户的业务程序
             //注意:
             //该种交易状态只在两种情况下出现
             //1、开通了普通即时到账,买家付款成功后。
             //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。
             //调试用,写文本函数记录程序运行情况是否正常
             //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
             //logResult($_POST['trade_status']);
         } else {
             if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                 //判断该笔订单是否在商户网站中已经做过处理
                 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
                 //如果有做过处理,不执行商户的业务程序
                 //注意:
                 //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。
                 //调试用,写文本函数记录程序运行情况是否正常
                 //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
                 //logResult($_POST['trade_status']);
             }
         }
         //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
         echo "success";
         //请不要修改或删除
         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     } else {
         //验证失败
         echo "fail";
         //调试用,写文本函数记录程序运行情况是否正常
         //logResult("这里写入想要调试的代码变量值,或其他运行的结果记录");
     }
 }