6
0
Fork 0

微信支付 发货信息

base
panliang 2026-06-29 19:28:21 +08:00
parent ed5c3a5397
commit 3197244f6e
3 changed files with 33 additions and 59 deletions

View File

@ -340,55 +340,5 @@ class OfflineOrderService
$coupon->markAsUse(); $coupon->markAsUse();
} }
} }
if (config('app.env') != "local") {
try {
$this->orderShip($order);
} catch (BizException $e) {
logger()->error("线下订单-自动发货失败: " . $e->getMessage(), ['id' => $order->id, 'sn' => $order->sn]);
}
}
}
/**
* 订单发货
*/
public function orderShip(OfflineOrder $order)
{
$now = now();
if (!$order->isPaid()) {
throw new BizException("订单未支付");
}
$userId = $order->user_id;
$socialite = SocialiteUser::where('user_id', $userId)->where('socialite_type', SocialiteType::WechatMiniProgram)->first();
if (!$socialite) {
throw new BizException("未找到用户的openid");
}
$openid = $socialite->socialite_id;
$items = $order->items()->with(['productCategory'])->get();
$desc = [];
foreach ($items as $item) {
$name = data_get($item, "productCategory.name", "定制服务");
array_push($desc, $name);
}
$data = [
"order_key" => [
"order_number_type" => 1,
"out_trade_no" => $order->payment_sn,
],
"logistics_type" => 4,
"delivery_mode" => 1,
"shipping_list" => [
"item_desc" => implode(";", $desc)
],
"upload_time" => $now,
"payer" => [
"openid" => $openid
]
];
WxMpService::make()->uploadShippingInfo($data);
$order->ship_time = $now;
$order->save();
} }
} }

View File

@ -3,10 +3,11 @@
namespace App\Services; namespace App\Services;
use App\Enums\OfflineOrderStatus; use App\Enums\OfflineOrderStatus;
use App\Enums\SocialiteType;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Exceptions\InvalidPaySerialNumberException; use App\Exceptions\InvalidPaySerialNumberException;
use App\Models\PayLog; use App\Models\PayLog;
use App\Models\{OfflineOrder, Order, OrderPre}; use App\Models\{OfflineOrder, Order, OrderPre, SocialiteUser};
class PayService class PayService
{ {
@ -54,6 +55,7 @@ class PayService
]); ]);
$payable = $payLog->payable; $payable = $payLog->payable;
$userId = null;
if ($payable instanceof Order) { if ($payable instanceof Order) {
if ($payable->isPaid()) { if ($payable->isPaid()) {
@ -63,6 +65,7 @@ class PayService
if ($payable->isCompleted()) { if ($payable->isCompleted()) {
throw new BizException('订单已完成'); throw new BizException('订单已完成');
} }
$userId = $payable->user_id;
$payable->update([ $payable->update([
'pay_sn' => $payLog->pay_sn, 'pay_sn' => $payLog->pay_sn,
@ -77,6 +80,7 @@ class PayService
} elseif ($payable->isRevoked()) { } elseif ($payable->isRevoked()) {
throw new BizException('订单取消'); throw new BizException('订单取消');
} }
$userId = $payable->user_id;
OfflineOrderService::make()->paySuccess($payable, [ OfflineOrderService::make()->paySuccess($payable, [
'payment_sn' => $payLog->pay_sn, 'payment_sn' => $payLog->pay_sn,
@ -86,9 +90,36 @@ class PayService
'status' => OfflineOrderStatus::Paid, 'status' => OfflineOrderStatus::Paid,
]); ]);
} elseif ($payable instanceof \App\Models\UserVip) { } elseif ($payable instanceof \App\Models\UserVip) {
$userId = $payable->user_id;
(new \App\Services\VipService())->success($payable, ['pay_at' => $payLog->pay_at]); (new \App\Services\VipService())->success($payable, ['pay_at' => $payLog->pay_at]);
} }
// 订单支付成功, 调用发货信息API
if ($userId) {
$socialite = SocialiteUser::where('user_id', $userId)->where('socialite_type', SocialiteType::WechatMiniProgram)->first();
if ($socialite) {
$openid = $socialite->socialite_id;
if ($openid) {
$data = [
"order_key" => [
"order_number_type" => 1,
"out_trade_no" => $payLog->pay_sn,
],
"logistics_type" => 4,
"delivery_mode" => 1,
"shipping_list" => [
"item_desc" => "定制服务"
],
"upload_time" => $payLog->pay_at,
"payer" => [
"openid" => $openid
]
];
WxMpService::make()->uploadShippingInfo($data);
}
}
}
return $payLog; return $payLog;
} }

View File

@ -17,18 +17,11 @@ class WxMpService
* 发货信息录入 * 发货信息录入
* https://developers.weixin.qq.com/miniprogram/dev/server/API/order_shipping/api_uploadshippinginfo.html * https://developers.weixin.qq.com/miniprogram/dev/server/API/order_shipping/api_uploadshippinginfo.html
*/ */
public function uploadShippingInfo($data) public function uploadShippingInfo(array $data)
{ {
$app = Factory::miniProgram(config('wechat.mini_program.default')); $app = Factory::miniProgram(config('wechat.mini_program.default'));
$accessToken = $app->access_token->getToken()['access_token']; $accessToken = $app->access_token->getToken()['access_token'];
$url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $accessToken; $url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $accessToken;
$data = [
"order_key" => [
"order_number_type" => 1,
"out_trade_no" => "",
],
"logistics_type" => 4
];
$response = Http::post($url, $data); $response = Http::post($url, $data);
$response->throw(); $response->throw();