PaySuccess.php 3.2 KB

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