Пример #1
0
 /**
  * Get transactions list
  * @param array $conditions - $key=>$value pairs
  * @return array $transactions - transactions list
  */
 public static final function getTransactionsByFields($conditions)
 {
     $transaction_model = new waTransactionModel();
     $transaction_data_model = new waTransactionDataModel();
     $transactions = $transaction_model->getByFields($conditions);
     $transactions_data = $transaction_data_model->getByField('transaction_id', array_keys($transactions), true);
     foreach ($transactions_data as $key => $row) {
         $transactions[$row['transaction_id']]['raw_data'][$row['field_id']] = $row['value'];
     }
     return $transactions;
 }
 private function restPayment($payment_form_data, $order_data, $auto_submit = false)
 {
     $order_data = waOrder::factory($order_data);
     $transaction_model = new waTransactionModel();
     $this->order_id = $order_data->id;
     $transaction_data = $transaction_model->getByField(array('app_id' => $this->app_id, 'plugin' => $this->id, 'order_id' => $this->order_id), 'id');
     ksort($transaction_data);
     $transaction_data = end($transaction_data);
     $messages = array();
     if (empty($transaction_data)) {
         if (!empty($payment_form_data['customer_phone'])) {
             $payment_form_data['customer_phone'] = preg_replace('@\\D+@', '', $payment_form_data['customer_phone']);
             if (preg_match('@\\d{11}@', $payment_form_data['customer_phone'])) {
                 try {
                     $transaction_data = $this->restAuth($payment_form_data, $order_data);
                 } catch (waPaymentException $ex) {
                     $messages[] = $ex->getMessage();
                 }
             } else {
                 $messages[] = 'Неверный номер телефона, используйте только цифры без пробелов и разделителей';
             }
         }
     } elseif ($transaction_data['state'] == self::STATE_AUTH && empty($payment_form_data['customer_phone'])) {
         $time = min(time() - strtotime($transaction_data['create_datetime']), time() - strtotime($transaction_data['update_datetime'])) / 3600;
         if ($time > min($this->lifetime, 2)) {
             try {
                 $transaction_data = $this->restAuth(array(), $order_data);
             } catch (waPaymentException $ex) {
                 if ($ex->getCode() == 210) {
                     unset($transaction_data['native_id']);
                 } else {
                     $messages[] = $ex->getMessage();
                 }
             }
         }
     }
     if (!empty($transaction_data['native_id'])) {
         if ($transaction_data['state'] == self::STATE_CAPTURED) {
             //TODO reload page
             //wa()->getResponse()->redirect('');
             $message = 'Заказ уже оплачен';
         } elseif ($transaction_data['state'] == self::STATE_AUTH) {
             $url = $this->getEndpointUrl('form', array('native_id' => $transaction_data['native_id']));
             if ($auto_submit) {
                 wa()->getResponse()->redirect($url);
                 return null;
             } else {
                 $view = wa()->getView();
                 $view->assign('form_url', $url);
                 $view->assign('messages', $messages);
                 return $view->fetch($this->path . '/templates/paymentRest.html');
             }
         } elseif (in_array($transaction_data['state'], array(self::STATE_DECLINED, self::STATE_CANCELED))) {
             $transaction_data_model = new waTransactionDataModel();
             $raw_status = $transaction_data_model->getByField(array('transaction_id' => $transaction_data['id'], 'field_id' => 'status'));
             switch (ifempty($raw_status['value'])) {
                 case 'expired':
                     /** Время жизни счета истекло. Счет не оплачен. **/
                     $message = sprintf('Время жизни счета истекло. Счет %s не оплачен.', $order_data->id_str);
                     break;
                 case 'unpaid':
                     /** Ошибка при проведении оплаты. Счет не оплачен. **/
                     $message = sprintf('Ошибка при проведении оплаты. Счет %s не оплачен.', $order_data->id_str);
                     break;
                 case 'rejected':
                     /**Счет отклонен.**/
                     $message = sprintf('Счет %s отклонен', $order_data->id_str);
                     break;
                 default:
                     $message = sprintf('Неизвестное состояние для счета %s', $order_data->id_str);
                     break;
             }
         } else {
             $message = 'Возможно, заказ уже оплачен';
         }
         return $message;
     } else {
         if (!empty($payment_form_data['customer_phone'])) {
             $mobile_phone = $payment_form_data['customer_phone'];
         } else {
             $mobile_phone = preg_replace('/[\\D]+/', '', $order_data->getContactField($this->customer_phone, 'default'));
         }
         $view = wa()->getView();
         $view->assign('mobile_phone', $mobile_phone);
         $view->assign('messages', $messages);
         return $view->fetch($this->path . '/templates/paymentRest.html');
     }
 }