PayForm.php 4.9 KB

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