123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace App\Http\Api;
- use Alipay\EasySDK\Kernel\Factory as Alipay;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\Exception;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Str;
- class NotifyUrlController extends HttpBaseController
- {
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- }
- public function alipay()
- {
- Log::info('支付宝支付---通知开始');
- //1. 设置参数(全局只需设置一次)
- Alipay::setOptions($this->getOptions());
- //2. 发起API调用
- $parameters = Request::all();
- 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 = 90 * 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(),
- ]);
- }
- }
- }
|