-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
96 lines (85 loc) · 3.26 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
Kirby::plugin('kirbyzone/sendfox', [
'blueprints' => [
'fields/email-subscription' => __DIR__ . '/blueprints/subscription.yml'
],
#creating siteMethods
#parameter
#listArr = array of list that will include
#dataArr = data consist of email, first_name and last_name
'siteMethods' => [
'addToSendFox' => function ($listArr, $dataArr) {
$status = true;
$msg ="success";
if(count($listArr) <= 0){
$msg = "Lists should not empty";
return ['status'=>false, 'msg'=>$msg];
}
if(count($dataArr) <= 0){
$msg = "Data should not empty";
return ['status'=>false, 'msg'=>$msg];
}else{
if(isset($dataArr['email'])){
if(!V::email($dataArr['email'])) {
$msg = 'Email is not correct';
return ['status'=>false, 'msg'=>$msg];
}
}
}
if(empty(option('sendfox_token')) ){
$msg = "sendfox access token is missing";
return ['status'=>false, 'msg'=>$msg];
}
// Lets Authenticate if the access token is valid
try{
$remote = Remote::request('https://api.sendfox.com/me', [
'method' => 'GET',
'headers' => [
'Content-type: application/json',
'Authorization: Bearer ' . trim(option('sendfox_token'))
]
]);
//Error if Auth failed
if(! json_decode($remote->content())){
$msg = "Authorization Failed";
return ['status'=>false, 'msg'=>$msg];
}
}catch(Exception $e) {
return ['status'=>false, 'msg'=>$e->getMessage()];
}
$data = array_merge(
[
'email' => '',
'first_name' => '',
'last_name' => ''
],$dataArr);
//Create Subscription and send to sendfox
try {
$url = 'https://api.sendfox.com/contacts';
$data = [
"email" => $dataArr['email'],
"first_name" => $dataArr['first_name'],
"last_name" => $dataArr['last_name'],
"lists" => $listArr
];
$options = [
'headers' => [
'Authorization: Bearer ' . trim(option('sendfox_token')),
'Content-Type: application/json'
],
'method' => 'POST',
'data' => json_encode($data)
];
$response = Remote::request($url, $options);
if(! json_decode($response->content())){
$msg = "Creation of Contact Failed";
return ['status'=>false, 'msg'=>$msg];
}
}catch(Exception $e) {
return ['status'=>false, 'msg'=>$e->getMessage()];
}
return ['status'=>$status, 'msg'=>$msg];;
}
]
]);
?>