setWebsite(__NAMESPACE__); } public function alipay() { Log::info('支付宝支付---通知开始'); //1. 设置参数(全局只需设置一次) Alipay::setOptions($this->getOptions()); Log::info('支付宝私钥', $this->getOptions()); //2. 发起API调用 $parameters = Request::all(); $ress = Alipay::payment()->common()->verifyNotify($parameters); Log::info('支付宝支付---params=', $ress); try { if (Alipay::payment()->common()->verifyNotify($parameters)) { //更新订单 $orderData = [ 'order_status' => 2, 'pay_amount_total' => $parameters['total_amount'], //实际付款金额 支付接口,返回的支付金额,仅做参考 'pay_channel' => 3, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝 'out_trade_no' => $parameters['out_trade_no'], //第三方支付流水号 'pay_time' => time(), //付款时间 'pay_info' => json_encode($parameters), ]; return $this->updateOrder($orderData['out_trade_no'], $orderData); } else { Log::info('支付宝支付---失败,签名验证失败!'); return 'fail'; } } catch (\Exception $e) { Log::info('支付宝支付---失败!' . $e->getMessage()); return 'fail'; } } /** * 微信公众号-支付成功后的回调 */ public function wechatpay() { Log::info('微信支付---通知开始'); $config = config('easywechat.pay.default'); $app = Factory::payment($config); try { $response = $app->handlePaidNotify(function ($message, $fail) { //判断该订单是否存在 if ($message['result_code'] == 'SUCCESS' && $message['return_code'] == 'SUCCESS') { //更新订单 $orderData = [ 'order_status' => 2, 'pay_amount_total' => $message['total_fee'], //实际付款金额 支付接口,返回的支付金额,仅做参考 'pay_channel' => 2, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝 'out_trade_no' => $message['transaction_id'], //第三方支付流水号 'pay_time' => time(), //付款时间 'pay_info' => json_encode($message), ]; return $this->updateOrder($message['out_trade_no'], $orderData); } else { return $fail('通信失败,请稍后再通知我'); } }); return $response->send(); } catch (Exception $e) { Log::error('微信支付---支付失败 ==' . $e->getMessage()); return 'failed'; } } /** * 微信小程序-支付成功后的回调 */ public function wxpay() { Log::info('微信小程序支付回调开始了!'); $config = config('wechat.payment.default'); $app = Factory::payment($config); $response = $app->handlePaidNotify(function ($message, $fail) { //判断该订单是否存在 if ($message['result_code'] == 'SUCCESS' && $message['return_code'] == 'SUCCESS') { Log::info('微信小程序支付回调成功!', $message); //更新订单 $orderData = [ 'order_status' => 2, 'pay_amount_total' => $message['total_fee'], //实际付款金额 支付接口,返回的支付金额,仅做参考 'pay_channel' => 1, //订单支付渠道 1 微信小程序 2 微信公众号 3支付宝 'out_trade_no' => $message['transaction_id'], //第三方支付流水号 'pay_time' => time(), //付款时间 'pay_info' => json_encode($message), ]; return $this->updateOrder($message['out_trade_no'], $orderData); } else { Log::error(__FUNCTION__ . '微信小程序支付回调失败!', []); return $fail('通信失败,请稍后再通知我'); } }); return $response->send(); } ############################################################################################################ ############################################################################################################ ############################################################################################################ /** * @param $out_trade_no * @param $orderData * @return string|null */ private function updateOrder($out_trade_no, $orderData): ?string { $logic_sign = substr($out_trade_no, 0, 2); switch ($logic_sign) { case 'GO': // 服务商品订单 return $this->updateOrderTable($out_trade_no, $orderData); case 'CO': // 服务套餐订单 return $this->updateComboOrder($out_trade_no, $orderData); default: Log::error("======= 产品类型对应的订单服务不存在 ========"); return null; } } /** * @param $out_trade_no * @param $orderData * @return string */ private function updateOrderTable($out_trade_no, $orderData): string { $orderInfo = DB::table('order_product')->where('order_no', $out_trade_no)->where("order_status", 1)->first(); if ($orderInfo) { //更新订单状态信息 $is_success = DB::transaction(function () use ($orderInfo, $orderData) { $is_success = DB::table('order_product')->where('id', $orderInfo->id)->update($orderData); if ($is_success) { // 更新产品 $this->updateUserProduct($orderInfo->user_id, $orderInfo->product_id, $orderInfo->product_sku); Log::info('回调-服务商品订单支付成功!'); return true; } else { Log::info('回调-电商订单支付失败!'); return false; } }); if ($is_success) { return 'success'; } else { return 'fail'; } } else { Log::info('回调-订单不存在!-订单号:' . $out_trade_no); return 'fail'; } } /** * @param $out_trade_no * @param $orderData * @return string */ private function updateComboOrder($out_trade_no, $orderData): string { $orderInfo = DB::table('order_combo')->where('order_no', $out_trade_no)->where("order_status", 1)->first(); if ($orderInfo) { // 有效期 $orderData['expiry_time'] = time() + $orderInfo->expiry_time * 24 * 3600; //更新订单信息 $is_success = DB::transaction(function () use ($orderInfo, $orderData) { $is_success = DB::table('order_combo')->where('id', $orderInfo['id'])->update($orderData); if ($is_success) { $productIds = $orderInfo['product_ids']; foreach ($productIds as $productId) { // 更新产品 $this->updateUserProduct($orderInfo['user_id'], $productId, 4); } Log::info('回调-服务商品订单支付成功!'); return true; } else { Log::info('回调-电商订单支付失败!'); return false; } }); if ($is_success) { return 'success'; } else { return 'fail'; } } else { Log::info('回调-订单不存在!-订单号:' . $out_trade_no); return 'fail'; } } /** * @param $userId * @param $productId * @param $product_sku * @return void */ private function updateUserProduct($userId, $productId, $product_sku): void { $validity_type = 1; if ($product_sku == 1) { // 月价格 $endTime = 93 * 24 * 3600; } elseif ($product_sku == 2) { // 半年价格 $endTime = 183 * 24 * 3600; } elseif ($product_sku == 3) { // 一年价格 $endTime = 365 * 24 * 3600; } elseif ($product_sku == 4) { // 永久有效 $validity_type = 2; $endTime = 0; } else if ($product_sku == 5) { // 7 天有效 $endTime = 7 * 24 * 3600; } else { $endTime = 0; } // 添加商品 $isExist = DB::table('user_buy_bill')->where(['user_id' => $userId, 'product_id' => $productId])->first(); if ($isExist) { // validity_type 1 时间限制 2 永久有效 if ($isExist->validity_type == 2) { // 如果原来已经是永久会员,则不变 // $validity_type = 2; // $endTime = 0; } else { $validity_end_time = $isExist->validity_end_time; if ($validity_end_time < time()) { $validity_end_time = time(); } $endTime = $endTime + $validity_end_time; DB::table('user_buy_bill')->where('id', $isExist->id)->update(['validity_type' => $validity_type, 'validity_end_time' => $endTime]); } } else { if ($validity_type == 1) { $endTime = $endTime + time(); } else { $endTime = 0; } DB::table('user_buy_bill')->insert([ 'user_id' => $userId, 'product_id' => $productId, 'validity_type' => $validity_type, 'validity_end_time' => $endTime, 'mid' => Str::random(12), 'created_at' => time(), 'updated_at' => time(), ]); } } }