Beispiel #1
0
 /**
  * Converts subscription products to sku text
  * @param AW_Sarp_Model_Subscription $Subscription
  * @return
  */
 protected function _convertProductsSku(AW_Sarp_Model_Subscription $Subscription)
 {
     $out = array();
     foreach ($Subscription->getItems() as $Item) {
         $out[] = $Item->getOrderItem()->getSku();
     }
     return implode(self::DB_DELIMITER, $out);
 }
 /**
  * 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));
 }