OrderPaid.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace addons\shopro\job\trade;
  3. use addons\shopro\job\BaseJob;
  4. use think\queue\Job;
  5. use think\Db;
  6. use think\exception\HttpResponseException;
  7. use app\admin\model\shopro\trade\Order;
  8. use app\admin\model\shopro\user\User;
  9. use addons\shopro\service\Wallet as WalletService;
  10. use addons\shopro\facade\Wechat;
  11. use addons\shopro\library\easywechatPlus\WechatMiniProgramShop;
  12. /**
  13. * 订单自动操作
  14. */
  15. class OrderPaid extends BaseJob
  16. {
  17. /**
  18. * 交易订单支付完成
  19. */
  20. public function paid(Job $job, $data)
  21. {
  22. try {
  23. $order = $data['order'];
  24. $user = $data['user'];
  25. $order = Order::where('id', $order['id'])->find();
  26. $user = User::get($user['id']);
  27. // 数据库删订单的问题常见,这里被删的订单直接把队列移除
  28. if ($order) {
  29. Db::transaction(function () use ($order, $user, $data) {
  30. if ($order->type == 'recharge') {
  31. // 充值
  32. $ext = $order->ext;
  33. $rule = $ext['rule'] ?? [];
  34. $money = (isset($rule['money']) && $rule['money'] > 0) ? $rule['money'] : 0;
  35. $gift_type = $rule['gift_type'] ?? 'money';
  36. $gift = (isset($rule['gift']) && $rule['gift'] > 0) ? $rule['gift'] : 0;
  37. if ($money > 0) {
  38. // 增加余额
  39. WalletService::change($user, 'money', $money, 'order_recharge', [
  40. 'order_id' => $order->id,
  41. 'order_sn' => $order->order_sn,
  42. 'order_type' => 'trade_order',
  43. ]);
  44. }
  45. if ($gift > 0) {
  46. // 充值赠送
  47. WalletService::change($user, $gift_type, $gift, 'recharge_gift', [
  48. 'order_id' => $order->id,
  49. 'order_sn' => $order->order_sn,
  50. 'order_type' => 'trade_order',
  51. ]);
  52. }
  53. }
  54. $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram());
  55. // 微信小程序,使用 微信支付, 并且存在微信发货管理权限时,才推送发货消息
  56. if ($order->platform == 'WechatMiniProgram' && $order->pay_type == 'wechat' && $uploadshoppingInfo->isTradeManaged()) {
  57. $uploadshoppingInfo->tradeUploadShippingInfos($order);
  58. }
  59. });
  60. }
  61. // 删除 job
  62. $job->delete();
  63. } catch (HttpResponseException $e) {
  64. $data = $e->getResponse()->getData();
  65. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  66. format_log_error($e, 'OrderPaid.paid.HttpResponseException', $message);
  67. } catch (\Exception $e) {
  68. format_log_error($e, 'OrderPaid.paid');
  69. }
  70. }
  71. }