m\modules\v1\models\PayOrderForm::pay PHP Method

pay() public method

public pay ( $runValidation = true )
    public function pay($runValidation = true)
    {
        if ($runValidation && !$this->validate()) {
            return false;
        }
        if ($this->_order->status !== Order::STATUS_UNPAID) {
            return false;
        }
        require_once Yii::getAlias('@vendor') . "/pingplusplus/pingpp-php/init.php";
        \Pingpp\Pingpp::setApiKey(Yii::$app->params['pingpp.apiKey']);
        try {
            $extra = [];
            switch ($this->channel) {
                case self::CHANNEL_ALIPAY_WAP:
                    $extra = ['success_url' => Yii::$app->request->hostInfo . '/#/order/detail/' . $this->_order->id, 'cancel_url' => Yii::$app->request->hostInfo . '/#/order/pay/' . $this->_order->id];
                    break;
                case self::CHANNEL_WX_PUB:
                    Yii::$app->session->open();
                    $extra = ['open_id' => Yii::$app->session['wechatOpenid']];
                    break;
                case self::CHANNEL_ALIPAY_PC_DIRECT:
                    $extra = ['success_url' => Yii::$app->request->hostInfo . '/#/order/detail/' . $this->_order->id];
                    break;
                default:
                    throw new InvalidValueException('支付渠道错误!');
            }
            $ch = \Pingpp\Charge::create(['subject' => '笑e购订单', 'body' => '笑e购(xiaoego.com)订单,订单号:' . $this->_order->order_sn, 'amount' => bcmul($this->_order->real_fee, 100), 'order_no' => $this->_order->order_sn, 'currency' => 'cny', 'extra' => $extra, 'channel' => $this->channel, 'client_ip' => Yii::$app->request->userIP, 'time_expire' => $this->_order->timeout + 1800, 'app' => ['id' => Yii::$app->params['pingpp.appId']], 'description' => mb_strlen($this->_order->description, 'UTF-8') <= 255 ? $this->_order->description : substr($this->_order->description, 0, 253) . '……']);
            return $ch;
        } catch (\Exception $e) {
            throw $e;
        }
    }

Usage Example

 public function actionPay($id)
 {
     try {
         $payOrderForm = new PayOrderForm($id);
     } catch (InvalidParamException $e) {
         throw new BadRequestHttpException($e->getMessage());
     }
     if ($payOrderForm->load(Yii::$app->request->post(), '')) {
         try {
             if ($result = $payOrderForm->pay()) {
                 Yii::info("用户请求支付订单成功!订单号:{$payOrderForm->order->order_sn},支付渠道:{$payOrderForm->channel}");
                 return ['status' => 'success', 'data' => ['charge' => $result->__toArray(true)]];
             } else {
                 $message = '请求支付订单失败!';
             }
         } catch (\Exception $e) {
             $message = $e->getMessage();
         }
     } else {
         $message = $payOrderForm->getFirstError('channel');
     }
     Yii::error("用户请求支付订单失败!订单号:{$payOrderForm->order->order_sn},支付渠道:{$payOrderForm->channel},说明:{$message}");
     return ['status' => 'fail', 'data' => ['message' => $message]];
 }