public function setFromTransaction(Invoice $invoice, Am_Paysystem_Transaction_Abstract $transaction) { $this->dattm = $transaction->getTime()->format('Y-m-d H:i:s'); $this->invoice_id = $invoice->invoice_id; $this->user_id = $invoice->user_id; $this->currency = $invoice->currency; $this->paysys_id = $transaction->getPlugin()->getId(); $this->receipt_id = $transaction->getReceiptId(); $this->transaction_id = $transaction->getUniqId(); /// get from invoice $isFirst = !$invoice->getPaymentsCount(); $amount = $transaction->getAmount(); if ($amount <= 0) { $amount = $isFirst && $invoice->first_total ? $invoice->first_total : $invoice->second_total; } $this->amount = (double) $amount; $this->discount = $isFirst ? $invoice->first_discount : $invoice->second_discount; $this->tax = $isFirst ? $invoice->first_tax : $invoice->second_tax; $this->shipping = $isFirst ? $invoice->first_shipping : $invoice->second_shipping; return $this; }
public function _doBill(Invoice $invoice, $doFirst, CcRecord $cc = null, Am_Paysystem_Result $result) { $request = new Am_HttpRequest(self::TICKET_AUTH_URL, Am_HttpRequest::METHOD_POST); $order_id = $invoice->public_id . "-" . sprintf("%03d", $invoice->getPaymentsCount() + 1); $currency = $this->getCurrencyCode($invoice); $amount = ($doFirst ? $invoice->first_total : $invoice->second_total) * 100; $post_params = new stdclass(); $post_params->merchant = $this->getConfig('merchant'); $post_params->amount = $amount; $post_params->orderId = $order_id; $post_params->ticket = $invoice->data()->get(self::TICKET); $post_params->textreply = 'true'; $post_params->currency = $currency; $post_params->capturenow = 'true'; $post_params->md5key = md5($s2 = $this->getConfig('key2') . md5($s1 = $this->getConfig('key1') . "merchant=" . $this->getConfig('merchant') . "&orderid=" . $order_id . "&ticket=" . $invoice->data()->get(self::TICKET) . "¤cy=" . $currency . "&amount=" . $amount)); if ($this->getConfig('testing')) { $post_params->test = 'yes'; } $request->addPostParameter((array) $post_params); $transaction = new Am_Paysystem_Transaction_DibsRecurringSale($this, $invoice, $request, $doFirst); $transaction->run($result); }
public function _doBill(Invoice $invoice, $doFirst, CcRecord $cc, Am_Paysystem_Result $result) { $r = new Am_HttpRequest(self::REBILL_URL, Am_HttpRequest::METHOD_POST); $vars = array('MerchantId' => $this->getConfig('merchant_id'), 'RebillAnchor' => $invoice->data()->get(self::REBILL_ANCHOR), 'OrderId' => $invoice->public_id . '-' . $invoice->getPaymentsCount(), 'Amount' => $invoice->second_total, 'Currency' => $invoice->currency, 'ContentType' => 'text'); $vars['SecurityKey'] = $this->getRebillSecurityKey($vars); foreach ($vars as $k => $v) { $r->addPostParameter($k, $v); } $transaction = new Am_Paysystem_Transaction_PayonlineSystemRebill($this, $invoice, $r, $doFirst); $transaction->run($result); }
protected function createXml(Invoice $invoice, $doFirst) { $request = <<<CUT <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> </soap:Header> <soap:Body> </soap:Body> </soap:Envelope> CUT; $x = new SimpleXMLElement($request); $ns = $x->getNamespaces(); $b = $x->children($ns['soap'])->Body; $bb = $b->addChild('ProcessPayment', "", 'https://www.eway.com.au/gateway/managedpayment'); $h = $x->children($ns['soap'])->Header; $hh = $h->addChild('eWAYHeader', "", 'https://www.eway.com.au/gateway/managedpayment'); $hh->addChild('eWAYCustomerID', $this->plugin->getConfig('customer_id')); $hh->addChild('Username', $this->plugin->getConfig('customer_username')); $hh->addChild('Password', $this->plugin->getConfig('customer_password')); $bb->addChild('managedCustomerID', $invoice->getUser()->data()->get(Am_Paysystem_EwayRapid3::TOKEN)); $bb->addChild('amount', $invoice->second_total * 100); $bb->addChild('invoiceReference', $invoice->public_id . "-" . sprintf("%03d", $invoice->getPaymentsCount())); $bb->addChild('invoiceDescription', $invoice->getLineDescription()); $xml = $x->asXML(); return $xml; }
function processRecurring(Invoice $invoice, $doFirst = true) { $request = new Am_HttpRequest(self::API_URL, Am_HttpRequest::METHOD_POST); $post_params = new stdclass(); $post_params->protocol = '6'; $post_params->msgtype = 'recurring'; $post_params->merchant = $this->getConfig('merchant'); $post_params->ordernumber = $invoice->public_id . "-" . sprintf("%03d", $invoice->getPaymentsCount()) . "-R"; $post_params->amount = ($doFirst ? $invoice->first_total : $invoice->second_total) * 100; $post_params->currency = $invoice->currency; $post_params->autocapture = '1'; $post_params->transaction = $invoice->data()->get(self::SUBSCRIBE); foreach ((array) $post_params as $k => $v) { $cstr .= $v; } $cstr .= $this->getConfig('secret'); $post_params->md5check = md5($cstr); $request->addPostParameter((array) $post_params); return $request; }
/** * Process invoice and insert necessary commissions for it * * External code should guarantee that this method with $payment = null will be called * only once for each user for First user invoice */ public function processPayment(Invoice $invoice, InvoicePayment $payment = null) { $aff_id = $invoice->aff_id; /* @var $coupon Coupon */ try { if (!$aff_id && ($coupon = $invoice->getCoupon())) { // try to find affiliate by coupon $aff_id = $coupon->aff_id ? $coupon->aff_id : $coupon->getBatch()->aff_id; } } catch (Am_Exception_Db_NotFound $e) { //coupon not found } if (empty($aff_id)) { $aff_id = $invoice->getUser()->aff_id; } if ($aff_id && empty($invoice->aff_id)) { // set aff_id to invoice for quick access next time $invoice->updateQuick('aff_id', $aff_id); } // run event to get plugins chance choose another affiliate $event = new Am_Event(Bootstrap_Aff::AFF_FIND_AFFILIATE, array('invoice' => $invoice, 'payment' => $payment)); $event->setReturn($aff_id); $this->getDi()->hook->call($event); $aff_id = $event->getReturn(); if (empty($aff_id)) { return; } // no affiliate id registered if ($aff_id == $invoice->getUser()->pk()) { return; } //strange situation // load affiliate and continue $aff = $this->getDi()->userTable->load($aff_id, false); if (!$aff || !$aff->is_affiliate) { return; } // affiliate not found $user = $invoice->getUser(); if (!$user->aff_id) { $user->aff_id = $aff->pk(); $user->aff_added = sqlTime('now'); $user->data()->set('aff-source', 'invoice-' . $invoice->pk()); $user->save(); } // try to load other tier affiliate $aff_tier = $aff; $aff_tiers = array(); $aff_tiers_exists = array($aff->pk()); for ($tier = 1; $tier <= $this->getMaxTier(); $tier++) { if (!$aff_tier->aff_id || $aff_tier->pk() == $invoice->getUser()->pk()) { break; } $aff_tier = $this->getDi()->userTable->load($aff_tier->aff_id, false); if (!$aff_tier || !$aff_tier->is_affiliate || $aff_tier->pk() == $invoice->getUser()->pk() || in_array($aff_tier->pk(), $aff_tiers_exists)) { //already in chain break; } $aff_tiers[$tier] = $aff_tier; $aff_tiers_exists[] = $aff_tier->pk(); } $isFirst = !$payment || $payment->isFirst(); //to define price field $paymentNumber = is_null($payment) ? 0 : $invoice->getPaymentsCount(); if (!$payment) { $tax = 0; } else { $tax = $this->getDi()->config->get('aff.commission_include_tax', false) ? 0 : doubleval($payment->tax); } $amount = $payment ? $payment->amount - $tax : 0; $date = $payment ? $payment->dattm : 'now'; // now calculate commissions $items = is_null($payment) ? array_slice($invoice->getItems(), 0, 1) : $invoice->getItems(); foreach ($items as $item) { //we do not calculate commission for free items in invoice $prefix = $isFirst ? 'first' : 'second'; if (!is_null($payment) && !(double) $item->get("{$prefix}_total")) { continue; } $comm = $this->getDi()->affCommissionRecord; $comm->date = sqlDate($date); $comm->record_type = AffCommission::COMMISSION; $comm->invoice_id = $invoice->invoice_id; $comm->invoice_item_id = $item->invoice_item_id; $comm->invoice_payment_id = $payment ? $payment->pk() : null; $comm->receipt_id = $payment ? $payment->receipt_id : null; $comm->product_id = $item->item_id; $comm->is_first = $paymentNumber <= 1; $comm->_setPayment($payment); $comm->_setInvoice($invoice); $comm_tier = clone $comm; $rules = array(); $topay_this = $topay = $this->calculate($invoice, $item, $aff, $paymentNumber, 0, $amount, $date, $rules); if ($topay > 0) { $comm->aff_id = $aff->pk(); $comm->amount = $topay; $comm->tier = 0; $comm->_setAff($aff); $comm->insert(); $comm->setCommissionRules(array_map(create_function('$el', 'return $el->pk();'), $rules)); } foreach ($aff_tiers as $tier => $aff_tier) { $rules = array(); $topay_this = $this->calculate($invoice, $item, $aff_tier, $paymentNumber, $tier, $topay_this, $date, $rules); if ($topay_this > 0) { $comm_this = clone $comm_tier; $comm_this->aff_id = $aff_tier->pk(); $comm_this->amount = $topay_this; $comm_this->tier = $tier; $comm_this->_setAff($aff_tier); $comm_this->insert(); $comm_this->setCommissionRules(array_map(create_function('$el', 'return $el->pk();'), $rules)); } } } }
/** * Return unused amount for $item subscription * null will be returned if: * - subscription is lifetime * - subscription is for free product * - subscription is expired * @param Invoice $invoice * @param InvoiceItem $item * @return float|null */ function getUnusedAmount(Invoice $invoice, InvoiceItem $item) { $row = $this->getDi()->db->selectRow("\n SELECT begin_date, expire_date \n FROM ?_access\n WHERE invoice_id=?d AND product_id=?\n ORDER by expire_date desc LIMIT 1 \n ", $invoice->pk(), $item->item_id); if (!$row) { return; } $maxExpire = $row['expire_date']; $maxBegin = $row['begin_date']; if ($maxExpire < $this->getDi()->sqlDate) { return null; } if ($maxExpire == Am_Period::MAX_SQL_DATE) { return null; } $daysTotal = $this->diffDays($maxBegin, $maxExpire); $daysUnused = $this->diffDays($this->getDi()->sqlDate, $maxExpire) - 1; // -1 as today date can be fully used $pc = $invoice->getPaymentsCount(); $field = $pc == 1 && (double) $invoice->first_total || $pc == 0 ? 'first_total' : 'second_total'; $paid = $item->get($field); return moneyRound($daysUnused * $paid / $daysTotal); }