| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace addons\JoinPay\api\controllers;
- use addons\JoinPay\common\global_func\payment_logic\JoinPay;
- use addons\JoinPay\common\global_func\payment_logic\JoinPayFastPay;
- use yii\web\Controller;
- use Yii;
- use Exception;
- use common\helpers\FileHelper;
- use common\helpers\ArrayHelper;
- use common\enums\PayTypeEnum;
- /**
- * 支付回调
- *
- * Class NotifyController
- * @package frontend\controllers
- */
- class NotifyController extends Controller
- {
- //use PayNotify;
- /**
- * 关闭csrf
- *
- * @var bool
- */
- public $enableCsrfValidation = false;
- /**
- * 公用支付回调 - 汇聚支付
- * @return string
- */
- public function actionJoinpay()
- {
- $params = \Yii::$app->request->get();
- $params = array_map('urldecode', $params);
- $logPath = $this->getLogPath('joinpay');
- $ab_url = \Yii::$app->request->absoluteUrl;
- FileHelper::writeLog($logPath, var_export($params, true) . PHP_EOL . $ab_url);
- try{
- $my_param = !empty($params['r5_Mp']) ? json_decode($params['r5_Mp'],true) : []; //json格式
- //获取对应支付配置
- $mall_id = $my_param['mall_id'] ?? 0;
- $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.joinpay', true);
- $config = json_decode($config, true);
- //调用支付类:回调方法
- // $pay = Yii::$app->payment->joinpay($config);
- $pay = new JoinPay($config);
- $message = $pay->notify();
- FileHelper::writeLog($logPath, var_export($message, true));
- if ($this->pay($message,PayTypeEnum::JOINPAY)) {
- die('success');
- }
- die('操作失败');
- }catch(\Exception $e){
- // 记录报错日志
- $logPath = $this->getLogPath('error');
- FileHelper::writeLog($logPath, $e->getMessage());
- die('操作失败'); // 通知响应
- }
- }
- /**
- * 公用支付回调 - 汇聚快捷支付
- * @return string
- */
- public function actionJoinpayFastpay()
- {
- try{
- $post = \Yii::$app->request->post();
- $logPath = $this->getLogPath('joinpay-fastpay');
- FileHelper::writeLog($logPath, json_encode($post,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
- if(empty($post) || empty($post['data'])){
- die('操作失败'); // 通知响应
- }
- $data = json_decode($post['data'],true);
- $callback_param = json_decode($data['callback_param'],true);
- $mall_id = $callback_param['mall_id'] ?? 0;
- //获取对应支付配置
- $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.joinpay_fast', true);
- $config = json_decode($config, true);
- // 这种不解耦的写法已废弃
- // $pay = Yii::$app->payment->joinpayFastPay($config);
- $pay = new JoinPayFastPay($config);
- $message = $pay->notify($post);
- if(!$message){
- die('操作失败');
- }
- if ($this->pay($message,PayTypeEnum::JOINPAY_FAST_PAY)) {
- die('success');
- }
- die('操作失败');
- }catch(\Exception $e){
- // 记录报错日志
- $logPath = $this->getLogPath('error');
- FileHelper::writeLog($logPath, $e->getMessage());
- die('操作失败'); // 通知响应
- }
- }
- /**
- * 支付公用方法
- * @param $message
- * @param int $pay_type 支付类型
- * @return bool
- */
- protected function pay($message,$pay_type)
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- if (!($payLog = Yii::$app->services->pay->getByTradeNo($message['out_trade_no']))) {
- throw new Exception('找不到支付信息');
- }
- // 支付完成
- if ($payLog->is_pay == 1) {
- return true;
- }
- //支付金额验证
- if(bccomp($message['pay_fee'], $payLog->getOnlinePayFee()) !== 0){
- throw new Exception('支付金额不一致');
- }
- $payLog->pay_type = $pay_type;
- $payLog->is_pay = 1;
- $payLog->pay_time = time();
- $payLog->third_trade_no = $message['trade_no'] ?? '';
- $payLog->second_trade_no = $message['r9_BankTrxNo'] ?? '';
- $payLog->trade_type = $message['trade_type'] ?? '';
- $logPath = $this->getLogPath('joinpay');
- FileHelper::writeLog($logPath, var_export(ArrayHelper::toArray($payLog), true));
- if (!$res = $payLog->save()) {
- throw new Exception('日志修改失败');
- }
- $transaction->commit();
- FileHelper::writeLog($logPath, 'update-res => ' . var_export($res, true));
- // 业务回调
- $orders = Yii::$app->services->pay->changePaymentTradeOrder($payLog,$pay_type);
- Yii::$app->services->pay->notify($orders);
- return true;
- } catch (\Exception $e) {
- $transaction->rollBack();
- //FileHelper::writeLog($this->getLogPath('error'), $e->getMessage());
- // 记录报错日志
- FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
- 'trace' => $e->getTrace(),
- 'code' => $e->getCode(),
- ])));
- return false;
- }
- }
- /**
- * @param $type
- * @return string
- */
- protected function getLogPath($type)
- {
- return Yii::getAlias('@runtime') . "/pay-logs/" . date('Y_m_d') . '/' . $type . '.txt';
- }
- }
|