PayHandler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace common\pay\huidao;
  3. use backend\models\Deposit;
  4. use common\pay\BasePayHandler;
  5. use Yii;
  6. class PayHandler extends BasePayHandler
  7. {
  8. public $payUrl;
  9. public $pMid;
  10. public $secretKey;
  11. /**
  12. * @inheritdoc
  13. */
  14. public function init()
  15. {
  16. parent::init();
  17. if ($this->payUrl == null) {
  18. $this->payUrl = Yii::$app->params['huidao.payUrl'];
  19. }
  20. if ($this->pMid == null) {
  21. $this->pMid = Yii::$app->params['huidao.pMid'];
  22. }
  23. if ($this->secretKey == null) {
  24. $this->secretKey = Yii::$app->params['huidao.secretKey'];
  25. }
  26. }
  27. /**
  28. * @param array $deposit
  29. * @param array $params
  30. * @return string
  31. */
  32. public function outPay($deposit, $params = [])
  33. {
  34. $data = [];
  35. // 基本参数
  36. $data['p_version'] = '100'; // 版本号 100(固定值)
  37. $data['p_mid'] = $this->pMid; // 商户号
  38. $data['p_signType'] = '1'; // 签名类型 1:MD5
  39. // $data['p_hmac'] = ''; // 提交签名 不参与签名
  40. // 业务参数
  41. $data['item_transType'] = '1001'; // 交易类型 1001 (b2c 网银)
  42. $data['item_businessType'] = '100101'; // 业务类型 100101 消费
  43. $data['item_orderNo'] = 'SN' . $deposit['id']; // 商户订单号 10-30 位字母数字
  44. $data['item_submitTime'] = date('YmdHis'); // 提交时间 订单提交时间
  45. $data['item_currency'] = 'CNY'; // 订单币种 CNY(固定人民币)
  46. $data['item_orderAmount'] = number_format($deposit['rmb'], 2, '.', ''); // 订单金额 单位元, 保留 2 位小数
  47. // $data['item_bankCode'] = ''; // 银行代码 直连银行代码
  48. $data['item_pageUrl'] = $this->returnUrl; // 页面地址 页面返回 URL
  49. $data['item_notifyUrl'] = $this->notifyUrl; // 通知地址 后台通知 URL
  50. // $data['item_orderPeriod'] = ''; // 过期时间 单位分钟,默认 30 分
  51. // $data['item_additional'] = ''; // 附加信息 自定义信息
  52. // $data['item_productDesc'] = ''; // 产品描述 产品描述信息
  53. $data['p_hmac'] = static::makeSign($data, $this->secretKey);
  54. return static::createHtml($data, $this->payUrl);
  55. }
  56. /**
  57. * @param array $params
  58. * @param string $secret
  59. * @return string
  60. */
  61. public static function makeSign($params, $secret)
  62. {
  63. $signStr = '';
  64. ksort($params);
  65. foreach ($params as $key => $value) {
  66. if ($key == 'p_hmac' || $value === null || $value === '') {
  67. continue;
  68. }
  69. $value = strval($value); // 2.00 => "2.00"
  70. $signStr .= "{$key}={$value}&";
  71. }
  72. $signStr = rtrim($signStr, '&') . "&key={$secret}";
  73. return strtoupper(md5($signStr));
  74. }
  75. /**
  76. * @param array $data
  77. * @return bool
  78. */
  79. public function handleNotify($data)
  80. {
  81. $p_hmac = isset($data['p_hmac']) ? $data['p_hmac'] : '';
  82. if (isset($data['item_orderAmount'])) {
  83. $data['item_orderAmount'] = number_format($data['item_orderAmount'], 2, '.', '');
  84. }
  85. $makeSign = static::makeSign($data, $this->secretKey);
  86. if ($makeSign == $p_hmac) {
  87. if (isset($data['item_resultCode']) && $data['item_resultCode'] == '1001') {
  88. $merOrderId = trim($data['item_orderNo']);
  89. $merOrderId = str_ireplace("SN", "", $merOrderId);
  90. /** @var Deposit $model */
  91. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  92. if ($model) {
  93. // $model->type = 1;
  94. // $model->save(false);
  95. return true;
  96. }
  97. }
  98. }
  99. return false;
  100. }
  101. public function outNotify($success)
  102. {
  103. if ($success == true) {
  104. return "SUCCESS";
  105. } else {
  106. return "ERROR";
  107. }
  108. }
  109. /**
  110. * @param array $data
  111. * @return bool
  112. */
  113. public function handleReturn($data)
  114. {
  115. $p_hmac = isset($data['p_hmac']) ? $data['p_hmac'] : '';
  116. if (isset($data['item_orderAmount'])) {
  117. $data['item_orderAmount'] = number_format($data['item_orderAmount'], 2, '.', '');
  118. }
  119. $makeSign = static::makeSign($data, $this->secretKey);
  120. if ($makeSign == $p_hmac) {
  121. if (isset($data['item_resultCode']) && $data['item_resultCode'] == '1001') {
  122. $merOrderId = trim($data['item_orderNo']);
  123. $merOrderId = str_ireplace("SN", "", $merOrderId);
  124. /** @var Deposit $model */
  125. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  126. if ($model) {
  127. return true;
  128. }
  129. }
  130. }
  131. return false;
  132. }
  133. public function outReturn($success)
  134. {
  135. }
  136. }