PayForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace common\pay;
  3. use backend\models\Deposit;
  4. use common\helpers\RateHelper;
  5. use yii\base\Model;
  6. class PayForm extends Model
  7. {
  8. public $amount;
  9. public $login;
  10. public $memberId;
  11. public $payType;
  12. public $notifyUrl;
  13. public $returnUrl;
  14. public $bankCode;
  15. public $ip;
  16. private $_outPayResult;
  17. private $_outNotifyResult;
  18. private $_outReturnResult;
  19. public static $payNames = [
  20. 1 => '爱农',
  21. 2 => '汇道',
  22. 3 => '',
  23. 4 => '华银',
  24. 5 => '奥凸',
  25. 6 => '汇道',
  26. ];
  27. /**
  28. * @return array
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['amount', 'login', 'memberId', 'payType', 'notifyUrl', 'returnUrl'], 'required'],
  34. ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  35. [['login', 'memberId', 'payType'], 'integer'],
  36. [['notifyUrl', 'returnUrl', 'ip'], 'string'],
  37. ['payType', 'in', 'range' => array_keys(self::$payNames)],
  38. ['bankCode', 'required', 'when' => function ($model) {
  39. /** @var PayForm $model */
  40. return in_array($model->payType, [4, 5, 6]);
  41. }, 'message' => '请选择银行'],
  42. ];
  43. }
  44. /**
  45. * @return bool
  46. */
  47. public function outPay()
  48. {
  49. if ($this->validate()) {
  50. $rate = RateHelper::getRate();
  51. if (empty($rate['sellRate'])) {
  52. $this->addError('amount', '汇率获取失败');
  53. return false;
  54. }
  55. $payType = $this->payType;
  56. $model = new Deposit();
  57. $model->type = 0;
  58. $model->member_id = $this->memberId;
  59. $model->amount = $this->amount;
  60. $model->rate = $rate['sellRate'];
  61. $model->rmb = number_format($this->amount * $model->rate, 2, '.', '');
  62. $model->in_time = intval(microtime(true) * 1000);
  63. $model->pay_name = isset(self::$payNames[$payType]) ? trim(self::$payNames[$payType]) : '';
  64. $model->login = $this->login;
  65. if ($model->save()) {
  66. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  67. if ($handler == null) {
  68. $this->addError('amount', '支付方式不存在');
  69. return false;
  70. }
  71. $handler->notifyUrl = $this->notifyUrl;
  72. $handler->returnUrl = $this->returnUrl;
  73. $params = [];
  74. $params['bankCode'] = $this->bankCode;
  75. $params['ip'] = $this->ip;
  76. $this->_outPayResult = $handler->outPay($model->getAttributes(), $params);
  77. }
  78. }
  79. return !$this->hasErrors();
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. public function getOutPayResult()
  85. {
  86. return $this->_outPayResult;
  87. }
  88. /**
  89. * @param array $data
  90. * @return bool
  91. */
  92. public function handleNotify($data)
  93. {
  94. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  95. unset($data['token']);
  96. unset($data['payType']);
  97. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  98. $success = false;
  99. if ($handler != null) {
  100. $success = $handler->handleNotify($data);
  101. $this->_outNotifyResult = $handler->outNotify($success);
  102. }
  103. return $success;
  104. }
  105. /**
  106. * @return mixed
  107. */
  108. public function getOutNotifyResult()
  109. {
  110. return $this->_outNotifyResult;
  111. }
  112. /**
  113. * @param array $data
  114. * @return bool
  115. */
  116. public function handleReturn($data)
  117. {
  118. $payType = isset($data['payType']) ? trim($data['payType']) : '';
  119. unset($data['token']);
  120. unset($data['payType']);
  121. $handler = BasePayHandler::getPayHandlerByPayType($payType);
  122. $success = false;
  123. if ($handler != null) {
  124. $success = $handler->handleReturn($data);
  125. $this->_outReturnResult = $handler->outReturn($success);
  126. }
  127. return $success;
  128. }
  129. /**
  130. * @return mixed
  131. */
  132. public function getOutReturnResult()
  133. {
  134. return $this->_outReturnResult;
  135. }
  136. }