Beispiel #1
0
 public function init()
 {
     parent::init();
     $this->setTitle('Edit Membership Plan')->setDescription('Please note that payment parameters (Price, ' . 'Recurrence, Duration, Trial Duration) cannot be edited after ' . 'creation. If you wish to change these, you will have to create a ' . 'new plan and disable the current one.');
     /*
        // Disable some elements
        $this->getElement('price')
            ->setIgnore(true)
            ->setAttrib('disable', true)
            ->clearValidators()
            ->setRequired(false)
            ->setAllowEmpty(true)
            ;
        $this->getElement('recurrence')
            ->setIgnore(true)
            ->setAttrib('disable', true)
            ->clearValidators()
            ->setRequired(false)
            ->setAllowEmpty(true)
            ;
        $this->getElement('duration')
            ->setIgnore(true)
            ->setAttrib('disable', true)
            ->clearValidators()
            ->setRequired(false)
            ->setAllowEmpty(true)
            ;
     * 
     */
     // Change the submit label
     $this->getElement('execute')->setLabel('Edit Plan');
 }
 public function createAction()
 {
     // Make form
     $this->view->form = $form = new Payment_Form_Admin_Package_Create();
     // Get supported billing cycles
     $gateways = array();
     $supportedBillingCycles = array();
     $partiallySupportedBillingCycles = array();
     $fullySupportedBillingCycles = null;
     $gatewaysTable = Engine_Api::_()->getDbtable('gateways', 'payment');
     foreach ($gatewaysTable->fetchAll() as $gateway) {
         $gateways[$gateway->gateway_id] = $gateway;
         $supportedBillingCycles[$gateway->gateway_id] = $gateway->getGateway()->getSupportedBillingCycles();
         $partiallySupportedBillingCycles = array_merge($partiallySupportedBillingCycles, $supportedBillingCycles[$gateway->gateway_id]);
         if (null === $fullySupportedBillingCycles) {
             $fullySupportedBillingCycles = $supportedBillingCycles[$gateway->gateway_id];
         } else {
             $fullySupportedBillingCycles = array_intersect($fullySupportedBillingCycles, $supportedBillingCycles[$gateway->gateway_id]);
         }
     }
     $partiallySupportedBillingCycles = array_diff($partiallySupportedBillingCycles, $fullySupportedBillingCycles);
     $multiOptions = array_combine(array_map('strtolower', $fullySupportedBillingCycles), $fullySupportedBillingCycles);
     $form->getElement('recurrence')->setMultiOptions($multiOptions);
     $form->getElement('recurrence')->options['forever'] = 'One-time';
     $form->getElement('duration')->setMultiOptions($multiOptions);
     $form->getElement('duration')->options['forever'] = 'Forever';
     /*
         $form->getElement('trial_duration')
      ->setMultiOptions($multiOptions)
      //->setDescription('-')
      ;
         $form->getElement('trial_duration')->options['Fully Supported']['forever'] = 'None';
         //$form->getElement('trial_duration')->setValue('0 forever');
     * 
     */
     // Check method/data
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $values = $form->getValues();
     $tmp = $values['recurrence'];
     unset($values['recurrence']);
     if (empty($tmp) || !is_array($tmp)) {
         $tmp = array(null, null);
     }
     $values['recurrence'] = (int) $tmp[0];
     $values['recurrence_type'] = $tmp[1];
     $tmp = $values['duration'];
     unset($values['duration']);
     if (empty($tmp) || !is_array($tmp)) {
         $tmp = array(null, null);
     }
     $values['duration'] = (int) $tmp[0];
     $values['duration_type'] = $tmp[1];
     /*
         $tmp = $values['trial_duration'];
         unset($values['trial_duration']);
         if( empty($tmp) || !is_array($tmp) ) {
      $tmp = array(null, null);
         }
         $values['trial_duration'] = (int) $tmp[0];
         $values['trial_duration_type'] = $tmp[1];
     * 
     */
     if (!empty($values['default']) && (double) $values['price'] > 0) {
         return $form->addError('Only a free plan may be the default plan.');
     }
     $packageTable = Engine_Api::_()->getDbtable('packages', 'payment');
     $db = $packageTable->getAdapter();
     $db->beginTransaction();
     try {
         // Update default
         if (!empty($values['default'])) {
             $packageTable->update(array('default' => 0), array('`default` = ?' => 1));
         }
         // Create package
         $package = $packageTable->createRow();
         $package->setFromArray($values);
         $package->save();
         // Create package in gateways?
         if (!$package->isFree()) {
             $gatewaysTable = Engine_Api::_()->getDbtable('gateways', 'payment');
             foreach ($gatewaysTable->fetchAll(array('enabled = ?' => 1)) as $gateway) {
                 $gatewayPlugin = $gateway->getGateway();
                 // Check billing cycle support
                 if (!$package->isOneTime()) {
                     $sbc = $gateway->getGateway()->getSupportedBillingCycles();
                     if (!in_array($package->recurrence_type, array_map('strtolower', $sbc))) {
                         continue;
                     }
                 }
                 if (method_exists($gatewayPlugin, 'createProduct')) {
                     $gatewayPlugin->createProduct($package->getGatewayParams());
                 }
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     // Redirect
     return $this->_helper->redirector->gotoRoute(array('action' => 'index'));
 }