PayForm.php 6.4 KB

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