PayForm.php 6.6 KB

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