| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace backend\models\forms;
- use backend\models\PayApi;
- use common\helpers\Utils;
- 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 => '顺通支付',
- 5 => '3xmta支付',
- 6 => '迅捷支付',
- 7 => 'Ctype支付',
- 8 => '科星支付',
- 9 => '创汇支付',
- 10 =>'OTC支付',
- 11 =>'银联支付A',
- 12 =>'银联支付B',
- 13 =>'银联支付C',
- 14 =>'JD通道A',
- 15 =>'eagle',
- ];
- public static $payMethods = [
- 'renren' => 1,
- 'wangtong' => 2,
- 'sand' => 3,
- 'shangxinshuntong' => 4,
- 'threexmta' => 5,
- 'xunjie' => 6,
- 'ctype' => 7,
- 'kexing' => 8,
- 'chuanghui' => 9,
- 'otczhifu' =>10,
- 'yinliantiaozhuan'=>11,
- 'yinliantiaozhuan2'=>12,
- 'yinliantiaozhuan3'=>13,
- 'jiedeng'=>14,
- 'eagle'=>15,
- ];
- /**
- * @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', 'required', 'when' => function ($model) {
- /** @var PayForm $model */
- return in_array($model->payType, [1,3,8]);
- }, 'message' => '请选择银行'],
- ];
- }
- /**
- * @return bool
- */
- public function outPay()
- {
- if (Yii::$app->getUser()->getIsGuest()) {
- $this->addError('login', '用户未登录');
- Yii::$app->getUser()->loginRequired();
- return false;
- }
- if ($this->validate()) {
- $data = [];
- $data['orderSn'] = date('YmdHis').rand(1000,9999);
- $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);
- $data['ip'] = Utils::getClientIp();
- $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;
- }
- }
|