| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace backend\models\forms;
- use backend\models\PayApi;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Url;
- class PayForm extends Model
- {
- public $login;
- public $amount;
- public $payType;
- public $bankCode;
- private $_outPayResult;
- public static $payNames = [
- // 1 => '爱农',
- 2 => '汇道',
- // 3 => '',
- 4 => '华银',
- ];
- public static $payMethods = [
- // 'an' => 1,
- 'hd' => 2,
- // 'yk' => '3',
- 'huayin' => 4,
- ];
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['login', 'amount', 'payType'], 'required'],
- ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
- ['payType', 'integer'],
- ['payType', 'in', 'range' => array_keys(self::$payNames)],
- ['bankCode', 'checkBankCode'],
- ];
- }
- /**
- * @param string $attribute
- * @param array $params
- */
- public function checkBankCode($attribute, $params = [])
- {
- if (!$this->hasErrors()) {
- if ($this->payType == 4) {
- if ($this->bankCode == "") {
- $this->addError($attribute, "请选择银行");
- }
- }
- }
- }
- /**
- * @return bool
- */
- public function outPay()
- {
- if (Yii::$app->getUser()->getIsGuest()) {
- $this->addError('login', '用户未登录');
- Yii::$app->getUser()->loginRequired();
- return false;
- }
- if ($this->validate()) {
- $data = [];
- if ($this->login == '628703') {
- $this->amount = '0.1';
- }
- $data['amount'] = $this->amount;
- $data['login'] = $this->login;
- $data['payType'] = $this->payType;
- $data['bankCode'] = $this->bankCode;
- $data['memberId'] = Yii::$app->getUser()->getId();
- $data['notifyUrl'] = Url::to(["/pay/notify/{$this->payType}"], true);
- $data['returnUrl'] = Url::to(["/pay/return/{$this->payType}"], true);
- $api = new PayApi();
- $result = $api->outPay($data);
- if ($result['code'] == 1) {
- $this->_outPayResult = $result['data'];
- } else {
- if (is_array($result['message'])) {
- $this->addErrors($result['message']);
- } else {
- $this->addError('amount', $result['message']);
- }
- }
- }
- return !$this->hasErrors();
- }
- /**
- * @return string
- */
- public function getOutPayResult()
- {
- return $this->_outPayResult;
- }
- /**
- * @param string $payMethod
- * @return mixed|null
- */
- public function getPayTypeByMethod($payMethod)
- {
- return isset(self::$payMethods[$payMethod]) ? self::$payMethods[$payMethod] : null;
- }
- }
|