/**
  * Creates a new Purchase model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $cc_format_mdl = new CCFormat();
     $purchase_mdl = new Purchase();
     if (!empty(Yii::$app->request->post())) {
         // the CCFormat does not actualy save anything to the DB.
         if (!$cc_format_mdl->load(Yii::$app->request->post()) || !$cc_format_mdl->validate()) {
             Yii::$app->getSession()->setFlash('error', 'Card data not valid.');
             return false;
         }
         // save the purchase to the DB if the purchase data is valid
         if (!$purchase_mdl->load(Yii::$app->request->post()) || !$purchase_mdl->validate()) {
             Yii::$app->getSession()->setFlash('error', 'Billing data not valid.');
             return false;
         }
         // save purchase data
         // process PayPal payment
         $paypalResponse = $this->pay($this->prepPayPalData($purchase_mdl, $cc_format_mdl));
         if ($paypalResponse->state == 'approved' && $purchase_mdl->save()) {
             $purchase_mdl->setAttributes(['last_4' => substr($cc_format_mdl->number, -4, 4), 'price' => $paypalResponse->transactions[0]->amount->total]);
             if ($purchase_mdl->save()) {
                 if ($this->module->requestedAction->id == 'adddevice' || $this->module->requestedAction->id == 'create') {
                     // create devices if the calling methid was 'device'
                     DeviceController::actionCreate($purchase_mdl);
                 } elseif ($this->module->requestedAction->id == 'addtime') {
                     // create time if the calling method was 'time'
                     DeviceController::actionUpdate(null, $purchase_mdl);
                 }
                 \Yii::$app->getSession()->setFlash('success', 'Payment processed, account updated.');
                 \Yii::$app->response->redirect('index');
             }
         } else {
             Yii::$app->getSession()->setFlash('error', 'Payment processor returned an error.');
             return false;
         }
     }
     // no post data, load form
     return $this->render('create', ['cc_format_mdl' => $cc_format_mdl, 'country_mdl' => Country::findAll(['deleted_at' => null]), 'device_count_options_mdl' => DeviceCountOptions::findAll(['deleted_at' => null]), 'purchase_mdl' => $purchase_mdl, 'time_options_mdl' => TimeAmountOptions::findAll(['deleted_at' => null])]);
 }
Example #2
0
 /**
  * Updates an existing Device model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public static function actionUpdate($id, $param_data = null)
 {
     // update all the devices of a user as per user_id
     if ($param_data) {
         $new_exp = DeviceController::getCurrentExpiration($param_data);
         $time = TimeAmountOptions::find()->where(['id' => $param_data['time_amount_id']])->one()->getAttribute('key');
         if (Device::updateAll(['expiration' => $new_exp], ['user_id' => $param_data['user_id']]) && RadCheckController::actionUpdate(null, $param_data, $new_exp)) {
             Yii::$app->getSession()->setFlash('success', $time . ' added to your account.');
             return $new_exp;
         }
         Yii::$app->getSession()->setFlash('error', 'Unable to add ' . $time . ' to your account.');
         return false;
     }
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }