|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\API\Client\CustomerClient; |
|
6
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
|
7
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeSetting; |
|
8
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
|
9
|
|
|
use SilverStripe\Forms\FieldList; |
|
10
|
|
|
use SilverStripe\Forms\TextField; |
|
11
|
|
|
use SilverStripe\ORM\DataExtension; |
|
12
|
|
|
use SilverStripe\Security\Member; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CustomerExtension |
|
16
|
|
|
* @package Dynamic\FoxyStripe\ORM |
|
17
|
|
|
* |
|
18
|
|
|
* @property Member $owner |
|
19
|
|
|
* @property \SilverStripe\ORM\FieldType\DBInt Customer_ID |
|
20
|
|
|
*/ |
|
21
|
|
|
class CustomerExtension extends DataExtension |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $db = [ |
|
|
|
|
|
|
27
|
|
|
'Customer_ID' => 'Int', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
34
|
|
|
'Orders' => Order::class, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
41
|
|
|
'Customer_ID' => true, // make unique |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param FieldList $fields |
|
46
|
|
|
*/ |
|
47
|
|
|
public function updateCMSFields(FieldList $fields) |
|
48
|
|
|
{ |
|
49
|
|
|
$fields->replaceField('Customer_ID', TextField::create('Customer_ID')->performReadonlyTransformation()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
54
|
|
|
*/ |
|
55
|
49 |
|
public function onBeforeWrite() |
|
56
|
|
|
{ |
|
57
|
49 |
|
parent::onBeforeWrite(); |
|
58
|
|
|
|
|
59
|
49 |
|
$client = CustomerClient::create($this->owner); |
|
60
|
|
|
|
|
61
|
49 |
|
if (!$this->owner->Customer_ID && FoxyStripeSetting::current_foxystripe_setting()->UseSingleSignOn) { |
|
62
|
|
|
$data = $client->putCustomer(); |
|
63
|
|
|
|
|
64
|
|
|
$parts = explode('/', $data['_links']['self']['href']); |
|
65
|
|
|
|
|
66
|
|
|
$customerID = $parts[count($parts) - 1]; |
|
67
|
|
|
|
|
68
|
|
|
$this->owner->Customer_ID = $customerID; |
|
69
|
49 |
|
} elseif ($this->owner->isChanged()) { |
|
70
|
49 |
|
$response = $client->putCustomer(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|