feat(split): 添加微信分账V3版本验证和汇付分账冻结资金处理 - 在ProfitSharingService中添加Yansongda Pay V3版本验证,确保微信官方分账功能正常运行 - 实现汇付分账冻结资金检测逻辑,当资金被冻结时标记分账失败而非抛出异常 - 在管理后台控制器中增强错误处理和状态显示功能 - 优化前端界面,根据分账模式动态显示相应操作按钮 ```
97 行
3.6 KiB
PHP
97 行
3.6 KiB
PHP
<?php
|
||
|
||
namespace addons\shopro\library\split;
|
||
|
||
use addons\shopro\model\Config as ShoproConfig;
|
||
use Yansongda\Pay\Pay;
|
||
use Yansongda\Pay\Plugin\Wechat\Fund\Profitsharing\AddReceiverPlugin;
|
||
use Yansongda\Pay\Plugin\Wechat\Fund\Profitsharing\CreatePlugin;
|
||
use Yansongda\Pay\Plugin\Wechat\Fund\Profitsharing\QueryPlugin;
|
||
use Yansongda\Pay\Plugin\Wechat\Fund\Profitsharing\ReturnPlugin;
|
||
|
||
class ProfitSharingService
|
||
{
|
||
protected $config = [];
|
||
|
||
public function __construct()
|
||
{
|
||
$this->config = $this->buildWechatConfig();
|
||
}
|
||
|
||
public function createOrder(array $params)
|
||
{
|
||
return $this->callPlugin(CreatePlugin::class, $params);
|
||
}
|
||
|
||
public function addReceiver(array $params)
|
||
{
|
||
return $this->callPlugin(AddReceiverPlugin::class, $params);
|
||
}
|
||
|
||
public function queryOrder(array $params)
|
||
{
|
||
return $this->callPlugin(QueryPlugin::class, $params);
|
||
}
|
||
|
||
public function returnOrder(array $params)
|
||
{
|
||
return $this->callPlugin(ReturnPlugin::class, $params);
|
||
}
|
||
|
||
protected function callPlugin($plugin, array $params)
|
||
{
|
||
$this->assertYansongdaPayV3();
|
||
$wechat = Pay::wechat($this->config);
|
||
if (!is_object($wechat) || !method_exists($wechat, 'mergeCommonPlugins')) {
|
||
throw new \RuntimeException(
|
||
'当前加载的是 Yansongda Pay V2,无法调用微信官方分账。'
|
||
. '纯汇付分账请勿使用「同步微信」;若需微信分账请将 epay 插件 API 版本改为 V3 并重新进入后台再试。'
|
||
);
|
||
}
|
||
$plugins = $wechat->mergeCommonPlugins([$plugin]);
|
||
return $wechat->pay($plugins, $params);
|
||
}
|
||
|
||
/**
|
||
* 微信官方分账(ProfitSharing)仅 epay 内置的 Yansongda V3 支持,与汇付 delaytrans/confirm 无关。
|
||
*/
|
||
protected function assertYansongdaPayV3()
|
||
{
|
||
if (!class_exists('\\addons\\epay\\library\\Service')) {
|
||
throw new \RuntimeException('请先安装并启用 epay(微信支付宝整合)插件');
|
||
}
|
||
if (!\addons\epay\library\Service::isVersionV3()) {
|
||
throw new \RuntimeException(
|
||
'epay 当前为 API V2,不支持微信官方分账接口。'
|
||
. '您若使用汇付延时分账:接收方在汇付控台绑定 huifu_id 即可,不要点「同步微信」。'
|
||
. '若业务必须用微信分账:在 epay 配置里改为 V3,并配置微信支付 APIv3 密钥与证书(与 V2 密钥不通用)。'
|
||
);
|
||
}
|
||
if (!class_exists(CreatePlugin::class)) {
|
||
throw new \RuntimeException('Yansongda Pay V3 类未加载,请确认 epay 为 V3 后刷新页面再试');
|
||
}
|
||
}
|
||
|
||
protected function buildWechatConfig()
|
||
{
|
||
$paymentConfig = json_decode(ShoproConfig::get(['name' => 'wechat'])->value, true);
|
||
$platformConfig = json_decode(ShoproConfig::get(['name' => 'wxMiniProgram'])->value, true);
|
||
|
||
if (isset($paymentConfig['mode']) && $paymentConfig['mode'] === 'service') {
|
||
$paymentConfig['sub_miniapp_id'] = $platformConfig['app_id'] ?? '';
|
||
} else {
|
||
$paymentConfig['miniapp_id'] = $platformConfig['app_id'] ?? '';
|
||
$paymentConfig['app_id'] = $platformConfig['app_id'] ?? '';
|
||
}
|
||
|
||
if (!empty($paymentConfig['cert_client'])) {
|
||
$paymentConfig['cert_client'] = ROOT_PATH . 'public' . $paymentConfig['cert_client'];
|
||
}
|
||
if (!empty($paymentConfig['cert_key'])) {
|
||
$paymentConfig['cert_key'] = ROOT_PATH . 'public' . $paymentConfig['cert_key'];
|
||
}
|
||
|
||
return $paymentConfig;
|
||
}
|
||
}
|