frontend\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;
        }
        try {
            switch ($this->platform) {
                case self::PLATFORM_ALIPAY:
                    require_once Yii::getAlias('@vendor') . '/payment/alipay/alipay.config.php';
                    require_once Yii::getAlias('@vendor') . '/payment/alipay/lib/alipay_submit.class.php';
                    $parameter = ['service' => 'create_direct_pay_by_user', 'partner' => trim($alipay_config['partner']), 'seller_email' => trim($alipay_config['seller_email']), 'payment_type' => '1', 'notify_url' => Url::to(['/payment/alipay-order-notify'], true), 'return_url' => Url::to(['/payment/alipay-order-return'], true), 'out_trade_no' => $this->_order->order_sn, 'subject' => '订单支付', 'total_fee' => $this->_order->real_fee, 'body' => '笑e购(xiaoego.com)订单,订单号:' . $this->_order->order_sn, 'show_url' => Url::to(['/order/detail', 'order' => $this->_order->order_sn], true), 'anti_phishing_key' => '', 'exter_invoke_ip' => '', '_input_charset' => trim(strtolower($alipay_config['input_charset']))];
                    //建立请求
                    $alipaySubmit = new \AlipaySubmit($alipay_config);
                    $htmlText = $alipaySubmit->buildRequestForm($parameter, 'post', '');
                    break;
                default:
                    throw new InvalidValueException();
            }
            return $htmlText;
        } catch (\Exception $e) {
            return false;
        }
    }

Usage Example

 public function actionPay($order)
 {
     $this->layout = 'column2';
     try {
         $payOrderForm = new PayOrderForm($order);
     } catch (InvalidParamException $e) {
         throw new BadRequestHttpException($e->getMessage());
     }
     if ($payOrderForm->order->status !== Order::STATUS_UNPAID) {
         return $this->redirect(['/order/detail', 'order' => $order]);
     }
     if ($payOrderForm->load(Yii::$app->request->post())) {
         if ($result = $payOrderForm->pay()) {
             Yii::info("用户请求支付订单成功!订单号:{$order},支付平台:{$payOrderForm->platform}");
             $this->layout = false;
             return $result;
         } else {
             Yii::error("用户请求支付订单失败!订单号:{$order},支付平台:{$payOrderForm->platform}");
         }
     }
     return $this->render('pay', ['model' => $payOrderForm, 'order' => $payOrderForm->order]);
 }