OrderLongTimeNoPayNotification.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Notifications;
  3. use App\Channels\SendMiniChannel;
  4. use App\Models\AdminMerchant;
  5. use App\Models\Model;
  6. use App\Models\User;
  7. use App\Models\Auth;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Notifications\Notification;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Notifications\Messages\MailMessage;
  12. use Illuminate\Support\Facades\Log;
  13. use Overtrue\EasySms\EasySms;
  14. use Alipay\EasySDK\Kernel\Factory;
  15. use Alipay\EasySDK\Kernel\Config;
  16. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  17. class OrderLongTimeNoPayNotification extends Notification
  18. {
  19. use Queueable;
  20. public static $user;
  21. public static $order;
  22. /**
  23. * Create a new notification instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct(User $user, $order)
  28. {
  29. //
  30. self::$user = $user;
  31. self::$order = $order;
  32. }
  33. /**
  34. * Get the notification's delivery channels.
  35. *
  36. * @param mixed $notifiable
  37. * @return array
  38. */
  39. public function via($notifiable)
  40. {
  41. return [SendMiniChannel::class];
  42. }
  43. /**
  44. * Get the mail representation of the notification.
  45. *
  46. * @param mixed $notifiable
  47. * @return \Illuminate\Notifications\Messages\MailMessage
  48. */
  49. public function toMail($notifiable)
  50. {
  51. return (new MailMessage)
  52. ->line('The introduction to the notification.')
  53. ->action('Notification Action', url('/'))
  54. ->line('Thank you for using our application!');
  55. }
  56. /**
  57. * Get the array representation of the notification.
  58. *
  59. * @param mixed $notifiable
  60. * @return array
  61. */
  62. public function toArray($notifiable)
  63. {
  64. return [
  65. //
  66. ];
  67. }
  68. /**
  69. * 发送微信小程序消息通知
  70. *
  71. * */
  72. public function toSendMini($notifiable)
  73. {
  74. $order = self::$order;
  75. $user = self::$user;
  76. $status = false;
  77. $merchant = AdminMerchant::byId($user->merchant_id);
  78. if (!$merchant) {
  79. Log::error('[待支付提醒错误]:商户不存在');
  80. }
  81. $form = Auth::query()->where('user_id',$user->id)->first();
  82. switch ($form->type){
  83. case Auth::TYPE_WEAPP:
  84. //微信
  85. $app = app('wechat.mini_program');
  86. if($merchant->alipaymini_message_json!=''){
  87. $arr = json_decode($merchant->alipaymini_message_json);
  88. $TemplateId = isset($arr[0][1])|| !empty($arr[0][1])?$arr[0][1]: Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置') ;
  89. }else{
  90. Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置');
  91. }
  92. $page = 'pages/my_riding/my_riding?order=' . $order->no . '&index=' . $order->order_type;
  93. $data = [
  94. 'template_id' =>$TemplateId,// 所需下发的订阅模板id
  95. 'touser' => $form->credential, // 接收者(用户)的 openid
  96. 'page' => $page, // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  97. 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  98. 'date1' => [
  99. // 用车时间
  100. 'value' => $order->start_use_bike_time,
  101. ],
  102. 'thing2' => [
  103. // 骑行时间
  104. 'value' => floor($order->use_bike_time_length / 60) . 'h' . ($order->use_bike_time_length % 60) . 'm',
  105. ],
  106. 'amount3' => [
  107. // 待支付金额
  108. 'value' => $order->pay_money,
  109. ],
  110. 'thing4' => [
  111. // 费用明细
  112. 'value' => '点击卡片查看明细',
  113. ],
  114. ],
  115. ];
  116. $res = $app->subscribe_message->send($data);
  117. if ($res['errcode'] !== 0) {
  118. $status = true;
  119. }
  120. break;
  121. case Auth::TYPE_ALIPAY:
  122. //支付宝
  123. if($merchant->alipaymini_message_json!=''){
  124. $arr = json_decode($merchant->alipaymini_message_json);
  125. $TemplateId = isset($arr[0][1])|| !empty($arr[0][1])?$arr[0][1]:Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置') ;
  126. }else{
  127. Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置');
  128. }
  129. Factory::setOptions($this->getOptions($merchant));
  130. $toUserId = $form->credential;
  131. $formId = '';
  132. $userTemplateId = $TemplateId;
  133. $page = 'pages/index/index';
  134. $data = php2js([
  135. 'keyword1'=>[ 'value' => date('Y-m-d H:i:s') ], //下单时间
  136. 'keyword2'=>[ 'value' => '你有一个未支付订单,请及时支付。点击进入小程序' ], //订单信息
  137. 'keyword3'=>[ 'value' => $order->no], //订单编号
  138. 'keyword4'=>[ 'value' => $order->pay_money ], //订单金额
  139. ]);
  140. try {
  141. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  142. $res = Factory::Marketing()->TemplateMessage()->send($toUserId,$formId,$userTemplateId,$page,$data);
  143. $responseChecker = new ResponseChecker();
  144. //3. 处理响应或异常
  145. if ($responseChecker->success($res)) {
  146. // echo "调用发送成功";
  147. } else {
  148. $status = true;
  149. }
  150. } catch (Exception $e) {
  151. $status = true;
  152. }
  153. break;
  154. default:
  155. Log::info('小程序信息没有被发送原因:没有查找到登录方式');
  156. }
  157. if ($status) {
  158. Log::warning(json_encode($res));
  159. // $config = config('easySms');
  160. // $easySms = $easySms = new EasySms($config);
  161. app('easy_sms')->send($user->mobile, [
  162. 'template' => 'SMS_205437076',
  163. 'data' => [
  164. // 'truename' => $user->truename,
  165. // 'time' => $order->start_use_bike_time
  166. ]
  167. ]);
  168. }
  169. }
  170. function getOptions($merchant)
  171. {
  172. return alipay_mini_config( new Config(),$merchant);
  173. }
  174. }