'爱农', 2 => '汇道', 3 => '', 4 => '华银', 5 => '奥凸', 6 => '汇道', ]; /** * @return array */ public function rules() { return [ [['amount', 'login', 'memberId', 'payType', 'notifyUrl', 'returnUrl'], 'required'], ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'], [['login', 'memberId', 'payType'], 'integer'], [['notifyUrl', 'returnUrl', 'ip'], 'string'], ['payType', 'in', 'range' => array_keys(self::$payNames)], ['bankCode', 'required', 'when' => function ($model) { /** @var PayForm $model */ return in_array($model->payType, [4, 5, 6]); }, 'message' => '请选择银行'], ]; } /** * @return bool */ public function outPay() { if ($this->validate()) { $rate = RateHelper::getRate(); if (empty($rate['sellRate'])) { $this->addError('amount', '汇率获取失败'); return false; } $payType = $this->payType; $model = new Deposit(); $model->type = 0; $model->member_id = $this->memberId; $model->amount = $this->amount; $model->rate = $rate['sellRate']; $model->rmb = number_format($this->amount * $model->rate, 2, '.', ''); $model->in_time = intval(microtime(true) * 1000); $model->pay_name = isset(self::$payNames[$payType]) ? trim(self::$payNames[$payType]) : ''; $model->login = $this->login; if ($model->save()) { $handler = BasePayHandler::getPayHandlerByPayType($payType); if ($handler == null) { $this->addError('amount', '支付方式不存在'); return false; } $handler->notifyUrl = $this->notifyUrl; $handler->returnUrl = $this->returnUrl; $params = []; $params['bankCode'] = $this->bankCode; $params['ip'] = $this->ip; $this->_outPayResult = $handler->outPay($model->getAttributes(), $params); } } return !$this->hasErrors(); } /** * @return mixed */ public function getOutPayResult() { return $this->_outPayResult; } /** * @param array $data * @return bool */ public function handleNotify($data) { $payType = isset($data['payType']) ? trim($data['payType']) : ''; unset($data['token']); unset($data['payType']); $handler = BasePayHandler::getPayHandlerByPayType($payType); $success = false; if ($handler != null) { $success = $handler->handleNotify($data); $this->_outNotifyResult = $handler->outNotify($success); } return $success; } /** * @return mixed */ public function getOutNotifyResult() { return $this->_outNotifyResult; } /** * @param array $data * @return bool */ public function handleReturn($data) { $payType = isset($data['payType']) ? trim($data['payType']) : ''; unset($data['token']); unset($data['payType']); $handler = BasePayHandler::getPayHandlerByPayType($payType); $success = false; if ($handler != null) { $success = $handler->handleReturn($data); $this->_outReturnResult = $handler->outReturn($success); } return $success; } /** * @return mixed */ public function getOutReturnResult() { return $this->_outReturnResult; } }