PayForm.php 6.3 KB

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