PayForm.php 3.3 KB

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