/** * 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])]); }