post(); $key = date('YmdHis') . '_' . rand(1000, 9999); $channel = $payType == 10? 'wechat':'alipay'; if (empty($result)) { $result = file_get_contents('php://input'); $result = $result ? json_decode($result, true) : []; } try { RedisService::set("caches:payments:notify_{$scene}:result_".date('YmdHis').rand(10,99), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'result' => $result], 7200); $pay = PaymentService::make()->createPay($scene, $payType); if (empty($pay) || empty($scene) || empty($payType)) { if (env('APP_DEBUG')) { logger()->channel($channel)->error("【{$date} 支付回调】参数错误:" . json_encode(['scene' => $scene, 'payType' => $payType, 'data' => $result], 256)); } RedisService::set("caches:payments:notify_{$scene}:error_" . $key, ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'result' => $result], 7200); return response('failed'); } // 验签和重组参数 $data = $pay->callback(); RedisService::set("caches:payments:notify_{$scene}:data_" . $key, ['all' => request()->all(), 'scene' => $scene,'data'=>$data, 'payType' => $payType, 'result' => $result], 7200); if (env('APP_DEBUG')) { logger()->channel($channel)->info("【{$date} 支付回调】验证结果:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256)); } // 回调处理 if (PaymentService::make()->catchNotify($scene, $payType, $data)) { RedisService::set("caches:payments:notify_{$scene}:success_" . $key, ['all' => request()->all(), 'scene' => $scene,'data'=>$data, 'payType' => $payType, 'result' => $result], 7200); logger()->channel('alipay')->info("【{$date} 支付回调】回调成功:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256)); return response('success'); } else { $msg = PaymentService::make()->getError(); logger()->channel($channel)->info("【{$date} 支付回调】回调失败:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType, 'error' => lang($msg)], 256)); return message($msg ? $msg : 2635, false); } } catch (\Exception $exception) { RedisService::set("caches:payments:notify_{$scene}:error_" . $key, ['all' => request()->all(), 'scene' => $scene,'payType' => $payType,'error' => $exception->getMessage(), 'data' => $result], 7200); logger()->channel($channel)->error("【{$date} 支付回调】回调错误:" . json_encode(['data' => $result,'scene'=>$scene,'payType'=>$payType, 'error' => $exception->getMessage()], 256)); return message(2635, false, ['error' => $exception->getMessage()]); } } /** * 支付回调 * @param int $productId 产品 * @return array|false|\Psr\Http\Message\ResponseInterface */ public function payCallback($productId) { $date = date('Y-m-d H:i:s'); $result = isset($_POST)? $_POST : request()->post(); $key = date('YmdHis') . '_' . rand(1000, 9999); $channel = 'dryPay'; if (empty($result)) { $result = file_get_contents('php://input'); $result = $result ? json_decode($result, true) : []; } try { $sign = isset($result['sign'])?$result['sign']:''; unset($result['sign']);//删除掉sign字段 $checkSign = DyrPayService::make()->makeSign($result, 2); RedisService::set("caches:payments:payNotify_{$productId}:result_".date('YmdHis').rand(10,99), ['all' => request()->all(), 'sign'=>$sign,'checkSign'=>$checkSign, 'product_id' => $productId, 'result' => $result], 7200); if($sign != $checkSign){ if (env('APP_DEBUG')) { logger()->channel($channel)->error("【{$date} 支付回调】签名错误:" . json_encode([ 'product_id' => $productId,'data' => $result], 256)); } RedisService::set("caches:payments:payNotify_{$productId}:sign_" . $key, ['all' => request()->all(), 'product_id' => $productId, 'result' => $result], 7200); return response('failed'); } // 验签和重组参数 $data = $result; RedisService::set("caches:payments:notify_{$productId}:data_" . $key, ['all' => request()->all(),'product_id' => $productId, 'result' => $result], 7200); if (env('APP_DEBUG')) { logger()->channel($channel)->info("【{$date} 支付回调】验证结果:" . json_encode(['data' => $data,'product_id' => $productId],256)); } // 回调处理 if (PaymentService::make()->catchPayNotify($productId, $data)) { RedisService::set("caches:payments:notify_{$productId}:success_" . $key, ['all' => request()->all(), 'product_id' => $productId, 'result' => $result], 7200); logger()->channel('alipay')->info("【{$date} 支付回调】回调成功:" . json_encode(['data' => $data,'product_id' => $productId],256)); return response('success'); } else { $msg = PaymentService::make()->getError(); logger()->channel($channel)->info("【{$date} 支付回调】回调失败:" . json_encode(['data' => $data,'product_id' => $productId, 'error' => lang($msg)], 256)); return message($msg ? $msg : 2635, false); } } catch (\Exception $exception) { RedisService::set("caches:payments:notify_{$productId}:error_" . $key, ['all' => request()->all(), 'product_id' => $productId, 'error' => $exception->getMessage(), 'data' => $result], 7200); logger()->channel($channel)->error("【{$date} 支付回调】回调错误:" . json_encode(['data' => $result,'product_id' => $productId, 'error' => $exception->getMessage()], 256)); return message(2635, false, ['error' => $exception->getMessage()]); } } /** * 退款回调 * @param string $scene 场景,refund-退款 * @param int $payType 支付方式:10-微信,20-支付宝 * @return array|false * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\InvalidParamsException */ public function refund($payType = 10) { $scene = 'depositRefund'; $date = date('Y-m-d H:i:s'); $result = request()->post(); $pay = PaymentService::make()->createPay('refund', $payType); $channel = $payType == 10? 'wechat':'alipay'; if (empty($pay) || empty($scene) || empty($payType)) { if (env('APP_DEBUG')) { logger()->channel($channel)->error("【{$date} 退款回调】验证错误:" . json_encode(['scene' => $scene, 'payType' => $payType, 'data' => $result], 256)); } RedisService::set("caches:refunds:notify_error_" . date('YmdH'), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'data' => $result], 7200); return false; } // 验签和重组参数 $data = $pay->callback(); RedisService::set("caches:refunds:notify_data_" . date('YmdH'), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'data' => $result], 7200); if (env('APP_DEBUG')) { logger()->channel($channel)->info("【{$date} 退款回调】验证结果:" . json_encode($data, 256)); } // 回调处理 if (PaymentService::make()->catchNotify($scene, $payType, $data)) { logger()->channel($channel)->info("【{$date} 退款回调】回调成功:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256)); return response('success'); } else { $msg = PaymentService::make()->getError(); logger()->channel($channel)->info("【{$date} 退款回调】回调失败:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType, 'error' => lang($msg)], 256)); return message($msg ? $msg : 2635, false); } } }