PayForm.php 3.2 KB

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