<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace Modules\Wechat\Services;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use EasyWeChat\Pay\Application;
use Modules\Order\Services\OrderService;
use Modules\Store\Services\PaymentService;
use Modules\Order\Services\RefundService;

class NotifyService {

	public function orderNotify($request){
		$config = Config::get('wechat.miniapp');

		$app = new Application($config);

		$server = $app->getServer();
		// 处理支付结果事件
		$server->handlePaid(function ($message) {
			app(OrderService::class)->paySuccess($message['out_trade_no']);
		});

		return $server->serve();
	}

	public function paymentNotify($request){
		$config = Config::get('wechat.miniapp');

		$app = new Application($config);

		$server = $app->getServer();
		// 处理支付结果事件
		$server->handlePaid(function ($message) {
			app(PaymentService::class)->paySuccess($message['out_trade_no'], $message['transaction_id']);
		});

		return $server->serve();
	}

	public function refundNotify($request){
		$config = Config::get('wechat.miniapp');

		$app = new Application($config);
		$server = $app->getServer();
		// 处理退款结果事件
		$server->handleRefunded(function ($message) {
			app(RefundService::class)->refundSuccess($message);
		});

		return $server->serve();
	}
}