feature/main20240421
parent
1694b5b63b
commit
b8c356c089
@ -0,0 +1,182 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare (strict_types=1); |
||||||
|
|
||||||
|
namespace app\command; |
||||||
|
|
||||||
|
use think\console\Command; |
||||||
|
use think\console\Output; |
||||||
|
use think\console\Input; |
||||||
|
use think\facade\Db; |
||||||
|
use app\common\model\Order; |
||||||
|
use app\common\model\Store; |
||||||
|
use app\common\enum\payment\Method as PaymentMethodEnum; |
||||||
|
use app\common\model\MerchantPay as merchantPayModel; |
||||||
|
use app\common\model\Merchant as merchantModel; |
||||||
|
use EasyWeChat\Factory; |
||||||
|
use app\common\model\PaymentTemplate; |
||||||
|
use app\common\model\Payment; |
||||||
|
use app\common\model\PaymentTrade; |
||||||
|
use app\common\model\wxapp\Setting; |
||||||
|
|
||||||
|
|
||||||
|
// /www/server/php/74/bin/php /server/wwwroot/yanzong/think test |
||||||
|
class ProfitSharing extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
// 指令配置 |
||||||
|
$this->setName('ProfitSharing')->setDescription('自动分账'); |
||||||
|
$this->addArgument("store_id"); |
||||||
|
$this->addArgument("div"); |
||||||
|
$this->addArgument("mod"); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(Input $input, Output $output) |
||||||
|
{ |
||||||
|
$store_id = $input->getArgument("store_id"); |
||||||
|
$div = $input->getArgument("div"); |
||||||
|
$mod = $input->getArgument("mod"); |
||||||
|
$limit = 100; |
||||||
|
while (TRUE) { |
||||||
|
echo $store_id.PHP_EOL; |
||||||
|
$sql = "SELECT store_id FROM `yoshop_store` WHERE store_id > " . $store_id . " AND store_id % ". $div . " = ". $mod ." AND store_version = 1 AND is_delete = 0 AND is_recycle = 0 AND status = 1 order by store_id asc LIMIT ".$limit; |
||||||
|
$stores = Db::query($sql); |
||||||
|
if (!$stores) { |
||||||
|
echo "没有多商户商城了".PHP_EOL; |
||||||
|
return false; |
||||||
|
} |
||||||
|
var_dump($stores); |
||||||
|
foreach ($stores as $store) { |
||||||
|
$where = []; |
||||||
|
//查询已完成的订单,并且未分账的订单 |
||||||
|
$where[] = ['order_status','=', 30]; |
||||||
|
$where[] = ['pay_status','=', 20]; |
||||||
|
$where[] = ['is_refund','=', 10]; |
||||||
|
$where[] = ['is_delete','=', 0]; |
||||||
|
$where[] = ['profitsharing_status','=', 0]; |
||||||
|
$where[] = ['store_id','=', $store['store_id']]; |
||||||
|
$where[] = ['merchant_id','>', 0]; |
||||||
|
$orders = Order::where($where) |
||||||
|
->field('order_id,total_price,order_price,pay_price,pay_method,cost_price,merchant_id,store_id,order_status,pay_status,delivery_status,receipt_status,delivery_type,delivery_time,create_time') |
||||||
|
->select(); |
||||||
|
// var_dump($orders->toArray()); |
||||||
|
// exit(); |
||||||
|
if ($orders->isEmpty()) { |
||||||
|
echo $store['store_id']."没有已完成的订单要分账".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
//微信支付订单抽佣给平台、余额支付抽佣给平台,把订单金额记录到商户账上 |
||||||
|
foreach ($orders as $order) { |
||||||
|
//余额支付 |
||||||
|
if ($order->pay_method == PaymentMethodEnum::BALANCE) { |
||||||
|
//增加商户支付详情 |
||||||
|
$model = \app\store\model\Merchant::detail($order->merchant_id, $order->store_id); |
||||||
|
$precent = 1; |
||||||
|
if ($model['commission_ratio'] > 0) { |
||||||
|
$precent = (1 - $model['commission_ratio'] / 1000); |
||||||
|
} |
||||||
|
$precentPrice = round($precent * $order->pay_price, 2); |
||||||
|
(new merchantPayModel())->addDetail($order, $precentPrice); |
||||||
|
|
||||||
|
//累计商户余额支付金额 |
||||||
|
merchantModel::setIncTotal($order->merchant_id, $precentPrice); |
||||||
|
|
||||||
|
//更新 |
||||||
|
$ret = Order::where('order_id',$order->order_id)->update(['profitsharing_status' => 2, 'profitsharing_time' => time()]); |
||||||
|
echo "余额支付分账成功".PHP_EOL; |
||||||
|
var_dump($ret); |
||||||
|
} elseif($order->pay_method == PaymentMethodEnum::WECHAT){//微信支付 |
||||||
|
//商户微信支付配置 |
||||||
|
$payment = Payment::where('store_id', $order->store_id)->where('merchant_id', $order->merchant_id)->where('method',PaymentMethodEnum::WECHAT)->where('is_enable', 1)->find(); |
||||||
|
if (!$payment) { |
||||||
|
echo $store['store_id']."微信支付方式没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$payment_template = PaymentTemplate::where('template_id', $payment->template_id)->where('is_delete', 1)->find(); |
||||||
|
if (!$payment_template) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$wechat_config = $payment_template['config'] ? json_decode($payment_template['config'], true) : []; |
||||||
|
if (!$wechat_config) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$wechat_config = $wechat_config['wechat']['normal'] ?? []; |
||||||
|
//小程序配置 |
||||||
|
$mini = Setting::where('store_id', $order->store_id)->find(); |
||||||
|
if (!$mini) { |
||||||
|
echo $store['store_id']."小程序配置没有".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$mini_config = $mini['values'] ? json_decode($mini['config'], true) : []; |
||||||
|
if (!$mini_config) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
//分账 |
||||||
|
$config = [ |
||||||
|
'app_id' => $mini_config['app_id'], |
||||||
|
"secret" => $mini_config['app_secret'], |
||||||
|
'mch_id' => $wechat_config['mchId'], |
||||||
|
'key' => $wechat_config['apiKey'], |
||||||
|
'cert_path' => PaymentTemplateModel::realPathCertFile(PaymentMethodEnum::WECHAT, $wechat_config['apiclientCert'], $order->store_id), |
||||||
|
'key_path' => PaymentTemplateModel::realPathCertFile(PaymentMethodEnum::WECHAT, $wechat_config['apiclientKey'], $order->store_id), |
||||||
|
'notify_url' => '', |
||||||
|
]; |
||||||
|
$payment = Factory::payment($config); |
||||||
|
|
||||||
|
//平台微信支付配置信息 |
||||||
|
$platform_payment = Payment::where('store_id', $order->store_id)->where('merchant_id', 0)->where('method',PaymentMethodEnum::WECHAT)->where('is_enable', 1)->find(); |
||||||
|
if (!$platform_payment) { |
||||||
|
echo $store['store_id']."微信支付方式没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$platform_payment_template = PaymentTemplate::where('template_id', $platform_payment->template_id)->where('is_delete', 1)->find(); |
||||||
|
if (!$platform_payment_template) { |
||||||
|
echo $store['store_id'].$platform_payment->template_id."微信支付模版没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$platform_wechat_config = $platform_payment_template['config'] ? json_decode($payment_template['config'], true) : []; |
||||||
|
if (!$platform_wechat_config) { |
||||||
|
echo $store['store_id'].$platform_payment->template_id."微信支付模版没有配置11".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$platform_wechat_config = $platform_wechat_config['wechat']['normal'] ?? []; |
||||||
|
|
||||||
|
|
||||||
|
$payment_trade = PaymentTrade::where('trade_id', $order->trade_id)->field('trade_no')->find(); |
||||||
|
if (!$payment_trade) { |
||||||
|
echo $store['store_id'].$platform_payment->template_id."没有交易流水号".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$transaction_id = $payment_trade->trade_no; |
||||||
|
$out_trade_no = "PS".date("YmdHis").mt_rand(1000,9999); |
||||||
|
$receivers = [ |
||||||
|
[ |
||||||
|
"type" => "MERCHANT_ID", |
||||||
|
"account" => $platform_wechat_config['mchId'], |
||||||
|
"amount" => 1, |
||||||
|
"description" => "分到商户" |
||||||
|
] |
||||||
|
]; |
||||||
|
$sharing = $payment->profit_sharing->share($transaction_id, $out_trade_no, $receivers); |
||||||
|
var_dump($sharing); |
||||||
|
var_dump($sharing->return_code); |
||||||
|
var_dump($sharing->result_code); |
||||||
|
//更新 |
||||||
|
$ret = Order::where('order_id',$order->order_id)->update(['profitsharing_status' => 1, 'profitsharing_time' => time(),'out_order_no' => $out_trade_no]); |
||||||
|
echo "微信支付分账中".PHP_EOL; |
||||||
|
var_dump($ret); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
$store_id = end($stores)['store_id']; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,125 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare (strict_types=1); |
||||||
|
|
||||||
|
namespace app\command; |
||||||
|
|
||||||
|
use think\console\Command; |
||||||
|
use think\console\Output; |
||||||
|
use think\console\Input; |
||||||
|
use think\facade\Db; |
||||||
|
use app\common\model\Order; |
||||||
|
use app\common\model\Store; |
||||||
|
use app\common\enum\payment\Method as PaymentMethodEnum; |
||||||
|
use app\common\model\MerchantPay as merchantPayModel; |
||||||
|
use app\common\model\Merchant as merchantModel; |
||||||
|
use EasyWeChat\Factory; |
||||||
|
use app\common\model\PaymentTemplate; |
||||||
|
use app\common\model\Payment; |
||||||
|
use app\common\model\PaymentTrade; |
||||||
|
use app\common\model\wxapp\Setting; |
||||||
|
|
||||||
|
|
||||||
|
// /www/server/php/74/bin/php /server/wwwroot/yanzong/think test |
||||||
|
class ProfitSharingResult extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
// 指令配置 |
||||||
|
$this->setName('ProfitSharingResult')->setDescription('自动分账'); |
||||||
|
$this->addArgument("order_id"); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(Input $input, Output $output) |
||||||
|
{ |
||||||
|
$order_id = $input->getArgument("order_id"); |
||||||
|
if ($order_id) { |
||||||
|
$where[] = ['order_id','=', $order_id]; |
||||||
|
} else { |
||||||
|
//查询已完成的订单,并且未分账的订单 |
||||||
|
$where[] = ['order_status','=', 30]; |
||||||
|
$where[] = ['pay_status','=', 20]; |
||||||
|
$where[] = ['is_refund','=', 10]; |
||||||
|
$where[] = ['is_delete','=', 0]; |
||||||
|
$where[] = ['profitsharing_status','=', 1]; |
||||||
|
$where[] = ['merchant_id','>', 0]; |
||||||
|
$where[] = ['method','=', PaymentMethodEnum::WECHAT]; |
||||||
|
} |
||||||
|
|
||||||
|
$orders = Order::where($where) |
||||||
|
->field('order_id,total_price,order_price,pay_price,pay_method,cost_price,merchant_id,store_id,order_status,pay_status,delivery_status,receipt_status,delivery_type,delivery_time,create_time,out_order_no') |
||||||
|
->select(); |
||||||
|
// var_dump($orders->toArray()); |
||||||
|
// exit(); |
||||||
|
if ($orders->isEmpty()) { |
||||||
|
echo $store['store_id']."没有已完成的订单要分账".PHP_EOL; |
||||||
|
return false; |
||||||
|
} |
||||||
|
//微信支付订单抽佣给平台、余额支付抽佣给平台,把订单金额记录到商户账上 |
||||||
|
foreach ($orders as $order) { |
||||||
|
|
||||||
|
//商户微信支付配置 |
||||||
|
$payment = Payment::where('store_id', $order->store_id)->where('merchant_id', $order->merchant_id)->where('method',PaymentMethodEnum::WECHAT)->where('is_enable', 1)->find(); |
||||||
|
if (!$payment) { |
||||||
|
echo $store['store_id']."微信支付方式没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$payment_template = PaymentTemplate::where('template_id', $payment->template_id)->where('is_delete', 1)->find(); |
||||||
|
if (!$payment_template) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$wechat_config = $payment_template['config'] ? json_decode($payment_template['config'], true) : []; |
||||||
|
if (!$wechat_config) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$wechat_config = $wechat_config['wechat']['normal'] ?? []; |
||||||
|
//小程序配置 |
||||||
|
$mini = Setting::where('store_id', $order->store_id)->find(); |
||||||
|
if (!$mini) { |
||||||
|
echo $store['store_id']."小程序配置没有".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$mini_config = $mini['values'] ? json_decode($mini['config'], true) : []; |
||||||
|
if (!$mini_config) { |
||||||
|
echo $store['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
//分账 |
||||||
|
$config = [ |
||||||
|
'app_id' => $mini_config['app_id'], |
||||||
|
"secret" => $mini_config['app_secret'], |
||||||
|
'mch_id' => $wechat_config['mchId'], |
||||||
|
'key' => $wechat_config['apiKey'], |
||||||
|
'cert_path' => PaymentTemplateModel::realPathCertFile(PaymentMethodEnum::WECHAT, $wechat_config['apiclientCert'], $order->store_id), |
||||||
|
'key_path' => PaymentTemplateModel::realPathCertFile(PaymentMethodEnum::WECHAT, $wechat_config['apiclientKey'], $order->store_id), |
||||||
|
'notify_url' => '', |
||||||
|
]; |
||||||
|
$payment = Factory::payment($config); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$payment_trade = PaymentTrade::where('trade_id', $order->trade_id)->field('trade_no')->find(); |
||||||
|
if (!$payment_trade) { |
||||||
|
echo $store['store_id']."没有交易流水号".PHP_EOL; |
||||||
|
continue; |
||||||
|
} |
||||||
|
$transaction_id = $payment_trade->trade_no; |
||||||
|
$sharing = $payment->profit_sharing->query($transaction_id,$order->out_order_no); |
||||||
|
var_dump($sharing); |
||||||
|
var_dump($sharing->return_code); |
||||||
|
var_dump($sharing->result_code); |
||||||
|
//更新 |
||||||
|
$ret = Order::where('order_id',$order->order_id)->update(['profitsharing_status' => 2, 'profitsharing_time' => time()]); |
||||||
|
echo "微信支付分账结果".PHP_EOL; |
||||||
|
var_dump($ret); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue