Version: current

      1 <?php
      2 // Добавление к заказу списка платежей по нему (не понятно, нужно ли - приязка к заказу есть в платеже чрезе поля PaidForID и PaidForClass)
      3 class PaymentOrderExtension extends DataObjectDecorator {
      4
      5     function extraStatics() {}
      6
      7     /*
      8      * Платежи по заказу
      9      *
     10      * @return DataObjectSet
     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     /* сделано в Order.php
     30     function onBeforeWrite(){
     31         if ($this->owner->isChanged('Status')) {
     32             $changes = $this->owner->getChangedFields();
     33             $statusData = $changes['Status'];
     34             if ($statusData['before'] == 'Query' && $statusData['after'] == 'Unpaid') { //если заказ прошел проверку
     35                 //Высылаем письмо с сылкой на оплату заказа
     36
     37                 $conf = SiteConfig::current_site_config();
     38                 $email = new Email();
     39                 $email->setFrom($conf->CartSenderEmail());
     40                 $to = '';
     41                 if ($this->owner->Email) {
     42                     $to = $this->owner->Email;
     43                 }
     44                 elseif ($this->owner->MemberID && $this->owner->Member() && $this->owner->Member()->Email) {
     45                     $to = $this->owner->Member()->Email;
     46                 }
     47                 if ($to) {
     48                     $email->setSubject(_t('Order.PaymentLink', 'Link to pay order'));
     49                     $email->setTo($to);
     50                     $email->setTemplate('Order_PaymentLink');
     51                     $email->populateTemplate(array(
     52                         'Link' => $this->PaymentLink(),
     53                         'Order' => $this->owner,
     54                     ));
     55                     $email->send();
     56                 }
     57             }
     58
     59         }
     60         parent::onBeforeWrite();
     61     }
     62     */
     63
     64
     65     function updateCMSFields(& $fields) {
     66         $fields->findOrMakeTab('Root.Payments', _t('Order.tab_Payments', 'Payments'));
     67         //$paymentFields = singleton('Payment')->getCMSFields();
     68         //$paymentFields->addFieldToTab('Root.Main', new HiddenField('PaidForID','PaidForID',$this->owner->ID));
     69         //$paymentFields->addFieldToTab('Root.Main', new HiddenField('PaidForClass','PaidForClass',$this->owner->ClassName));
     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                     //$order->Paid = SS_Datetime::now()->Rfc2822();
     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                 // !! Возможно добавить валидацию/корректировку телефона по формату Яндекса (+79001231212)
    160                 if (!$this->owner->Email) {
    161                     if (!$this->owner->Phone) {
    162                         $result->error(_t('YaMoneyPayment.BadContactsForATOLL'));
    163                     } else {
    164                         // телефон по формату Яндекса (+79001231212)
    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. -