| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace common\pay;
- use backend\models\Deposit;
- use backend\models\Member;
- use common\helpers\RateHelper;
- use yii\base\Model;
- class PayForm extends Model
- {
- public $amount;
- public $login;
- public $memberId;
- public $payType;
- public $notifyUrl;
- public $returnUrl;
- public $bankCode;
- public $ip;
- public $orderSn;
- private $_outPayResult;
- private $_outNotifyResult;
- private $_outReturnResult;
- public static $payNames = [
- /*1 => '人人支付',
- 2 => '网通支付',
- 3 => '杉德支付',
- 4 => '顺通支付',
- 5 => '3xmta支付',
- 6 => '迅捷支付',
- 7 => 'Ctype支付',
- 8 => '科星支付',
- ,*/
- 10 =>'OTC支付',
- 11 =>'银联支付A',
- 12 =>'银联支付B',
- 13 =>'银联支付C',
- 14 =>'JD通道A',
- 15 =>'eagle',
- 16 =>'对私支付',
- 17 =>'对公支付',
- 18 =>'globalpay',
- 19 =>'payplat支付',
- ];
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['amount', 'login', 'memberId', 'payType', 'notifyUrl', 'returnUrl','orderSn'], 'required'],
- ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
- [['login', 'memberId', 'payType'], 'integer'],
- [['notifyUrl', 'returnUrl', 'ip', 'orderSn'], 'string'],
- ['payType', 'in', 'range' => array_keys(self::$payNames)],
- ['bankCode', 'required', 'when' => function ($model) {
- /** @var PayForm $model */
- return in_array($model->payType, [1,3,8]);
- }, 'message' => '请选择银行'],
- ['login', 'checkLogin'],
- ];
- }
- /**
- * @param string $attribute
- * @param array $params
- */
- public function checkLogin($attribute, $params = [])
- {
- if (!$this->hasErrors()) {
- $member = Member::findById($this->memberId);
- if ($member == null) {
- $this->addError($attribute, '用户不存在');
- } else {
- $logins = preg_split('/\s*,\s*/', $member['logins']);
- if (!in_array($this->login, $logins)) {
- $this->addError($attribute, 'MT4账户不属于当前用户');
- }
- }
- }
- }
- /**
- * @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();
- if($payType=='11'){
- $model->type = 2;
- }else if($payType=='12'){
- $model->type = 2;
- }else if($payType=='13'){
- $model->type = 2;
- }else if($payType=='14'){
- $model->type = 2;
- }else{
- $model->type = 0;
- }
-
- $model->member_id = $this->memberId;
- $model->amount = $this->amount;
-
-
- if($payType=='11'||$payType=='12'||$payType=='13'|| $payType=='14' || $payType=='15'){
- $model->rate = $rate['exchangeRate'];
- }else{
- $model->rate = $rate['sellRate'];
- if($this->amount<100){
- $this->addError('amount', '单笔支付不能小于100美元');
- }
- }
- $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;
- $model->order_sn = $this->orderSn;
- if($payType=='15'){
- $handler = BasePayHandler::getPayHandlerByPayType($payType);
- if ($handler == null) {
- $this->addError('amount', '支付方式不存在');
- return false;
- }
- $params = [];
- $params['login'] = $this->login;
- $params['member_id'] = $this->memberId;
- $params['bankCode'] = $this->bankCode;
- $params['ip'] = $this->ip;
- $this->_outPayResult = $handler->outPay('', $params);
- }else{
- 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;
- $params['login'] = $this->login;
- $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']) : '';
- if($data['payType']=="wy_bb" || $data['payType']=="alipay_bb"){
- $handler = BasePayHandler::getPayHandlerByPayType(19);
- }else{
- $handler = BasePayHandler::getPayHandlerByPayType($payType);
- unset($data['payType']);
- }
- unset($data['token']);
- $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;
- }
- }
|