| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace common\pay\huidao;
- use backend\models\Deposit;
- use common\pay\BasePayHandler;
- use Yii;
- class PayHandler extends BasePayHandler
- {
- public $payUrl;
- public $pMid;
- public $secretKey;
- /**
- * @inheritdoc
- */
- public function init()
- {
- parent::init();
- if ($this->payUrl == null) {
- $this->payUrl = Yii::$app->params['huidao.payUrl'];
- }
- if ($this->pMid == null) {
- $this->pMid = Yii::$app->params['huidao.pMid'];
- }
- if ($this->secretKey == null) {
- $this->secretKey = Yii::$app->params['huidao.secretKey'];
- }
- }
- /**
- * @param array $deposit
- * @param array $params
- * @return string
- */
- public function outPay($deposit, $params = [])
- {
- $data = [];
- // 基本参数
- $data['p_version'] = '100'; // 版本号 100(固定值)
- $data['p_mid'] = $this->pMid; // 商户号
- $data['p_signType'] = '1'; // 签名类型 1:MD5
- // $data['p_hmac'] = ''; // 提交签名 不参与签名
- // 业务参数
- $data['item_transType'] = '1001'; // 交易类型 1001 (b2c 网银)
- $data['item_businessType'] = '100101'; // 业务类型 100101 消费
- $data['item_orderNo'] = 'SN' . $deposit['id']; // 商户订单号 10-30 位字母数字
- $data['item_submitTime'] = date('YmdHis'); // 提交时间 订单提交时间
- $data['item_currency'] = 'CNY'; // 订单币种 CNY(固定人民币)
- $data['item_orderAmount'] = number_format($deposit['rmb'], 2, '.', ''); // 订单金额 单位元, 保留 2 位小数
- // $data['item_bankCode'] = ''; // 银行代码 直连银行代码
- $data['item_pageUrl'] = $this->returnUrl; // 页面地址 页面返回 URL
- $data['item_notifyUrl'] = $this->notifyUrl; // 通知地址 后台通知 URL
- // $data['item_orderPeriod'] = ''; // 过期时间 单位分钟,默认 30 分
- // $data['item_additional'] = ''; // 附加信息 自定义信息
- // $data['item_productDesc'] = ''; // 产品描述 产品描述信息
- $data['p_hmac'] = static::makeSign($data, $this->secretKey);
- return static::createHtml($data, $this->payUrl);
- }
- /**
- * @param array $params
- * @param string $secret
- * @return string
- */
- public static function makeSign($params, $secret)
- {
- $signStr = '';
- ksort($params);
- foreach ($params as $key => $value) {
- if ($key == 'p_hmac' || $value === null || $value === '') {
- continue;
- }
- $value = strval($value); // 2.00 => "2.00"
- $signStr .= "{$key}={$value}&";
- }
- $signStr = rtrim($signStr, '&') . "&key={$secret}";
- return strtoupper(md5($signStr));
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleNotify($data)
- {
- $p_hmac = isset($data['p_hmac']) ? $data['p_hmac'] : '';
- if (isset($data['item_orderAmount'])) {
- $data['item_orderAmount'] = number_format($data['item_orderAmount'], 2, '.', '');
- }
- $makeSign = static::makeSign($data, $this->secretKey);
- if ($makeSign == $p_hmac) {
- if (isset($data['item_resultCode']) && $data['item_resultCode'] == '1001') {
- $merOrderId = trim($data['item_orderNo']);
- $merOrderId = str_ireplace("SN", "", $merOrderId);
- /** @var Deposit $model */
- $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
- if ($model) {
- // $model->type = 1;
- // $model->save(false);
- return true;
- }
- }
- }
- return false;
- }
- public function outNotify($success)
- {
- if ($success == true) {
- return "SUCCESS";
- } else {
- return "ERROR";
- }
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleReturn($data)
- {
- $p_hmac = isset($data['p_hmac']) ? $data['p_hmac'] : '';
- if (isset($data['item_orderAmount'])) {
- $data['item_orderAmount'] = number_format($data['item_orderAmount'], 2, '.', '');
- }
- $makeSign = static::makeSign($data, $this->secretKey);
- if ($makeSign == $p_hmac) {
- if (isset($data['item_resultCode']) && $data['item_resultCode'] == '1001') {
- $merOrderId = trim($data['item_orderNo']);
- $merOrderId = str_ireplace("SN", "", $merOrderId);
- /** @var Deposit $model */
- $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
- if ($model) {
- return true;
- }
- }
- }
- return false;
- }
- public function outReturn($success)
- {
- }
- }
|