123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Notifications;
- use Alipay\EasySDK\Kernel\Config;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Kernel\Util\ResponseChecker;
- use App\Channels\SendMiniChannel;
- use App\Models\AdminMerchant;
- use App\Models\Auth;
- use App\Models\User;
- use Illuminate\Bus\Queueable;
- use Illuminate\Notifications\Notification;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Notifications\Messages\MailMessage;
- use Illuminate\Support\Facades\Log;
- class OrderRefundNotification extends Notification
- {
- use Queueable;
- public static $user;
- public static $order;
- public static $money; // 退款金额
- public static $type; // 退款金额
- /**
- * Create a new notification instance.
- *
- * @return void
- */
- public function __construct(User $user, $order, $money, $type = '')
- {
- //
- self::$user = $user;
- self::$order = $order;
- self::$money = $money;
- self::$type = $type;
- }
- /**
- * Get the notification's delivery channels.
- *
- * @param mixed $notifiable
- * @return array
- */
- public function via($notifiable)
- {
- return [SendMiniChannel::class];
- }
- /**
- * Get the mail representation of the notification.
- *
- * @param mixed $notifiable
- * @return \Illuminate\Notifications\Messages\MailMessage
- */
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->line('The introduction to the notification.')
- ->action('Notification Action', url('/'))
- ->line('Thank you for using our application!');
- }
- /**
- * Get the array representation of the notification.
- *
- * @param mixed $notifiable
- * @return array
- */
- public function toArray($notifiable)
- {
- return [
- //
- ];
- }
- /**
- * 发送微信小程序消息通知
- *
- * */
- public function toSendMini($notifiable)
- {
- $order = self::$order;
- $user = self::$user;
- $merchant = AdminMerchant::byId($user->merchant_id);
- if (!$merchant) {
- Log::error('[待支付提醒错误]:商户不存在');
- }
- $form = Auth::query()->where('user_id',$user->id)->first();
- switch ($form->type) {
- case Auth::TYPE_WEAPP:
- if($merchant->alipaymini_message_json!=''){
- $arr = json_decode($merchant->alipaymini_message_json);
- $TemplateId = isset($arr[1][1])|| !empty($arr[1][1])?$arr[1][1]: Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置') ;
- }else{
- Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置');
- }
- $app = app('wechat.mini_program'); //laravel-wechat 自带服务容器
- $page = 'pages/my_riding/my_riding?order=' . $order->no . '&index=' . $order->order_type;
- $data = [
- 'template_id' => $TemplateId, // 所需下发的订阅模板id
- 'touser' => $user->auth->credential, // 接收者(用户)的 openid
- //'page' => $page, // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
- 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
- 'character_string2' => [
- // 订单编号
- 'value' => $order->no,
- ],
- 'amount3' => [
- // 退款金额
- 'value' => self::$money . '元',
- ],
- 'phrase1' => [
- // 退款结果
- 'value' => self::$type,
- ],
- 'thing4' => [
- // 退款进度
- 'value' => '已为您成功办理退款',
- ],
- ],
- ];
- $res = $app->subscribe_message->send($data);
- // Log::info($res);
- return $res;
- break;
- case Auth::TYPE_ALIPAY:
- if($merchant->alipaymini_message_json!=''){
- $arr = json_decode($merchant->alipaymini_message_json);
- $TemplateId = isset($arr[1][1])|| !empty($arr[1][1])?$arr[1][1]: Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置') ;
- }else{
- Log::error('[待支付提醒错误]:商户'.$user->merchant_id.'模板ID没有配置');
- }
- Factory::setOptions($this->getOptions($merchant));
- $toUserId = $form->credential;
- $formId = '';
- $userTemplateId = $TemplateId; //退款id
- $page = 'pages/index/index';
- $data = php2js([
- 'keyword1'=>[ 'value' => date('Y-m-d H:i:s') ], //退款时间
- 'keyword2'=>[ 'value' => '已为您成功办理退款' ], //退款说明
- 'keyword3'=>[ 'value' => self::$money . '元' ], //退款金额
- ]);
- try {
- //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
- $res = Factory::Marketing()->TemplateMessage()->send($toUserId,$formId,$userTemplateId,$page,$data);
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($res)) {
- // echo "调用发送成功";
- } else {
- $status = true;
- }
- } catch (Exception $e) {
- $status = true;
- }
- break;
- default:
- Log::info('小程序信息没有被发送原因:没有查找到登录方式');
- }
- }
- function getOptions($merchat_id)
- {
- $merchant = AdminMerchant::byId($merchat_id);
- if (!$merchant) {
- Log::error('[待支付提醒错误]:商户不存在');
- }
- return alipay_mini_config( new Config(),$merchant);
- }
- }
|