コード例 #1
0
 public function handleRecord($id, $record)
 {
     switch ($this->_vars['action']) {
         case 'access':
             switch ($this->_vars['access_type']) {
                 case 'exact':
                     $begin_date = $this->_vars['begine_date'];
                     $expire_date = $this->_vars['expire_date'];
                     break;
                 case 'period':
                     $invoice = $this->grid->getDi()->invoiceRecord;
                     $invoice->setUser($this->grid->getDi()->userTable->load($id));
                     $product = $this->grid->getDi()->productTable->load($this->_vars['product_id']);
                     $begin_date = $product->calculateStartDate($this->grid->getDi()->sqlDate, $invoice);
                     $p = new Am_Period($this->_vars['period']);
                     $expire_date = $p->addTo($begin_date);
                     break;
             }
             $a = $this->grid->getDi()->accessRecord;
             $a->begin_date = $begin_date;
             $a->expire_date = $expire_date;
             $a->product_id = $this->_vars['product_id'];
             $a->user_id = $id;
             $a->comment = $this->_vars['comment'];
             $a->insert();
             break;
         case 'payment':
             $invoice = $this->grid->getDi()->invoiceRecord;
             $invoice->user_id = $id;
             $invoice->add($this->grid->getDi()->productTable->load($this->_vars['product_id']));
             $items = $invoice->getItems();
             $item = $items[0];
             $item->first_price = $item->first_total = $this->_vars['amount'];
             $item->second_price = $item->second_total = 0;
             $item->rebill_times = 0;
             $item->second_period = null;
             $invoice->first_subtotal = $invoice->first_total = $this->_vars['amount'];
             $invoice->second_subtotal = $invoice->second_total = 0;
             $invoice->rebill_times = 0;
             $invoice->second_period = null;
             $invoice->first_period = $item->first_period;
             $invoice->paysys_id = $this->_vars['paysys_id'];
             $invoice->comment = $this->_vars['comment'] ? $this->_vars['comment'] : 'mass-subscribe';
             $invoice->save();
             $tr = new Am_Paysystem_Transaction_Manual($this->grid->getDi()->plugins_payment->loadGet($invoice->paysys_id));
             $tr->setAmount($this->_vars['amount']);
             $tr->setTime(new DateTime($this->_vars['dattm']));
             $tr->setReceiptId('mass-subscribe-' . uniqid() . '-' . $invoice->pk());
             $invoice->addPayment($tr);
             break;
     }
 }
コード例 #2
0
 public function refundAction()
 {
     $this->invoice_payment_id = $this->getInt('invoice_payment_id');
     if (!$this->invoice_payment_id) {
         throw new Am_Exception_InputError("Not payment# submitted");
     }
     $p = $this->getDi()->invoicePaymentTable->load($this->invoice_payment_id);
     /* @var $p InvoicePayment */
     if (!$p) {
         throw new Am_Exception_InputError("No payment found");
     }
     if ($this->user_id != $p->user_id) {
         throw new Am_Exception_InputError("Payment belongs to another customer");
     }
     if ($p->isRefunded()) {
         throw new Am_Exception_InputError("Payment is already refunded");
     }
     $amount = sprintf('%.2f', $this->_request->get('amount'));
     if ($p->amount < $amount) {
         throw new Am_Exception_InputError("Refund amount cannot exceed payment amount");
     }
     if ($this->_request->getInt('manual')) {
         switch ($type = $this->_request->getFiltered('type')) {
             case 'refund':
             case 'chargeback':
                 $pl = $this->getDi()->plugins_payment->loadEnabled()->get($p->paysys_id);
                 if (!$pl) {
                     throw new Am_Exception_InputError("Could not load payment plugin [{$pl}]");
                 }
                 $invoice = $p->getInvoice();
                 $transaction = new Am_Paysystem_Transaction_Manual($pl);
                 $transaction->setAmount($amount);
                 $transaction->setReceiptId($p->receipt_id . '-manual-' . $type);
                 $transaction->setTime($this->getDi()->dateTime);
                 if ($type == 'refund') {
                     $invoice->addRefund($transaction, $p->receipt_id);
                 } else {
                     $invoice->addChargeback($transaction, $p->receipt_id);
                 }
                 break;
             case 'correction':
                 $this->getDi()->accessTable->deleteBy(array('invoice_payment_id' => $this->invoice_payment_id));
                 $invoice = $p->getInvoice();
                 $p->delete();
                 $invoice->updateStatus();
                 break;
             default:
                 throw new Am_Exception_InputError("Incorrect refund [type] passed:" . $type);
         }
         $res = array('success' => true, 'text' => ___("Payment has been successfully refunded"));
     } else {
         // automatic
         /// ok, now we have validated $p here
         $pl = $this->getDi()->plugins_payment->loadEnabled()->get($p->paysys_id);
         if (!$pl) {
             throw new Am_Exception_InputError("Could not load payment plugin [{$pl}]");
         }
         /* @var $pl Am_Paysystem_Abstract */
         $result = new Am_Paysystem_Result();
         $pl->processRefund($p, $result, $amount);
         if ($result->isSuccess()) {
             $p->getInvoice()->addRefund($result->getTransaction(), $p->receipt_id, $amount);
             $res = array('success' => true, 'text' => ___("Payment has been successfully refunded"));
         } elseif ($result->isAction()) {
             $action = $result->getAction();
             if ($action instanceof Am_Paysystem_Action_Redirect) {
                 $res = array('success' => 'redirect', 'url' => $result->getUrl());
             } else {
                 // todo handle other actions if necessary
                 throw new Am_Exception_NotImplemented("Could not handle refund action " . get_class($action));
             }
         } elseif ($result->isFailure()) {
             $res = array('success' => false, 'text' => join(";", $result->getErrorMessages()));
         }
     }
     $this->_response->setHeader("Content-type", "application/json");
     echo $this->getJson($res);
 }
コード例 #3
0
 public function handleRecord($id, $record)
 {
     if (!$this->_vars['add_payment']) {
         $a = $this->grid->getDi()->accessRecord;
         $a->begin_date = $this->_vars['start_date'];
         $a->expire_date = $this->_vars['expire_date'];
         $a->product_id = $this->_vars['product_id'];
         $a->user_id = $id;
         $a->insert();
     } else {
         $invoice = $this->grid->getDi()->invoiceRecord;
         $invoice->user_id = $id;
         $invoice->add($this->_getProduct($this->_vars['product_id']));
         $invoice->paysys_id = 'free';
         $invoice->comment = 'mass-subscribe';
         $invoice->calculate();
         $invoice->save();
         $tr = new Am_Paysystem_Transaction_Manual($this->grid->getDi()->plugins_payment->loadGet('free'));
         $tr->setAmount($this->_vars['amount']);
         $tr->setTime(new DateTime($this->_vars['start_date']));
         $tr->setReceiptId('mass-subscribe-' . uniqid());
         $invoice->addPayment($tr);
     }
 }