PayForm.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace backend\models\forms;
  3. use backend\models\PayApi;
  4. use common\helpers\Utils;
  5. use Yii;
  6. use yii\base\Model;
  7. use yii\helpers\Url;
  8. class PayForm extends Model
  9. {
  10. public $login;
  11. public $amount;
  12. public $payType;
  13. public $bankCode;
  14. private $_outPayResult;
  15. public static $payNames = [
  16. 1 => '人人支付',
  17. 2 => '网通支付',
  18. 3 => '杉德支付',
  19. 4 => '顺通支付',
  20. 5 => '3xmta支付',
  21. 6 => '迅捷支付',
  22. 7 => 'Ctype支付',
  23. 8 => '科星支付',
  24. ];
  25. public static $payMethods = [
  26. 'renren' => 1,
  27. 'wangtong' => 2,
  28. 'sand' => 3,
  29. 'shangxinshuntong' => 4,
  30. 'threexmta' => 5,
  31. 'xunjie' => 6,
  32. 'ctype' => 7,
  33. 'kexing' => 8,
  34. ];
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['login', 'amount', 'payType'], 'required'],
  42. ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  43. ['payType', 'integer'],
  44. ['payType', 'in', 'range' => array_keys(self::$payNames)],
  45. ['bankCode', 'required', 'when' => function ($model) {
  46. /** @var PayForm $model */
  47. return in_array($model->payType, [1,3,8]);
  48. }, 'message' => '请选择银行'],
  49. ];
  50. }
  51. /**
  52. * @return bool
  53. */
  54. public function outPay()
  55. {
  56. if (Yii::$app->getUser()->getIsGuest()) {
  57. $this->addError('login', '用户未登录');
  58. Yii::$app->getUser()->loginRequired();
  59. return false;
  60. }
  61. if ($this->validate()) {
  62. $data = [];
  63. $data['orderSn'] = date('YmdHis').rand(1000,9999);
  64. $data['amount'] = $this->amount;
  65. $data['login'] = $this->login;
  66. $data['payType'] = $this->payType;
  67. $data['bankCode'] = $this->bankCode;
  68. $data['memberId'] = Yii::$app->getUser()->getId();
  69. $data['notifyUrl'] = Url::to(["/pay/notify/{$this->payType}"], true);
  70. $data['returnUrl'] = Url::to(["/pay/return/{$this->payType}"], true);
  71. $data['ip'] = Utils::getClientIp();
  72. $api = new PayApi();
  73. $result = $api->outPay($data);
  74. if ($result['code'] == 1) {
  75. $this->_outPayResult = $result['data'];
  76. } else {
  77. if (is_array($result['message'])) {
  78. $this->addErrors($result['message']);
  79. } else {
  80. $this->addError('amount', $result['message']);
  81. }
  82. }
  83. }
  84. return !$this->hasErrors();
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function getOutPayResult()
  90. {
  91. return $this->_outPayResult;
  92. }
  93. /**
  94. * @param string $payMethod
  95. * @return mixed|null
  96. */
  97. public function getPayTypeByMethod($payMethod)
  98. {
  99. return isset(self::$payMethods[$payMethod]) ? self::$payMethods[$payMethod] : null;
  100. }
  101. }