/** * Creates subscription at paypal * @param AW_Sarp_Model_Subscription $Subscription * @return */ public function createSubscription(AW_Sarp_Model_Subscription $Subscription, $Order, $Quote) { // 1. Create subscription profile with 0 period // 2. Save CCInfoKey and CustomerInfoKey $Payment = $Quote->getPayment(); // Credit Card number $ccNumber = Mage::getSingleton('customer/session')->getSarpCcNumber(); // CVV //$ccCode = Mage::getSingleton('customer/session')->getSarpCcCid(); // Glue month and year $expirationDate = sprintf("%02s", $Payment->getMethodInstance()->getInfoInstance()->getCcExpMonth()) . $Payment->getMethodInstance()->getInfoInstance()->getCcExpYear(); // Customer name $customerName = $Quote->getBillingAddress()->getFirstname() . " " . $Quote->getBillingAddress()->getLastname(); // Calculate start date $Date = $Subscription->getNextSubscriptionEventDate(new Zend_Date($Subscription->getDateStart())); $Date->addDayOfYear(0 - $Subscription->getPeriod()->getPaymentOffset()); $date_start = $this->getWebService()->formatDate($Date); // Total $total = floatval($Subscription->getLastOrder()->getGrandTotal()); $this->getWebService()->getRequest()->reset()->setData(array('CcAccountNum' => $ccNumber, 'CcExpDate' => $expirationDate, 'FirstName' => $Quote->getBillingAddress()->getFirstname(), 'CustomerName' => $customerName, 'CustomerID' => $Quote->getCustomerEmail(), 'LastName' => $Quote->getBillingAddress()->getLastname(), 'BillingInterval' => "0", 'BillingPeriod' => "DAY", 'StartDate' => $date_start, 'EndDate' => $date_start, 'BillAmt' => $total, 'TotalAmt' => $total, 'ContractName' => Mage::helper('sarp')->__("Subscription #%s", $Subscription->getId()), 'ContractID' => $Subscription->getId())); try { $result = $this->getWebService()->addRecurringCreditCard(); $CCInfoKey = $result->CcInfoKey; $CustomerKey = $result->CustomerKey; /** * @todo Add and save ContractKey */ $Subscription->setRealId($CustomerKey)->setRealPaymentId($CCInfoKey)->save(); } catch (Exception $E) { throw new AW_Sarp_Exception("Argofire recurrent profile creation failed for subscription #{$Subscription->getId()} " . "and order #{$Order->getId()}", print_r($result, 1)); } $this->log(print_r($result, 1)); }
/** * Presets flat data from subscription * @param AW_Sarp_Model_Subscription $Subscription * @return AW_Sarp_Model_Subscription_Flat */ public function setSubscription(AW_Sarp_Model_Subscription $Subscription) { Zend_Date::setOptions(array('extend_month' => true)); // Fix Zend_Date::addMonth unexpected result if (!$Subscription->isInfinite()) { $expireDate = $Subscription->getDateExpire()->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); } else { $expireDate = null; } if ($Subscription->getIsNew()) { $lastOrderAmount = 0; $quote = clone $Subscription->getQuote(); foreach ($quote->getItemsCollection() as $Item) { if (Mage::helper('sarp')->isSubscriptionType($Item)) { $quote->removeItem($Item->getId()); } $quote->getShippingAddress()->setCollectShippingRates(true)->collectTotals(); $quote->collectTotals(); } $lastOrderAmount = $Subscription->getQuote()->getGrandTotal(); $virtual = $quote->getIsVirtual(); unset($quote); } else { $lastOrderAmount = $Subscription->getLastOrder()->getGrandTotal(); $virtual = $Subscription->getLastOrder()->getIsVirtual(); } if ($Subscription->isActive()) { $paymentOffset = $Subscription->getPeriod()->getPaymentOffset(); // Get next payment date if (!$Subscription->getLastPaidDate()) { $nextPaymentDate = $Subscription->getLastOrder()->getCreatedAtStoreDate(); $nextPaymentDate = $Subscription->getNextSubscriptionEventDate($Subscription->getDateStart()); $nextDeliveryDate = clone $Subscription->getDateStart(); $nextDeliveryDate->addDayOfYear(0 + floatval($paymentOffset)); } else { $nextPaymentDate = $Subscription->getNextSubscriptionEventDate(); } if ($paymentOffset) { if (!$Subscription->getLastPaidDate()) { // No payments made yet $lastOrderDate = clone $Subscription->getDateStart(); $lastOrderDate->addDayOfYear(0 - floatval($paymentOffset)); } else { $lastOrderDate = $Subscription->getLastOrder()->getCreatedAtStoreDate(); } $probablyDeliveryDate = clone $lastOrderDate; $probablyDeliveryDate = $probablyDeliveryDate->addDayOfYear(floatval($paymentOffset)); if ($probablyDeliveryDate->compare(new Zend_Date(), Zend_Date::DATE_SHORT) > 0) { $nextDeliveryDate = clone $lastOrderDate; } $nextPaymentDate->addDayOfYear(0 - floatval($paymentOffset)); } if (!isset($nextDeliveryDate)) { $nextDeliveryDate = clone $nextPaymentDate; } $nextDeliveryDate = $nextDeliveryDate->addDayOfYear(floatval($paymentOffset))->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); $nextPaymentDate = $nextPaymentDate->toString(AW_Sarp_Model_Subscription::DB_DATE_FORMAT); } else { // Drop next payment date if subscription is suspended $nextDeliveryDate = $nextPaymentDate = null; } $this->setSubscriptionId($Subscription->getId())->setCustomerName($Subscription->getCustomer()->getName())->setCustomerEmail($Subscription->getCustomer()->getEmail())->setFlatLastOrderStatus($Subscription->getLastOrder()->getStatus())->setFlatLastOrderAmount($lastOrderAmount)->setFlatLastOrderCurrencyCode($Subscription->getLastOrder()->getOrderCurrencyCode())->setFlatDateExpire($expireDate)->setHasShipping(1 - $virtual)->setFlatNextPaymentDate($nextPaymentDate)->setFlatNextDeliveryDate($nextDeliveryDate)->setProductsText($this->_convertProductsText($Subscription))->setProductsSku($this->_convertProductsSku($Subscription)); $Subscription->setCustomerName($Subscription->getCustomer()->getName())->setCustomerEmail($Subscription->getCustomer()->getEmail())->setFlatLastOrderStatus($Subscription->getLastOrder()->getStatus())->setFlatLastOrderAmount($lastOrderAmount)->setFlatLastOrderCurrencyCode($Subscription->getLastOrder()->getOrderCurrencyCode())->setFlatDateExpire($expireDate)->setHasShipping(1 - $virtual)->setFlatNextPaymentDate($nextPaymentDate)->setFlatNextDeliveryDate($nextDeliveryDate)->setProductsText($this->_convertProductsText($Subscription))->setProductsSku($this->_convertProductsSku($Subscription)); return $this; }
/** * Creates subscription at paypal * @param AW_Sarp_Model_Subscription $Subscription * @return */ public function createSubscription(AW_Sarp_Model_Subscription $Subscription, $Order, $Quote) { $Payment = $Quote->getPayment(); $ccNumber = Mage::getSingleton('customer/session')->getSarpCcNumber(); $expirationDate = $Payment->getMethodInstance()->getInfoInstance()->getCcExpMonth() . $Payment->getMethodInstance()->getInfoInstance()->getCcExpYear(); $ccCode = Mage::getSingleton('customer/session')->getSarpCcCid(); $ccType = $this->_convertCCType($Payment->getMethodInstance()->getInfoInstance()->getCcType()); $Date = $Subscription->getNextSubscriptionEventDate(new Zend_Date($Subscription->getDateStart())); $Date->addDayOfYear(0 - $Subscription->getPeriod()->getPaymentOffset()); $date_start = date('c', $Date->getTimestamp()); //$amount = floatval($Subscription->getLastOrder()->getGrandTotal()); // Due to first price can be different than AW_Sarp_Model_Product_Price::$doNotApplyFirstPeriodPrice = true; $order = clone $Order; $order->setReordered(true); //Mage::getSingleton('adminhtml/session_quote')->setQuote(null); $no = Mage::getSingleton('sarp/order_create')->reset(); $no->setRecollect(1); $no->getSession()->setUseOldShippingMethod(true); $no->setSendConfirmation(false); $no->setData('account', $Subscription->getCustomer()->getData()); $arr = array(); foreach ($Subscription->getItems() as $Item) { $arr[] = $Item->getPrimaryOrderItemId(); } $no->setItemIdFilter($arr); /*$Q = $no->getQuote(); foreach ($Q->getItemsCollection() as $QI) { $Q->removeItem($QI->getId()); }*/ $no->initFromOrder($order); // // $Q = $no->getQuote(); // foreach ($Q->getAllAddresses() as $address) { // $address->setCollectShippingRates(true); // } // $no->collectShippingRates(); $amount = $no->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->getGrandTotal(); //$no->reset(); /*$Q = $no->getQuote(); foreach ($Q->getItemsCollection() as $QI) { $Q->removeItem($QI->getId()); }*/ // echo $amount; // global $Fi; // if(!isset($Fi)) $Fi =1; // else $Fi ++; // // if($Fi >=2) die; // else return; $this->getWebService()->getRequest()->reset()->setData(array('CREDITCARDTYPE' => $ccType, 'ACCT' => $ccNumber, 'CVV2' => $ccCode, 'EXPDATE' => $expirationDate, 'FIRSTNAME' => $Quote->getBillingAddress()->getFirstname(), 'LASTNAME' => $Quote->getBillingAddress()->getLastname(), 'CURRENCYCODE' => $Subscription->getLastOrder()->getOrderCurrencyCode(), 'PROFILESTARTDATE' => $date_start, 'AMT' => $amount, 'BILLINGPERIOD' => $this->_convertPeriod($Subscription->getPeriod()), 'BILLINGFREQUENCY' => $Subscription->getPeriod()->getPeriodValue(), 'DESC' => Mage::helper('sarp')->__("Subscription #%s", $Subscription->getId()), 'SUBSCRIBERNAME' => $Quote->getBillingAddress()->getFirstname() . " " . $Quote->getBillingAddress()->getLastname(), 'PROFILEREFERENCE' => $Order->getIncrementId() . "-{$Subscription->getId()}", 'EMAIL' => $Quote->getCustomerEmail(), 'STREET' => $Quote->getBillingAddress()->getStreet(0), 'CITY' => $Quote->getBillingAddress()->getCity(), 'STATE' => $Quote->getBillingAddress()->getRegion(), 'COUNTRYCODE' => $Quote->getBillingAddress()->getCountryId(), 'ZIP' => $Quote->getBillingAddress()->getPostcode())); if (!$Subscription->isInfinite()) { // Subscription has expire date $this->getWebService()->getRequest()->setData('TOTALBILLINGCYCLES', $this->_getTotalBillingCycles($Subscription)); } $result = $this->getWebService()->runRequest('CreateRecurringPaymentsProfile'); if (isset($result['PROFILEID'])) { // Profile created $profileId = $result['PROFILEID']; $Subscription->setRealId($profileId)->save(); } else { throw new AW_Sarp_Exception("PayPal recurrent profile creation failed for subscription #{$Subscription->getId()} and order #{$Order->getId()}", print_r($result, 1)); } $this->log(print_r($result, 1)); }