예제 #1
0
 /**
  * Model definition.
  */
 function define()
 {
     // Fields.
     $this->fields = array('id', 'ref', 'account_id', 'order_id', 'method', 'action', 'amount', 'status', 'reason', 'date_created', 'date_updated', 'status' => function ($payment) {
         return Payments::get_status($payment);
     }, 'account' => function ($payment) {
         return get("/accounts/{$payment['account_id']}");
     }, 'order' => function ($payment) {
         return get("/orders/{$payment['order_id']}");
     });
     // Search fields.
     $this->search_fields = array('id', 'ref', 'reason');
     // Optional reference key.
     $this->slug_pk = 'ref';
     // Indexes.
     $this->indexes = array('id' => 'unique', 'ref');
     // Validate.
     $this->validate = array('required' => array('account_id', 'order_id', 'method', 'action', 'billing', 'amount', 'reason', 'status'), 'amount', 'unique' => array());
     // Event binds.
     $this->binds = array('POST' => function ($event, $model) {
         $data =& $event['data'];
         if ($order = get("/orders/{$data['order_id']}")) {
             // Default data from order.
             $data['account_id'] = $order['account_id'];
             $data['method'] = $data['method'] ?: $order['billing']['method'];
             $data['action'] = $data['action'] ?: "charge";
         } else {
             $model->error('Order #' . $data['order_id'] . ' not found', 'order_id');
         }
         // Payment data invalid?
         if (!$model->validate($data)) {
             // Prevent further processing.
             return false;
         }
         // Method/action case insensitive.
         $data['method'] = strtolower($data['method']);
         $data['action'] = strtolower($data['action']);
         // Get default status.
         $data['status'] = Payments::get_status($data);
         // Process default methods (i.e. cash, account credit, invoice).
         try {
             $data = Payments::process_default_methods($data);
         } catch (Exception $e) {
             $model->error($e->getMessage(), 'method');
             return false;
         }
     }, 'validate:amount' => function ($value, $field, $params, $model) {
         if ($value == 0) {
             $model->error('invalid', 'amount');
         }
     });
 }