PayForm.php 5.3 KB

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