OrderRefundNotification.php 6.5 KB

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