PayForm.php 5.6 KB

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