| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace common\pay\globalpay;
- use backend\models\Config;
- use backend\models\Deposit;
- use backend\models\SyncDesposit;
- use common\pay\BasePayHandler;
- use Yii;
- use yii\helpers\VarDumper;
- class PayHandler extends BasePayHandler
- {
- public $payUrl;
- public $confirmUrl;
- public $merId;
- public $Key;
- /**
- * @inheritdoc
- */
- public function init()
- {
- parent::init();
- if ($this->payUrl == null) {
- $this->payUrl = Yii::$app->params['globalpay.payUrl'];
- }
- if ($this->confirmUrl == null) {
- $this->confirmUrl = Yii::$app->params['globalpay.confirmUrl'];
- }
- if ($this->merId == null) {
- $this->merId = Yii::$app->params['globalpay.merId'];
- }
- if ($this->Key == null) {
- $this->Key = Yii::$app->params['globalpay.Key'];
- }
- }
- /**
- * @param array $deposit
- * @param array $params
- * @return string
- */
- public function outPay($deposit, $params = [])
- {
- $data = [];
-
- $data['mchNo'] = $this->merId; //商户号码
- $data['customerNo'] = $params['login']; //商户平台用户唯一标识
- $data['orderNo'] = $deposit['order_sn']; //订单号码
- $data['amount'] = $deposit['rmb'] * 100; //订单金额
- $data['currency'] = 'CNY'; //币种
- $data['returnUrl'] = $this->returnUrl; //支付完成返回地址(这个是必须有的)
- $data['notifyUrl'] = $this->notifyUrl; //异步回调地址
- $data['payType'] = 1; //支付类型
- $data['signType'] = 'MD5'; //签名类型
- $data['sign'] = PayUtils::makeSign($data,$this->Key);
- $result = PayUtils::send($data, $this->payUrl);
- if (isset($result["code"]) && $result["code"] == 200) {
- $order_id = $result["data"]["orderId"];
- $result['pay_url'] = $this->confirmUrl.$order_id;
- return $result;
- }else{
- return $result;
- }
-
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleNotify($data)
- {
-
- $tt = print_r($data,true);
- file_put_contents('globalpay_data.txt',$tt);
- if (isset($data['sign']) && trim($data['sign']) !== '') {
-
- if (PayUtils::verify($data, $this->Key)) {
- file_put_contents('globalpay_success.txt','验签成功');
- $merOrderId = trim($data['orderNo']);
- $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
- if ($reuslt['type'] != 1) {
- $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
- $configData = Config::find()->asArray()->one();
- if ($configData['auto_deposit'] == 1 && $res) {
- $syncDespositModel = new SyncDesposit();
- $syncDespositModel->login = $reuslt['login'];
- $syncDespositModel->amount = $reuslt['amount'];
- $syncDespositModel->comment = 'Deposit';
- $syncDespositModel->memo = $merOrderId; //存储入金表的订单id
- $syncDespositModel->type = 2;
- $syncDespositModel->in_time = time();
- $syncDespositModel->save();
- }
- return success;
- }
-
- }
- }
- return false;
- }
- public function outNotify($success)
- {
- if ($success == true) {
- return '{"code":200}';
- } else {
- // 返回的数据格式
- return "FAIL";
- }
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleReturn($data)
- {
- //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
- if (isset($data['sign']) && trim($data['sign']) !== '') {
- if (PayUtils::verify($data, $this->publicKey)) {
- if ($data['status'] == '1') {
- $merOrderId = trim($data['orderNo']);
- $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
- if ($reuslt) {
- return success;
- }
- }
- }
- }
- return false;
- }
- public function outReturn($success)
- {
- }
- }
|