PayForm.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ];
  32. /**
  33. * @return array
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['amount', 'login', 'memberId', 'payType', 'notifyUrl', 'returnUrl','orderSn'], 'required'],
  39. ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  40. [['login', 'memberId', 'payType'], 'integer'],
  41. [['notifyUrl', 'returnUrl', 'ip', 'orderSn'], 'string'],
  42. ['payType', 'in', 'range' => array_keys(self::$payNames)],
  43. ['bankCode', 'required', 'when' => function ($model) {
  44. /** @var PayForm $model */
  45. return in_array($model->payType, [1,3,8]);
  46. }, 'message' => '请选择银行'],
  47. ['login', 'checkLogin'],
  48. ];
  49. }
  50. /**
  51. * @param string $attribute
  52. * @param array $params
  53. */
  54. public function checkLogin($attribute, $params = [])
  55. {
  56. if (!$this->hasErrors()) {
  57. $member = Member::findById($this->memberId);
  58. if ($member == null) {
  59. $this->addError($attribute, '用户不存在');
  60. } else {
  61. $logins = preg_split('/\s*,\s*/', $member['logins']);
  62. if (!in_array($this->login, $logins)) {
  63. $this->addError($attribute, 'MT4账户不属于当前用户');
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * @return bool
  70. */
  71. public function outPay()
  72. {
  73. if ($this->validate()) {
  74. $rate = RateHelper::getRate();
  75. if (empty($rate['sellRate'])) {
  76. $this->addError('amount', '汇率获取失败');
  77. return false;
  78. }
  79. $payType = $this->payType;
  80. $model = new Deposit();
  81. $model->type = 0;
  82. $model->member_id = $this->memberId;
  83. $model->amount = $this->amount;
  84. $model->rate = $rate['sellRate'];
  85. $model->rmb = number_format($this->amount * $model->rate, 2, '.', '');
  86. $model->in_time = intval(microtime(true) * 1000);
  87. $model->pay_name = isset(self::$payNames[$payType]) ? trim(self::$payNames[$payType]) : '';
  88. $model->login = $this->login;
  89. $model->order_sn = $this->orderSn;
  90. if ($model->save()) {
  91. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  92. if ($handler == null) {
  93. $this->addError('amount', '支付方式不存在');
  94. return false;
  95. }
  96. $handler->notifyUrl = $this->notifyUrl;
  97. $handler->returnUrl = $this->returnUrl;
  98. $params = [];
  99. $params['bankCode'] = $this->bankCode;
  100. $params['ip'] = $this->ip;
  101. $this->_outPayResult = $handler->outPay($model->getAttributes(), $params);
  102. }
  103. }
  104. return !$this->hasErrors();
  105. }
  106. /**
  107. * @return mixed
  108. */
  109. public function getOutPayResult()
  110. {
  111. return $this->_outPayResult;
  112. }
  113. /**
  114. * @param array $data
  115. * @return bool
  116. */
  117. public function handleNotify($data)
  118. {
  119. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  120. unset($data['token']);
  121. unset($data['payType']);
  122. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  123. $success = false;
  124. if ($handler != null) {
  125. $success = $handler->handleNotify($data);
  126. $this->_outNotifyResult = $handler->outNotify($success);
  127. }
  128. return $success;
  129. }
  130. /**
  131. * @return mixed
  132. */
  133. public function getOutNotifyResult()
  134. {
  135. return $this->_outNotifyResult;
  136. }
  137. /**
  138. * @param array $data
  139. * @return bool
  140. */
  141. public function handleReturn($data)
  142. {
  143. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  144. unset($data['token']);
  145. unset($data['payType']);
  146. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  147. $success = false;
  148. if ($handler != null) {
  149. $success = $handler->handleReturn($data);
  150. $this->_outReturnResult = $handler->outReturn($success);
  151. }
  152. return $success;
  153. }
  154. /**
  155. * @return mixed
  156. */
  157. public function getOutReturnResult()
  158. {
  159. return $this->_outReturnResult;
  160. }
  161. }