PaySuccess.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\behavior\order;
  10. use app\common\service\Message as MessageService;
  11. use app\common\service\order\Printer as PrinterService;
  12. use app\common\service\wechat\wow\Order as WowOrder;
  13. use app\common\enum\OrderType as OrderTypeEnum;
  14. use app\common\enum\OrderStatus as OrderStatusEnum;
  15. use app\common\enum\order\OrderSource as OrderSourceEnum;
  16. /**
  17. * 订单支付成功后扩展类
  18. * Class PaySuccess
  19. * @package app\api\behavior\order
  20. */
  21. class PaySuccess
  22. {
  23. // 订单信息
  24. private $order;
  25. // 订单类型
  26. private $orderType;
  27. private $wxappId;
  28. /**
  29. * 订单来源回调业务映射类
  30. * @var array
  31. */
  32. protected $sourceCallbackClass = [
  33. OrderSourceEnum::MASTER => 'app\api\service\master\order\PaySuccess',
  34. OrderSourceEnum::BARGAIN => 'app\api\service\bargain\order\PaySuccess',
  35. OrderSourceEnum::SHARP => 'app\api\service\sharp\order\PaySuccess',
  36. ];
  37. /**
  38. * 执行入口
  39. * @param $order
  40. * @param int $orderType
  41. * @return bool
  42. * @throws \app\common\exception\BaseException
  43. * @throws \think\Exception
  44. * @throws \think\exception\DbException
  45. */
  46. public function run($order, $orderType = OrderTypeEnum::MASTER)
  47. {
  48. // 设置当前类的属性
  49. $this->setAttribute($order, $orderType);
  50. // 订单公共业务
  51. $this->onCommonEvent();
  52. // 普通订单业务
  53. if ($orderType == OrderTypeEnum::MASTER) {
  54. $this->onMasterEvent();
  55. }
  56. // 订单来源回调业务
  57. $this->onSourceCallback();
  58. return true;
  59. }
  60. /**
  61. * 设置当前类的属性
  62. * @param $order
  63. * @param int $orderType
  64. */
  65. private function setAttribute($order, $orderType = OrderTypeEnum::MASTER)
  66. {
  67. $this->order = $order;
  68. $this->wxappId = $this->order['wxapp_id'];
  69. $this->orderType = $orderType;
  70. }
  71. /**
  72. * 订单公共业务
  73. * @throws \app\common\exception\BaseException
  74. * @throws \think\Exception
  75. * @throws \think\exception\DbException
  76. */
  77. private function onCommonEvent()
  78. {
  79. // 发送消息通知
  80. (new MessageService)->payment($this->order, $this->orderType);
  81. // 小票打印
  82. (new PrinterService)->printTicket($this->order, OrderStatusEnum::ORDER_PAYMENT);
  83. }
  84. /**
  85. * 普通订单业务
  86. * @throws \app\common\exception\BaseException
  87. * @throws \think\exception\DbException
  88. */
  89. private function onMasterEvent()
  90. {
  91. // 同步好物圈
  92. (new WowOrder($this->wxappId))->import([$this->order], true);
  93. }
  94. /**
  95. * 订单来源回调业务
  96. * @return bool
  97. */
  98. private function onSourceCallback()
  99. {
  100. if (!isset($this->order['order_source'])) {
  101. return false;
  102. }
  103. if (!isset($this->sourceCallbackClass[$this->order['order_source']])) {
  104. return false;
  105. }
  106. $class = $this->sourceCallbackClass[$this->order['order_source']];
  107. return !is_null($class) ? (new $class)->onPaySuccess($this->order) : false;
  108. }
  109. }