PayForm.php 3.4 KB

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