1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Page; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
6
|
|
|
use Dynamic\FoxyStripe\Page\ProductPageController; |
7
|
|
|
use Dynamic\FoxyStripe\Form\FoxyStripePurchaseForm; |
8
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeSetting; |
9
|
|
|
use SilverStripe\View\Requirements; |
10
|
|
|
use SilverStripe\Forms\CurrencyField; |
11
|
|
|
use SilverStripe\Control\Director; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DonationProductController |
15
|
|
|
* |
16
|
|
|
* @mixin \Dynamic\FoxyStripe\Page\DonationProduct |
17
|
|
|
*/ |
18
|
|
|
class DonationProductController extends ProductPageController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $allowed_actions = [ |
|
|
|
|
24
|
|
|
'PurchaseForm', |
25
|
|
|
'updatevalue', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
public function init() |
32
|
|
|
{ |
33
|
|
|
parent::init(); |
34
|
|
|
//Requirements::javascript('framework/thirdparty/jquery/jquery.js'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return FoxyStripePurchaseForm |
39
|
|
|
*/ |
40
|
|
|
public function PurchaseForm() |
41
|
|
|
{ |
42
|
|
|
$form = parent::PurchaseForm(); |
43
|
|
|
|
44
|
|
|
$fields = $form->Fields(); |
45
|
|
|
|
46
|
|
|
$fields->dataFieldByName('price') |
47
|
|
|
->setAttribute('id', 'price'); |
48
|
|
|
|
49
|
|
|
$fields->insertAfter('price', $currencyField = CurrencyField::create('x:visiblePrice', 'Amount')); |
50
|
|
|
|
51
|
|
|
$currencyField->setAttribute('id', 'visiblePrice'); |
52
|
|
|
|
53
|
|
|
$fields->removeByName([ |
54
|
|
|
'x:visibleQuantity', |
55
|
|
|
ProductPage::getGeneratedValue($this->Code, 'weight', $this->Weight), |
|
|
|
|
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
if (FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
59
|
|
|
Requirements::javascript("silverstripe/userforms:client/dist/js/jquery-validation/jquery.validate.min.js"); |
60
|
|
|
Requirements::javascriptTemplate('_resources/vendor/dynamic/foxystripe/javascript/donationProduct.js', [ |
61
|
|
|
'Trigger' => (string)$currencyField->getAttribute('id'), |
62
|
|
|
'UpdateURL' => Director::absoluteURL($this->Link('updatevalue')), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$form->setAttribute('data-updateurl', $this->Link('updatevalue')); |
67
|
|
|
|
68
|
|
|
return $form; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* create new encrypted price value based on user input. |
73
|
|
|
* |
74
|
|
|
* @param $request |
75
|
|
|
* |
76
|
|
|
* @return string|\SilverStripe\Control\HTTPResponse |
77
|
|
|
*/ |
78
|
|
|
public function updatevalue(\SilverStripe\Control\HTTPRequest $request) |
79
|
|
|
{ |
80
|
|
|
if ($request->getVar('Price') && FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
81
|
|
|
$price = $request->getVar('Price'); |
82
|
|
|
$signedPrice = \FoxyCart_Helper::fc_hash_value($this->Code, 'price', $price, 'value', false); |
|
|
|
|
83
|
|
|
$json = json_encode(['Price' => $signedPrice]); |
84
|
|
|
|
85
|
|
|
$this->response->setBody($json); |
86
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
87
|
|
|
|
88
|
|
|
return $this->response; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 'false'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|