forked from schavery/sdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UCSalesforce.php
264 lines (203 loc) · 8.04 KB
/
UCSalesforce.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/*
Do UnConditional update of contact info in salesforce.
*/
namespace SDF;
require_once WP_PLUGIN_DIR . '/sdf/types.php';
require_once WP_PLUGIN_DIR . '/sdf/message.php';
class UCSalesforce extends Salesforce {
protected $contact;
private static $FRIEND_OF_SPARK = '00130000007qhRG';
public function init(&$info) {
try {
sdf_message_handler(\SDF\MessageTypes::DEBUG, 'Entered UCSalesforce class');
parent::api();
$this->contact = parent::get_contact($info['email']);
self::merge_contact($info);
parent::upsert_contact();
self::create_pending_li($info);
sdf_message_handler(MessageTypes::DEBUG,
'Finished handling Salesforce contact data');
} catch(\Exception $e) {
sdf_message_handler(MessageTypes::LOG,
__FUNCTION__ . ' : ' . print_r($e, true));
parent::emergency_email($info, $e->getMessage());
}
}
// Take the info array, and this->contact
// and try to reconcile any differences.
// I can't figure out how to make this code
// less of a plate of pasta
private function merge_contact(&$info) {
sdf_message_handler(MessageTypes::DEBUG,
'Attempting to reconcile existing Salesforce data (if any) with submitted donation data');
// Set basic member level, update later when we
// get capture confirmation
if(!isset($this->contact->Member_Level__c)) {
$this->contact->Member_Level__c = 'Donor';
sdf_message_handler(MessageTypes::DEBUG, 'Setting donor level to "Donor"');
}
// This person is obviously active, since they're
// submitting their information on this form
$this->contact->Contact_Status__c = 'Active';
$this->contact->Active_Member__c = true;
// Setup their company
if(isset($info['company'])) {
$id = self::company($info['company']);
if(is_null($id)) {
$this->contact->AccountId = static::$FRIEND_OF_SPARK;
sdf_message_handler(MessageTypes::DEBUG,
'Setting donor company to FRIEND OF SPARK');
} else {
$this->contact->AccountId = $id;
sdf_message_handler(MessageTypes::DEBUG,
'Setting donor company to submitted value');
}
} else {
$this->contact->AccountId = static::$FRIEND_OF_SPARK;
sdf_message_handler(MessageTypes::DEBUG,
'Setting donor company to FRIEND OF SPARK');
}
// Name
// We just blindly take whatever the contact submits.
// To improve this, we should notify the team when there
// is an existing value, and the new value is different.
$this->contact->FirstName = $info['first-name'];
$this->contact->LastName = $info['last-name'];
sdf_message_handler(MessageTypes::DEBUG, 'Setting name');
// Address, taking the same strategy as name.
if(empty($info['address2'])) {
$this->contact->MailingStreet = $info['address1'];
} else {
$this->contact->MailingStreet =
$info['address1']
. "\n"
. $info['address2'];
}
sdf_message_handler(MessageTypes::DEBUG, 'Setting address');
$this->contact->MailingCity = $info['city'];
$this->contact->MailingState = $info['state'];
$this->contact->MailingPostalCode = $info['zip'];
$this->contact->MailingCountry = $info['country'];
// Phone number
$this->contact->Phone = $info['tel'];
sdf_message_handler(MessageTypes::DEBUG, 'Setting telephone number');
// Email
// This is safe because the contact's email will
// either be null or the given address
$this->contact->Email = $info['email'];
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Setting email: %s', $this->contact->Email));
if(!isset($this->contact->First_Active_Date__c)) {
$this->contact->First_Active_Date__c =
date(parent::$DATE_FORMAT);
sdf_message_handler(MessageTypes::DEBUG, 'Setting first active date');
}
// Same strategy as name.
if(!empty($info['gender'])) {
$this->contact->Gender__c = ucfirst($info['gender']);
sdf_message_handler(MessageTypes::DEBUG, 'Setting gender');
}
// Every contact needs an 'Owner'
if(!isset($this->contact->Board_Member_Contact_Owner__c)) {
$this->contact->Board_Member_Contact_Owner__c = 'Donation Tracking Account';
sdf_message_handler(MessageTypes::DEBUG, 'Setting board member contact owner');
} else {
// If they have a contact owner - dont try to update it
// even to the same value - it sometimes fails because
// "owner is inactive, cannot reparent record"
// even though Amanda's account is def not inactive.
// Unsetting the contact owner should be fine - they will retain
// the existing owner through an update.
unset($this->contact->Board_Member_Contact_Owner__c);
sdf_message_handler(MessageTypes::DEBUG, 'UNSETTING board member contact owner');
}
// Birth month and year
if(!empty($info['birthday-month'])
&& !empty($info['birthday-year'])) {
$birthday = $info['birthday-year'] . '-' . $info['birthday-month'];
$birthday = strtotime($birthday);
// if strtotime bailed out, we will too
if($birthday != false) {
$this->contact->Birth_Month_Year__c = date('m/Y', $birthday);
sdf_message_handler(MessageTypes::DEBUG, 'Setting birth month year');
}
}
// Referral field
if(!empty($info['hearabout'])) {
$info['referral'] = ucfirst($info['hearabout']);
if(!isset($this->contact->How_did_you_hear_about_Spark__c)) {
$this->contact->How_did_you_hear_about_Spark__c = $info['referral'];
sdf_message_handler(MessageTypes::DEBUG,
'Setting preliminary referral data');
}
// Set referral if it's potentially a contact.
if($info['hearabout'] == 'Friend') {
if(!empty($info['hearabout-extra'])) {
sdf_message_handler(MessageTypes::DEBUG,
'Attempting to find referring contact');
$id = parent::search_salesforce(SearchBy::NAME,
$info['hearabout-extra']);
$this->contact->Referred_By__c = $id; // could be null
}
}
// Get the extra data if it's there
if(!empty($info['hearabout-extra'])) {
$info['referral'] .= ': ' . $info['hearabout-extra'];
}
}
}
// Find the company by name, or create a new company
private function company($name) {
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Attempting to search for company name: %s', $name));
if(strlen($name) > 0) {
$search = sprintf('FIND {"%s"} IN NAME FIELDS RETURNING Account(Id)',
parent::sosl_reserved_chars($name));
try {
$records = parent::$connection->search($search);
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Searching Salesforce with query: %s', $search));
if(count($records->searchRecords)) {
$id = $records->searchRecords[0]->record->Id;
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Found company with id: %s', $id));
} else {
sdf_message_handler(MessageTypes::DEBUG, 'Creating new company');
$company = new \stdClass();
$company->Name = $name;
$created = static::$connection->create(
array($company), 'Account');
$id = $created[0]->getId();
}
} catch(\Exception $e) {
// We can also keep this error suppressed
// because knowing the contact's company is not required.
sdf_message_handler(MessageTypes::LOG,
__FUNCTION__ . ' : ' . $e->getMessage());
}
return $id;
}
}
// creates a donation line item with in-honor-of information
private function create_pending_li(&$info) {
sdf_message_handler(MessageTypes::DEBUG,
'Attempting to create pending donation item');
$donation = new \stdClass();
$donation->Contact__c = $this->contact->Id;
$donation->Donation_Date__c = date(parent::$DATE_FORMAT);
$donation->Type__c = 'Membership';
$donation->Stripe_Status__c = 'Pending';
$donation->Stripe_Id__c = $info['stripe-id'];
$donation->Referred_by__c = parent::string_truncate($info['referral'], 255);
$donation->In_Honor_Of__c =
parent::string_truncate($info['inhonorof'], 64);
$r = parent::create(array($donation), 'Donation__c');
if(is_null($r)) {
sdf_message_handler(MessageTypes::DEBUG, 'Creating pending donation item failed, response was null');
} else if(!$r->isSuccess()) {
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Creating pending donation item failed, %s',$r->getErrors()));
}
}
} // end class ?>