forked from marcguyer/cheddargetter-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
1012 lines (927 loc) · 31.8 KB
/
Client.php
File metadata and controls
1012 lines (927 loc) · 31.8 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
*/
/**
* Client object for interacting with the CheddarGetter service
* @category CheddarGetter
* @package CheddarGetter
* @author Marc Guyer <marc@cheddargetter.com>
* @example example/example.php
*/
class CheddarGetter_Client {
/**
* The adapter to access cookie data and such.
* By default, it will use PHP superglobals directly but an implementation based on the
* abstraction of a framework can be used.
*
* @var CheddarGetter_Http_AdapterInterface
*/
static private $_requestAdapter;
/**
* @var string Username credential for accessing the CheddarGetter API
*/
private $_username;
/**
* @var string Password credential for accessing the CheddarGetter API
*/
private $_password;
/**
* @var string URL for accessing the CheddarGetter API
*/
private $_url;
/**
* @var string Product identifier
*/
private $_productCode;
/**
* @var string Product identifier (not necessary if productCode is used)
*/
private $_productId;
/**
* @var string Name to use for the marketing cookie
* @see setMarketingCookie
*/
private $_marketingCookieName = 'CGMK';
/**
* If you don't use Zend Framework, it's ok, the client will fallback to curl (so you need curl).
*
* @var CheddarGetter_Client_AdapterInterface
*/
private $_httpClient;
/**
* Constructor
*
* @param $url string
* @param $username string
* @param $password string
* @param $productCode string
* @param string $productId
* @param CheddarGetter_Client_AdapterInterface $adapter
*/
public function __construct($url, $username, $password, $productCode = null, $productId = null, CheddarGetter_Client_AdapterInterface $adapter = null) {
$this->setUrl($url);
$this->setUsername($username);
$this->setPassword($password);
$this->setProductCode($productCode);
$this->setProductId($productId);
if (!$adapter) {
if (class_exists('Zend_Http_Client')) {
$adapter = new CheddarGetter_Client_ZendAdapter();
} else {
$adapter = new CheddarGetter_Client_CurlAdapter();
}
}
$this->_httpClient = $adapter;
}
/**
* Set URL neccessary for for accessing the CheddarGetter API
*
* @param $url string
* @return CheddarGetter_Client
*/
public function setUrl($url) {
$this->_url = $url;
return $this;
}
/**
* Get URL
*
* @return string
*/
public function getUrl() {
return $this->_url;
}
/**
* Set username neccessary for for accessing the CheddarGetter API
*
* @param $username string
* @return CheddarGetter_Client
*/
public function setUsername($username) {
$this->_username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername() {
return $this->_username;
}
/**
* Set password neccessary for accessing the CheddarGetter API
*
* @param $password string
* @return CheddarGetter_Client
*/
public function setPassword($password) {
$this->_password = $password;
return $this;
}
/**
* Get current password
*
* @return string
*/
private function _getPassword() {
return $this->_password;
}
/**
* Set product code (required for all calls except getAllCustomers)
*
* @param $productCode string
* @return CheddarGetter_Client
*/
public function setProductCode($productCode) {
$this->_productCode = $productCode;
return $this;
}
/**
* Get current product code
*
* @return string
*/
public function getProductCode() {
return $this->_productCode;
}
/**
* Set product id (required for all calls except getAllCustomers unless productCode is used)
*
* @param $productId string
* @return CheddarGetter_Client
*/
public function setProductId($productId) {
$this->_productId = $productId;
return $this;
}
/**
* Get current product id
*
* @return string
*/
public function getProductId() {
return $this->_productId;
}
/**
* Set name for marketing metrics cookie
*
* @see setMarketingCookie
* @param $name string
* @return CheddarGetter_Client
*/
public function setMarketingCookieName($name) {
$this->_marketingCookieName = $name;
return $this;
}
/**
* Get marketing cookie name
*
* @see setMarketingCookie
* @return string
*/
public function getMarketingCookieName() {
return $this->_marketingCookieName;
}
/**
* Magic method wrapper
*
* Essentially just a sanity check making sure we have a productCode for those methods that require it
*
* @param string $method
* @param array $args
*/
public function __call($method, $args) {
switch ($method) {
case 'getUrl':
case 'setUrl':
case 'getUsername':
case 'setUsername':
case 'setPassword':
case 'getProductCode':
case 'setProductCode':
case 'getAllCustomers':
return call_user_func_array(array($this, $method), $args);
break;
default:
if (!$this->getProductCode() && !$this->getProductId()) {
throw new CheddarGetter_Client_Exception('A product code is required for ' . __CLASS__ . '::' . $method . '(). Use ' . __CLASS__ . '::setProductCode()', CheddarGetter_Client_Exception::USAGE_INVALID);
}
}
return call_user_func_array(array($this, $method), $args);
}
/**
* Get pricing plans
*
* Get all plans in the product.
*
* @link https://cheddargetter.com/developers#all-plans
* @param array|null $filters
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getPlans(array $filters = null) {
return new CheddarGetter_Response( $this->request('/plans/get', $filters) );
}
/**
* Get a single pricing plan
*
* @link https://cheddargetter.com/developers#single-plan
* @param string $code Your code for the plan
* @param string|null $id CG id for the plan
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getPlan($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request('/plans/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) )
);
}
/**
* Create new plan
*
* This method is not currently supported and could change in the future.
* Use at your own risk.
*
* @param array|null $data
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*//*
public function newPlan(array $data) {
return new CheddarGetter_Response($this->request('/plans/new', $data));
}*/
/**
* Change plan information
*
* This method is not currently supported and could change in the future.
* Use at your own risk.
*
* @param string $code Your code for the plan
* @param string|null $id CG id for the plan
* @param array|null $data
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*//*
public function editPlan($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/plans/edit/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}*/
/**
* Delete a plan
*
* @param string $code Your code for the plan
* @param string|null $id CG id for the plan
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function deletePlan($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/plans/delete/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code))
)
);
}
/**
* Get customers (DEPRECATED: use getCustomersList to query for multiple customers)
*
* Get all customers in the product
*
* @link https://cheddargetter.com/developers#all-customers
* @param array|null $filters {@link https://cheddargetter.com/developers#all-customers}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getCustomers(array $filters = null) {
return new CheddarGetter_Response($this->request('/customers/get', $filters));
}
/**
* Get customers
*
* Get all customers in the product
*
* @link https://cheddargetter.com/developers#all-customers
* @param array|null $filters {@link https://cheddargetter.com/developers#all-customers}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getCustomersList(array $filters = null) {
return new CheddarGetter_Response($this->request('/customers/list', $filters));
}
/**
* Get a single customer
*
* @link https://cheddargetter.com/developers#single-customer
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getCustomer($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) )
);
}
/**
* Get all customers
*
* Get all customers subscribed to any product
*
* @param array|null $filters
* @return CheddarGetter_Response
* @throws CheddarGetter_Client_Exception
* @throws CheddarGetter_Response_Exception
*/
public function getAllCustomers(array $filters = null) {
// this doesn't work yet
throw new CheddarGetter_Client_Exception("This method is a stub for future functionality. You're probable looking for CheddarGetter_Client::getCustomers()", CheddarGetter_Client_Exception::USAGE_INVALID);
if ($this->getProductCode()) {
throw new CheddarGetter_Client_Exception("Can't use a productCode when requesting getAllCustomers()", CheddarGetter_Client_Exception::USAGE_INVALID);
}
return new CheddarGetter_Response($this->request('/customers/get-all', $filters));
}
/**
* Create new customer
*
* @link https://cheddargetter.com/developers#add-customer
* @see setMarketingCookie
* @param array|null $data {@link https://cheddargetter.com/developers#add-customer}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function newCustomer(array $data) {
$requestAdapter = self::getRequestAdapter();
if ($requestAdapter->hasCookie($this->getMarketingCookieName())) {
// if there's marketing cookie information, add it to the data
$marketingFields = array(
'firstContactDatetime',
'referer',
'campaignTerm',
'campaignName',
'campaignSource',
'campaignMedium',
'campaignContent'
);
$intersect = array_intersect($marketingFields, array_keys($data));
if (empty($intersect)) {
$cookieData = json_decode($requestAdapter->getCookie($this->getMarketingCookieName()));
foreach ($marketingFields as $f) {
$data[$f] = $cookieData->{$f};
}
}
}
return new CheddarGetter_Response($this->request('/customers/new', $data));
}
/**
* Change customer and subscription information
*
* @link https://cheddargetter.com/developers#update-customer-subscription
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array|null $data {@link https://cheddargetter.com/developers#update-customer-subscription}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function editCustomer($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/edit/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Change customer information only
*
* @link https://cheddargetter.com/developers#update-customer
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array|null $data {@link https://cheddargetter.com/developers#update-customer}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function editCustomerOnly($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/edit-customer/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Delete a customer
*
* @link https://cheddargetter.com/developers#delete-customer
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function deleteCustomer($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/delete/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code))
)
);
}
/**
* Delete all customers
*
* WARNING: This will delete all customers and all related data in
* CheddarGetter and will delete all customer data at the gateway
* if a gateway is configured.
*
* @link https://cheddargetter.com/developers#delete-all-customers
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function deleteCustomers() {
return new CheddarGetter_Response(
$this->request(
'/customers/delete-all/confirm/' . time()
)
);
}
/**
* Change subscription information
*
* @link https://cheddargetter.com/developers#update-subscription
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array|null $data {@link https://cheddargetter.com/developers#update-subscription}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function editSubscription($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/edit-subscription/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Cancel subscription
*
* @link https://cheddargetter.com/developers#cancel-subscription
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function cancelSubscription($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/cancel/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code))
)
);
}
/**
* Increment a usage item quantity
*
* @link https://cheddargetter.com/developers#add-item-quantity
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data Your (itemCode or CG itemId) and [quantity] {@link https://cheddargetter.com/developers#add-item-quantity}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function addItemQuantity($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/add-item-quantity/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Decrement a usage item quantity
*
* @link https://cheddargetter.com/developers#remove-item-quantity
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data Your (itemCode or CG itemId) and [quantity] {@link https://cheddargetter.com/developers#remove-item-quantity}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function removeItemQuantity($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/remove-item-quantity/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Set a usage item quantity
*
* @link https://cheddargetter.com/developers#set-item-quantity
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data Your (itemCode or CG itemId) and quantity {@link https://cheddargetter.com/developers#set-item-quantity}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function setItemQuantity($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/set-item-quantity/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Add a custom charge (debit) or credit to the current invoice
*
* A positive 'eachAmount' will result in a debit. If negative, a credit.
*
* @link https://cheddargetter.com/developers#add-charge
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data chargeCode, quantity, eachAmount[, description] {@link https://cheddargetter.com/developers#add-charge}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function addCharge($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/add-charge/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Delete a custom charge (debit) or credit from the customer's current invoice
*
* CG's chargeId is required (found in the customers/get response)
*
* @link https://cheddargetter.com/developers#delete-charge
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data chargeId {@link https://cheddargetter.com/developers#delete-charge}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function deleteCharge($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/customers/delete-charge/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Create a new one-time invoice
*
* One-time invoices take one or more charges in the same format as newCustomer(). One-time invoices are executed immediately using the customer's existing subscription payment method. One-time invoices do not directly effect the subscription pending invoice or billing period.
*
* @link https://cheddargetter.com/developers#one-time-invoice
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data an array of arrays each with: chargeCode, quantity, eachAmount[, description] {@link https://cheddargetter.com/developers#one-time-invoice}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function newOneTimeInvoice($code, $id = null, array $data) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request(
'/invoices/new/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Void or refund invoice
*
* Voiding and refunding can be a bit tricky. Some billing solutions do not allow refunding while a transaction is "voidable". Transactions are usually voidable only for a short time (less than 24 hours). Some billing solutions do not allow voids. Check out the knowledge base article on the subject for more information: {@link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my}
*
* @link https://cheddargetter.com/developers#void-or-refund
* @link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my
* @param string $number The unique number of the invoice to be voided/refunded generated by CheddarGetter.
* @param string|null $id CG id of the invoice
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function voidOrRefundInvoice($number, $id = null) {
return new CheddarGetter_Response(
$this->request(
'/invoices/void-or-refund/' . (($id) ? 'id/'.$id : 'number/'.$number),
array('bogus' => 'make this a post')
)
);
}
/**
* Void invoice
*
* Voiding and refunding can be a bit tricky. Some billing solutions do not allow refunding while a transaction is "voidable". Transactions are usually voidable only for a short time (less than 24 hours). Some billing solutions do not allow voids. Check out the knowledge base article on the subject for more information: {@link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my}
*
* @link https://cheddargetter.com/developers#void
* @link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my
* @param string $number The unique number of the invoice to be voided/refunded generated by CheddarGetter.
* @param string|null $id CG id of the invoice
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function voidInvoice($number, $id = null) {
return new CheddarGetter_Response(
$this->request(
'/invoices/void/' . (($id) ? 'id/'.$id : 'number/'.$number),
array('bogus' => 'make this a post')
)
);
}
/**
* Refund invoice
*
* Voiding and refunding can be a bit tricky. Some billing solutions do not allow refunding while a transaction is "voidable". Transactions are usually voidable only for a short time (less than 24 hours). Some billing solutions do not allow voids. Check out the knowledge base article on the subject for more information: {@link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my}
*
* @link https://cheddargetter.com/developers#void
* @link http://support.cheddargetter.com/kb/operational-how-tos/credits-and-refunds-and-voids-oh-my
* @param string $number The unique number of the invoice to be voided/refunded generated by CheddarGetter.
* @param string|null $id CG id of the invoice
* @param array $amount The amount to be refunded if a partial refund. If the invoice is to be fully refunded, do not pass the amount.
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function refundInvoice($number, $id = null, $amount = null) {
return new CheddarGetter_Response(
$this->request(
'/invoices/void/' . (($id) ? 'id/'.$id : 'number/'.$number),
array('amount' => $amount)
)
);
}
/**
* Run an outstanding invoice
*
* An outstanding invoice might be one that hasn't been transacted yet or one that has been attempted unsucessfully.
*
* @link https://cheddargetter.com/developers#run-invoice
* @param string $code Your code for the customer
* @param string|null $id CG id for the customer
* @param array $data [ccCardCode] (optional) {@link https://cheddargetter.com/developers#run-invoice}
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function runOutstandingInvoice($code, $id = null, array $data = null) {
$this->_requireIdentifier($code, $id);
if (!$data) {
$data['bogus'] = 'make this a post';
}
return new CheddarGetter_Response(
$this->request(
'/customers/run-outstanding/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)),
$data
)
);
}
/**
* Get promotions
*
* Get all promotions in the product.
*
* @link https://cheddargetter.com/developers#all-promotions
* @param array|null $filters
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getPromotions(array $filters = null) {
return new CheddarGetter_Response( $this->request('/promotions/get', $filters) );
}
/**
* Get a single promotion
*
* @link https://cheddargetter.com/developers#single-promotion
* @param string $code Coupon code
* @param string|null $id CG id for the promotion
* @return CheddarGetter_Response
* @throws CheddarGetter_Response_Exception
*/
public function getPromotion($code, $id = null) {
$this->_requireIdentifier($code, $id);
return new CheddarGetter_Response(
$this->request('/promotions/get' . (($id) ? '/id/'.$id : '/code/' . urlencode($code)))
);
}
/**
* Execute CheddarGetter API request
*
* @param string $path Path to the API action
* @param array|null $args HTTP post key value pairs
* @return string Body of the response from the CheddarGetter API
* @throws CheddarGetter_Client_Exception
*/
protected function request($path, array $args = null) {
$url = $this->_url . '/xml' . $path;
if ($this->getProductId()) {
$url .= '/productId/' . urlencode($this->getProductId());
} else if ($this->getProductCode()) {
$url .= '/productCode/' . urlencode($this->getProductCode());
}
if (self::getRequestAdapter()->hasIp()) {
if (!empty($args) && empty($args['remoteAddress'])) {
$args['remoteAddress'] = self::getRequestAdapter()->getIp();
} else if (count($args) == 1 && !empty($args['remoteAddress'])) {
$url .= '/remoteAddress/' . $args['remoteAddress'];
unset($args['remoteAddress']);
} else {
$url .= '/remoteAddress/' . self::getRequestAdapter()->getIp();
}
}
return $this->_httpClient->request($url, $this->getUsername(), $this->_getPassword(), $args);
}
/**
* Set http client
*
* @param CheddarGetter_Client_AdapterInterface|Zend_Http_Client|resource $client Either a Zend_Http_Client or curl resource.
* @return CheddarGetter_Client
* @throws CheddarGetter_Client_Exception
*/
public function setHttpClient($client) {
if ($client instanceof CheddarGetter_Client_AdapterInterface) {
$this->_httpClient = $client;
return $this;
}
// Allows passing the curl resource or the Zend_Http_Client for BC reasons
if ($client instanceof Zend_Http_Client) {
$this->_httpClient = new CheddarGetter_Client_ZendAdapter($client);
return $this;
}
if (is_resource($client) && get_resource_type($client) == 'curl') {
$this->_httpClient = new CheddarGetter_Client_CurlAdapter($client);
return $this;
}
throw new CheddarGetter_Client_Exception("httpClient can only be an instance of CheddarGetter_Client_AdapterInterface or Zend_Http_Client or a php curl resource.", CheddarGetter_Client_Exception::USAGE_INVALID);
}
/**
* Get the current http client
*
* @return CheddarGetter_Client_AdapterInterface
*/
public function getHttpClient() {
return $this->_httpClient;
}
/**
* Set request adapter
*
* @param CheddarGetter_Http_AdapterInterface $requestAdapter
*/
static public function setRequestAdapter(CheddarGetter_Http_AdapterInterface $requestAdapter) {
self::$_requestAdapter = $requestAdapter;
}
/**
* Gets the request adapter.
*
* @return CheddarGetter_Http_AdapterInterface
*/
static public function getRequestAdapter() {
if (!self::$_requestAdapter) {
if (class_exists('Zend_Controller_Front')) {
self::$_requestAdapter = new CheddarGetter_Http_ZendAdapter();
} else {
self::$_requestAdapter = new CheddarGetter_Http_NativeAdapter();
}
}
return self::$_requestAdapter;
}
/**
* Convenience method for requiring an identifier
*
* @param string $code
* @param string $id
* @return bool true if $code or $id exists
* @throws CheddarGetter_Client_Exception if neither identifier exists
*/
private function _requireIdentifier($code, $id) {
if (!$code && !$id) {
throw new CheddarGetter_Client_Exception('Either a code or id is required', CheddarGetter_Client_Exception::USAGE_INVALID);
}
return true;
}
/**
* Convenience wrapper of setcookie() for setting a persistent cookie containing marketing metrics compatible with CheddarGetter's marketing metrics tracking.
*
* Running this method on every request to your marketing site sets or refines the marketing cookie data over time. There is no performance disadvantage to running this method on every request.
*
* If a lead has this cookie set at the time of signup, CheddarGetter_Client::newCustomer() will automatically add the data to the customer record. In other words, simply run this method on every request and there's nothing else to do to take advantage of the metrics tracking in CheddarGetter.
*
* {@link http://support.cheddargetter.com/faqs/marketing-metrics/marketing-metrics More about CheddarGetter's marketing metrics tracking }
*
* @see newCustomer
* @param string $cookieName
* @param int $expire
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @throws CheddarGetter_Client_Exception if headers are already sent
*/
public static function setMarketingCookie($cookieName = 'CGMK', $expire = null, $path = '/', $domain = null, $secure = false, $httpOnly = false) {
// default to a two year cookie
if (!$expire) {
$expire = time() + 60*60*24*365*2;
}
$utmParams = array(
'utm_term' => 'campaignTerm',
'utm_campaign' => 'campaignName',
'utm_source' => 'campaignSource',
'utm_medium' => 'campaignMedium',
'utm_content' => 'campaignContent'
);
$requestAdapter = self::getRequestAdapter();
// no cookie yet -- set the first contact date and referer in the cookie
// (only first request)
if ($requestAdapter->hasCookie($cookieName)) {
// when did this lead first find us? (right now!)
// we'll use this to determine the customer "vintage"
$cookieData = array('firstContactDatetime' => date('c'));
// if there's a __utma cookie, we can get a more accurate time
// which helps us get better data from visitors who first found us
// before we started setting our own cookie
if ($requestAdapter->hasCookie('__utma')) {
list(
$domainHash, $visitorId, $initialVisit, $previousVisit, $currentVisit, $visitCounter
) = explode('.', $requestAdapter->getCookie('__utma'));
if (isset($initialVisit) && $initialVisit && is_int($initialVisit)) {
$cookieData['firstContactDatetime'] = date('c', $initialVisit);
}
}
// set the raw referer (defaults to 'direct')
$cookieData['referer'] = 'direct';
if ($requestAdapter->hasReferrer()) {
$cookieData['referer'] = $requestAdapter->getReferrer();
}
// if there's some utm vars
// When tagging your inbound links for google analytics
// http://www.google.com/support/analytics/bin/answer.py?answer=55518
// our cookie will also benefit by the added params
foreach ($utmParams as $key=>$val) {
$cookieData[$val] = $requestAdapter->getRequestValue($key);
}
$requestAdapter->setCookie($cookieName, json_encode($cookieData), $expire, $path, $domain, $secure, $httpOnly);
// cookie is already set but maybe we can refine it with __utmz data
// (second and subsequent requests)
} else if ($requestAdapter->hasCookie('__utmz')) {
// get the existing cookie information
$cookieData = (array) json_decode($requestAdapter->getCookie($cookieName));
// see if the cookie is baked
// it's baked when it has firstContact, referer and at least one other value
if (isset($cookieData['firstContactDatetime']) && isset($cookieData['referer'])
&& $cookieData['firstContactDatetime'] && $cookieData['referer']
) {
$baked = false;
foreach ($utmParams as $key=>$val) {
if ($cookieData[$val]) {
$baked = true;
break;
}
}
if (!$baked) {
// split the __utmz cookie on periods
list(
$domainHash, $timestamp, $sessionNumber, $campaignNumber, $campaignData
) = explode('.', $requestAdapter->getCookie('__utmz'));
// parse the data
parse_str(strtr($campaignData, "|", "&"));
// see if it's a google adwords lead
// in this case, we only get the keyword
if (isset($utmgclid) && $utmgclid) {
$cookieData['campaignSource'] = 'google';
$cookieData['campaignMedium'] = 'ppc';
$cookieData['campaignTerm'] = (isset($utmctr)) ? $utmctr : '';
} else {
$cookieData['campaignSource'] = (isset($utmcsr)) ? $utmcsr : '';
$cookieData['campaignName'] = (isset($utmccn)) ? $utmccn : '';
$cookieData['campaignMedium'] = (isset($utmcmd)) ? $utmcmd : '';