Version: current

      1 <?php
      2
      3 class Payment extends DataObject {
      4     static $db = array(
      5         'Status' => "Enum('Incomplete,Success,Failure,Pending','Incomplete')",
      6         'Amount' => 'Decimal(10,2)',
      7         'Message' => 'HTMLText',
      8         'IP' => 'Varchar',
      9         'PaidForID' => "Int",
     10         'PaidForClass' => 'Varchar', //Order by default
     11         'HashLink' => 'Varchar',
     12         'PaymentResponse' => 'Text',
     13         'ExceptionError' => 'Text', //сообщение об ошибке из внешней системы
     14         'TransactionID' => 'Varchar(255)', //ID транзакции эквайера
     15     );
     16
     17     static $has_one = array(
     18         'PaymentType' => 'PaymentMethod',
     19         'Member' => 'Member',
     20     );
     21
     22     static $summary_fields = array('ID', 'StatusTitle', 'Amount', 'PaymentType.Title');
     23
     24     function fieldLabels($includerelations = true) {
     25         $labels = parent::fieldLabels($includerelations);
     26         $labels['StatusTitle'] = _t('Payment.db_Status', 'Title');
     27         $labels['PaymentType.Title'] = _t('Payment.has_one_PaymentType', 'Payment Type');
     28         return $labels;
     29     }
     30
     31
     32     static function StatusTitles() {
     33         $options = singleton('Payment')->dbObject('Status')->enumValues(false);
     34         foreach ($options as $key => $val) {
     35             $options[$key] = _t('Payment.Status_'.$key, $val);
     36         }
     37         return $options;
     38     }
     39
     40     function canView($member = null) {
     41         return ($this->PaidObject()) ? $this->PaidObject()->canView() : true;
     42     }
     43
     44     function canEdit($member = null) {
     45         return ($this->PaidObject()) ? $this->PaidObject()->canEdit() : true;
     46     }
     47
     48     function canDelete($member = null) {
     49         return ($this->PaidObject()) ? $this->PaidObject()->canDelete() : true;
     50     }
     51
     52     function canCreate($member = null) {
     53         return ($this->PaidObject()) ? $this->PaidObject()->canCreate() : true;
     54     }
     55
     56
     57     function StatusTitle() {
     58         $titles = self::StatusTitles();
     59         return $titles[$this->Status];
     60     }
     61
     62     /*
     63      * Возвращает оплачиваемый объект
     64      *
     65      */
     66     function PaidObject(){
     67         if ($this->PaidForClass && $this->PaidForID) {
     68             return DataObject::get_by_id($this->PaidForClass, $this->PaidForID);
     69         }
     70     }
     71
     72     function onBeforeWrite() {
     73         if (!$this->HashLink) {
     74             $this->HashLink = md5(time() . serialize($this->data()) . rand());
     75         }
     76         parent::onBeforeWrite();
     77     }
     78
     79     function onAfterWrite() {
     80         if($this->Status == 'Success' && $order = $this->PaidObject()) {
     81             if ($order->hasMethod('OnPaymentSuccess'))
     82                 $order->OnPaymentSuccess();
     83         }
     84     }
     85
     86     function getCMSFields() {
     87         $fields = parent::getCMSFields();
     88         $fields->replaceField('PaymentResponse', $fields->dataFieldByName('PaymentResponse')->performReadonlyTransformation());
     89         $fields->replaceField('ExceptionError', $fields->dataFieldByName('ExceptionError')->performReadonlyTransformation());
     90         $fields->replaceField('IP', $fields->dataFieldByName('IP')->performReadonlyTransformation());
     91         $fields->replaceField('MemberID', $fields->dataFieldByName('MemberID')->performReadonlyTransformation());
     92         $fields->replaceField('PaymentTypeID', $fields->dataFieldByName('PaymentTypeID')->performReadonlyTransformation());
     93         $fields->removeByName('PaidForID');
     94         $fields->removeByName('PaidForClass');
     95
     96         $fields->replaceField('Status', new DropdownField('Status', _t('Payment.db_Status', 'Status'), self::StatusTitles()));
     97         return $fields;
     98     }
     99
    100     function isNew() {
    101         return ($this->Status == 'Incomplete');
    102     }
    103 }
    104

[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. -