1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use NotificationChannels\WhatsApp\Component\Currency; |
7
|
|
|
use NotificationChannels\WhatsApp\Component\DateTime; |
8
|
|
|
use NotificationChannels\WhatsApp\Component\Image; |
9
|
|
|
use NotificationChannels\WhatsApp\Component\QuickReplyButton; |
10
|
|
|
use NotificationChannels\WhatsApp\Component\Text; |
11
|
|
|
use NotificationChannels\WhatsApp\WhatsAppChannel; |
12
|
|
|
use NotificationChannels\WhatsApp\WhatsAppTemplate; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Example notification demonstrating the use of parameter_name functionality |
16
|
|
|
* for WhatsApp template messages according to Meta WhatsApp Cloud API requirements. |
17
|
|
|
* |
18
|
|
|
* @see https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates |
19
|
|
|
*/ |
20
|
|
|
class OrderConfirmationNotification extends Notification |
21
|
|
|
{ |
22
|
|
|
private $order; |
23
|
|
|
private $customer; |
24
|
|
|
private $deliveryDate; |
25
|
|
|
|
26
|
|
|
public function __construct($order, $customer, \DateTimeImmutable $deliveryDate) |
27
|
|
|
{ |
28
|
|
|
$this->order = $order; |
29
|
|
|
$this->customer = $customer; |
30
|
|
|
$this->deliveryDate = $deliveryDate; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the notification's delivery channels. |
35
|
|
|
*/ |
36
|
|
|
public function via($notifiable): array |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
return [WhatsAppChannel::class]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Build the WhatsApp template message with named parameters. |
43
|
|
|
* |
44
|
|
|
* This example shows how to use parameter_name for better template maintenance |
45
|
|
|
* and compliance with Meta WhatsApp API requirements. |
46
|
|
|
*/ |
47
|
|
|
public function toWhatsapp($notifiable): WhatsAppTemplate |
48
|
|
|
{ |
49
|
|
|
return WhatsAppTemplate::create() |
50
|
|
|
->name('order_confirmation') // Your configured template name |
51
|
|
|
->language('en_US') |
52
|
|
|
|
53
|
|
|
// Header with named parameter for the product image |
54
|
|
|
->header( |
55
|
|
|
(new Image($this->order['product_image'])) |
56
|
|
|
->parameterName('product_image') |
57
|
|
|
) |
58
|
|
|
|
59
|
|
|
// Body components with named parameters |
60
|
|
|
->body( |
61
|
|
|
(new Text($this->customer['name'])) |
62
|
|
|
->parameterName('customer_name') |
63
|
|
|
) |
64
|
|
|
->body( |
65
|
|
|
(new Text($this->order['product_name'])) |
66
|
|
|
->parameterName('product_name') |
67
|
|
|
) |
68
|
|
|
->body( |
69
|
|
|
(new Currency($this->order['total'], $this->order['currency'])) |
70
|
|
|
->parameterName('order_total') |
71
|
|
|
) |
72
|
|
|
->body( |
73
|
|
|
(new DateTime($this->deliveryDate, 'F j, Y')) |
74
|
|
|
->parameterName('delivery_date') |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
// Buttons with named parameters |
78
|
|
|
->buttons( |
79
|
|
|
(new QuickReplyButton(['Track Order', 'Contact Support'])) |
80
|
|
|
->parameterName('action_buttons') |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
->to($notifiable->phone_number); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Example of mixed usage - combining named and positional parameters |
89
|
|
|
*/ |
90
|
|
|
class WelcomeNotification extends Notification |
91
|
|
|
{ |
92
|
|
|
private $customerName; |
93
|
|
|
private $welcomeBonus; |
94
|
|
|
|
95
|
|
|
public function __construct(string $customerName, float $welcomeBonus) |
96
|
|
|
{ |
97
|
|
|
$this->customerName = $customerName; |
98
|
|
|
$this->welcomeBonus = $welcomeBonus; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function via($notifiable): array |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
return [WhatsAppChannel::class]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function toWhatsapp($notifiable): WhatsAppTemplate |
107
|
|
|
{ |
108
|
|
|
return WhatsAppTemplate::create() |
109
|
|
|
->name('welcome_template') |
110
|
|
|
->language('en_US') |
111
|
|
|
|
112
|
|
|
// Mix of named and positional parameters |
113
|
|
|
->body(new Text('Welcome to our service!')) // Positional |
114
|
|
|
->body( |
115
|
|
|
(new Text($this->customerName)) |
116
|
|
|
->parameterName('customer_name') // Named |
117
|
|
|
) |
118
|
|
|
->body(new Text('You have received a bonus:')) // Positional |
119
|
|
|
->body( |
120
|
|
|
(new Currency($this->welcomeBonus, 'USD')) |
121
|
|
|
->parameterName('bonus_amount') // Named |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
->to($notifiable->phone_number); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.