PayForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace common\pay;
  3. use backend\models\Deposit;
  4. use backend\models\Member;
  5. use common\helpers\RateHelper;
  6. use yii\base\Model;
  7. class PayForm extends Model
  8. {
  9. public $amount;
  10. public $login;
  11. public $memberId;
  12. public $payType;
  13. public $notifyUrl;
  14. public $returnUrl;
  15. public $bankCode;
  16. public $ip;
  17. public $orderSn;
  18. private $_outPayResult;
  19. private $_outNotifyResult;
  20. private $_outReturnResult;
  21. public static $payNames = [
  22. /*1 => '人人支付',
  23. 2 => '网通支付',
  24. 3 => '杉德支付',
  25. 4 => '顺通支付',
  26. 5 => '3xmta支付',
  27. 6 => '迅捷支付',
  28. 7 => 'Ctype支付',
  29. 8 => '科星支付',
  30. */
  31. 9 => 'RTW支付',
  32. 10 => '威速通支付',
  33. 11 => '千万宝支付',
  34. 12 => '万鑫支付',
  35. 13 => '亿来支付',
  36. 14 => '云富支付',
  37. 15 => '付临门支付',
  38. 16 => '人工通道',
  39. 17 => '亿来2号',
  40. 18 => '乐支付',
  41. ];
  42. /**
  43. * @return array
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['amount', 'login', 'memberId', 'payType', 'notifyUrl', 'returnUrl','orderSn'], 'required'],
  49. ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  50. [['login', 'memberId', 'payType'], 'integer'],
  51. [['notifyUrl', 'returnUrl', 'ip', 'orderSn'], 'string'],
  52. ['payType', 'in', 'range' => array_keys(self::$payNames)],
  53. ['bankCode', 'required', 'when' => function ($model) {
  54. /** @var PayForm $model */
  55. return in_array($model->payType, [9]);
  56. }, 'message' => '请选择银行'],
  57. ['login', 'checkLogin'],
  58. ];
  59. }
  60. /**
  61. * @param string $attribute
  62. * @param array $params
  63. */
  64. public function checkLogin($attribute, $params = [])
  65. {
  66. if (!$this->hasErrors()) {
  67. $member = Member::findById($this->memberId);
  68. if ($member == null) {
  69. $this->addError($attribute, '用户不存在');
  70. } else {
  71. $logins = preg_split('/\s*,\s*/', $member['logins']);
  72. if (!in_array($this->login, $logins)) {
  73. $this->addError($attribute, 'MT4账户不属于当前用户');
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * @return bool
  80. */
  81. public function outPay()
  82. {
  83. if ($this->validate()) {
  84. $rate = RateHelper::getRate();
  85. if (empty($rate['sellRate'])) {
  86. $this->addError('amount', '汇率获取失败');
  87. return false;
  88. }
  89. $payType = $this->payType;
  90. $model = new Deposit();
  91. $model->type = 0;
  92. $model->member_id = $this->memberId;
  93. $model->amount = $this->amount;
  94. $model->rate = $rate['sellRate'];
  95. $model->rmb = number_format($this->amount * $model->rate, 2, '.', '');
  96. $model->in_time = intval(microtime(true) * 1000);
  97. $model->pay_name = isset(self::$payNames[$payType]) ? trim(self::$payNames[$payType]) : '';
  98. $model->login = $this->login;
  99. $model->order_sn = $this->orderSn;
  100. if ($model->save()) {
  101. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  102. if ($handler == null) {
  103. $this->addError('amount', '支付方式不存在');
  104. return false;
  105. }
  106. $handler->notifyUrl = $this->notifyUrl;
  107. $handler->returnUrl = $this->returnUrl;
  108. $params = [];
  109. $params['bankCode'] = $this->bankCode;
  110. $params['ip'] = $this->ip;
  111. $this->_outPayResult = $handler->outPay($model->getAttributes(), $params);
  112. }
  113. }
  114. return !$this->hasErrors();
  115. }
  116. /**
  117. * @return mixed
  118. */
  119. public function getOutPayResult()
  120. {
  121. return $this->_outPayResult;
  122. }
  123. /**
  124. * @param array $data
  125. * @return bool
  126. */
  127. public function handleNotify($data)
  128. {
  129. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  130. unset($data['token']);
  131. unset($data['payType']);
  132. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  133. $success = false;
  134. if ($handler != null) {
  135. $success = $handler->handleNotify($data);
  136. $this->_outNotifyResult = $handler->outNotify($success);
  137. }
  138. return $success;
  139. }
  140. /**
  141. * @return mixed
  142. */
  143. public function getOutNotifyResult()
  144. {
  145. return $this->_outNotifyResult;
  146. }
  147. /**
  148. * @param array $data
  149. * @return bool
  150. */
  151. public function handleReturn($data)
  152. {
  153. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  154. unset($data['token']);
  155. unset($data['payType']);
  156. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  157. $success = false;
  158. if ($handler != null) {
  159. $success = $handler->handleReturn($data);
  160. $this->_outReturnResult = $handler->outReturn($success);
  161. }
  162. return $success;
  163. }
  164. /**
  165. * @return mixed
  166. */
  167. public function getOutReturnResult()
  168. {
  169. return $this->_outReturnResult;
  170. }
  171. }