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