1 <?php
2
3 class PaymentOrderExtension extends DataObjectDecorator {
4
5 function () {}
6
7 8 9 10 11
12 function Payments() {
13 return DataObject::get('Payment', "PaidforClass='{$this->owner->ClassName}' and PaidForID={$this->owner->ID}", "ID");
14 }
15
16 function Payment() {
17 if ($payments = $this->Payments()) {
18 return $payments->Last();
19 }
20 return false;
21 }
22
23 function canPay($memberID) {
24 if ($this->owner->Status != 'Paid' || $this->owner->Status != 'Complete' || $this->owner->Status != 'Sent')
25 return true;
26 return false;
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 function updateCMSFields(& $fields) {
66 $fields->findOrMakeTab('Root.Payments', _t('Order.tab_Payments', 'Payments'));
67
68
69
70 $ctf = new ComplexTableField(
71 $this->owner,
72 'Payments',
73 'Payment',
74 null,
75 null,
76 "PaidForID = {$this->owner->ID} AND PaidForClass = '{$this->owner->ClassName}'"
77 );
78 $ctf->setPermissions(array('edit','delete'));
79 $fields->addFieldToTab('Root.Payments', $ctf );
80 }
81
82 83 84 85
86 function OnPaymentSuccess() {
87 if(($this->owner->Status != 'Paid') && ($this->owner->Status != 'Completed')){
88 if ($this->owner->Payments() && $this->owner->Payments()->Count()) {
89 $sum = 0;
90 foreach($this->owner->Payments() as $payment) {
91 if ($payment->Status == 'Success') {
92 $sum += $payment->Amount;
93 }
94 }
95 if ($sum >= $this->owner->getPaymentSum()) {
96
97 $this->owner->Status = 'Paid';
98
99 $this->owner->write();
100 }
101 }
102
103 }
104 }
105
106 function updateRequiredFields(& $fields) {
107 if (PaymentSiteConfig::getPaymentAfterConfirm())
108 $fields[] = 'Email';
109 }
110
111
112
113 function getPaymentSum() {
114 $payedSum = 0;
115 if ($this->owner->Payments() && $this->owner->Payments()->Count()) {
116 foreach($this->owner->Payments() as $payment) {
117 if ($payment->Status == 'Success') {
118 $payedSum += $payment->Amount;
119 }
120 }
121 }
122 return $this->owner->GrandTotal - $payedSum;
123 }
124
125 function PaymentType() {
126 if ($this->owner->Payments() && ($payment = $this->owner->Payments()->Last())) {
127 return $payment->PaymentType();
128 }
129 if (isset($this->owner->PaymentMethod) && (int)$this->owner->PaymentMethod) {
130 return DataObject::get_by_id('PaymentMethod', (int)$this->owner->PaymentMethod);
131 }
132 return false;
133 }
134
135 function PaymentLink() {
136 if ($paymentType = $this->owner->PaymentType()) {
137 return $paymentType->paymentLink($this->owner->Payments()->Last()->ID);
138 }
139 return false;
140 }
141
142 function ClearPaymentLink() {
143 if ($paymentType = $this->owner->PaymentType()) {
144 return $paymentType->getClearPaymentLink($this->owner->Payments()->Last());
145 }
146 return false;
147 }
148
149 function getTransactionID() {
150 if ($payment = $this->owner->Payment()) {
151 return $payment->TransactionID;
152 }
153 return false;
154 }
155
156 function updateValidationResult(& $result) {
157 if ($paymentMethod = $this->PaymentType()) {
158 if ($paymentMethod == 'YaMoneyPayment') {
159
160 if (!$this->owner->Email) {
161 if (!$this->owner->Phone) {
162 $result->error(_t('YaMoneyPayment.BadContactsForATOLL'));
163 } else {
164
165 if (!preg_match('/^(\\+7)\d{10}$/', $this->owner->Phone)) {
166 $result->error(_t('YaMoneyPayment.BadPhoneForATOLL'));
167 }
168 }
169 }
170 }
171 }
172 }
173 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-