PayForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 9 => '惠盈支付',
  25. 10 => '创汇支付',
  26. 11 => '农直通支付',
  27. ];
  28. public static $payMethods = [
  29. 'renren' => 1,
  30. 'wangtong' => 2,
  31. 'sand' => 3,
  32. 'shangxinshuntong' => 4,
  33. 'threexmta' => 5,
  34. 'xunjie' => 6,
  35. 'ctype' => 7,
  36. 'kexing' => 8,
  37. 'huiying' => 9,
  38. 'chuanghui' =>10,
  39. 'nongzhitong' =>11,
  40. ];
  41. /**
  42. * @inheritdoc
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['login', 'amount', 'payType'], 'required'],
  48. ['amount', 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  49. ['payType', 'integer'],
  50. ['payType', 'in', 'range' => array_keys(self::$payNames)],
  51. ['bankCode', 'required', 'when' => function ($model) {
  52. /** @var PayForm $model */
  53. return in_array($model->payType, [1,3,8,9,10,11]);
  54. }, 'message' => '请选择银行'],
  55. ];
  56. }
  57. /**
  58. * @return bool
  59. */
  60. public function outPay()
  61. {
  62. if (Yii::$app->getUser()->getIsGuest()) {
  63. $this->addError('login', '用户未登录');
  64. Yii::$app->getUser()->loginRequired();
  65. return false;
  66. }
  67. if ($this->validate()) {
  68. $data = [];
  69. $data['orderSn'] = date('YmdHis').rand(1000,9999);
  70. $data['amount'] = $this->amount;
  71. $data['login'] = $this->login;
  72. $data['payType'] = $this->payType;
  73. $data['bankCode'] = $this->bankCode;
  74. $data['memberId'] = Yii::$app->getUser()->getId();
  75. $data['notifyUrl'] = Url::to(["/pay/notify/{$this->payType}"], true);
  76. $data['returnUrl'] = Url::to(["/pay/return/{$this->payType}"], true);
  77. $data['ip'] = Utils::getClientIp();
  78. $api = new PayApi();
  79. $result = $api->outPay($data);
  80. if ($result['code'] == 1) {
  81. $this->_outPayResult = $result['data'];
  82. } else {
  83. if (is_array($result['message'])) {
  84. $this->addErrors($result['message']);
  85. } else {
  86. $this->addError('amount', $result['message']);
  87. }
  88. }
  89. }
  90. return !$this->hasErrors();
  91. }
  92. /**
  93. * @return string
  94. */
  95. public function getOutPayResult()
  96. {
  97. return $this->_outPayResult;
  98. }
  99. /**
  100. * @param string $payMethod
  101. * @return mixed|null
  102. */
  103. public function getPayTypeByMethod($payMethod)
  104. {
  105. return isset(self::$payMethods[$payMethod]) ? self::$payMethods[$payMethod] : null;
  106. }
  107. }