Пример #1
0
 public function actionCreate()
 {
     $this->checkAccess("create");
     $transaction = Yii::$app->getDb()->beginTransaction();
     try {
         $card_number = Yii::$app->request->post('card_number');
         $card_exp_month = Yii::$app->request->post('card_exp_month');
         $card_exp_year = Yii::$app->request->post('card_exp_year');
         $card_cvc = Yii::$app->request->post('card_cvc');
         $token_wpay = Yii::$app->request->post('token');
         $name_wpay = Yii::$app->request->post('name');
         $description_wpay = Yii::$app->request->post('description');
         $_3ds_wpay = false;
         $authoriseOnly_wpay = false;
         $billing_address = array("address1" => Yii::$app->request->post('address1'), "postalCode" => Yii::$app->request->post('postcode'), "city" => Yii::$app->request->post('city'), "state" => Yii::$app->request->post('state'), "countryCode" => Yii::$app->request->post('countryCode'));
         $class_id = Yii::$app->request->post('class_id');
         $class = Classes::findOne($class_id);
         if (!is_object($class)) {
             throw new Exception("Class not found", 404);
         }
         if ($class->class_stime <= time()) {
             throw new Exception("You cannot do this, time out for this class.", 404);
         }
         $user = User::findIdentityByAccessToken(Yii::$app->request->get('access-token'));
         if ($user->user_type !== "student") {
             throw new Exception("You cannot do this", 404);
         }
         if ($user->isPaidClass($class)) {
             throw new Exception("You already paid for this class", 400);
         }
         $worldpay = new WorldpayHelper($this->worldpayKey);
         $price = Coupons::getClassPrice(Yii::$app->request->post('coupon'), $class->class_price);
         $result = $worldpay->createOrder(array('token' => $token_wpay, 'orderDescription' => "Buying class {$class->class_name}(id:{$class->class_id})", 'amount' => $price, 'is3DSOrder' => false, 'authoriseOnly' => false, 'orderType' => 'ECOM', 'currencyCode' => 'USD', 'name' => $name_wpay, 'billingAddress' => $billing_address, 'customerIdentifiers' => array('my-customer-ref' => 'customer-ref'), 'customerOrderCode' => 'A123'));
         if (array_key_exists('error', $result)) {
             throw new Exception($result["error"], 500);
         }
         if ($result['paymentStatus'] !== 'SUCCESS' && $result['paymentStatus'] !== 'AUTHORIZED') {
             throw new Exception("Paid was not successful. Try again later");
         }
         //add user to view class
         $user->link('classes', $class);
         $transactionData = array("user_id" => $user->user_id, "transation_id" => $result['orderCode'], "description" => "Buying class {$class->class_name}", "status" => $result["paymentStatus"], "amount" => $price / 100, "currency" => 'USD', "class_id" => $class->class_id);
         $transactionHistoty = new TransactionHistory();
         $transactionHistoty->load($transactionData, '');
         if (!$transactionHistoty->save()) {
             throw new Exception($transactionHistoty->getFirstError());
         }
         $notification = new Notification();
         $notification->load(array("user_id" => $user->user_id, "title" => "You successfuly bought class {$class->class_name}", "text" => "You successfuly bought class {$class->class_name}", "status" => "new"), '');
         $notification->save();
         $name = base64_encode($user->user_id);
         $notificationTrainer = new Notification();
         $notificationTrainer->load(array("user_id" => $class->class_trainer_id, "title" => "User successfuly bought your class.", "text" => "<a href='/public_profile/{$name}'>User</a> successfuly bought your class '{$class->class_name}'.", "status" => "new"), '');
         $notificationTrainer->save();
         $transaction->commit();
         self::sendStudentInvoicePdf($class);
         self::sendTrainerInvoicePdf($class);
         return ApiHelper::successResponse($transactionHistoty);
     } catch (Exception $ex) {
         $transaction->rollBack();
         return ApiHelper::errorResponse($ex->getMessage());
     }
 }
Пример #2
0
 public function actionCheckCoupon()
 {
     $model = new $this->modelClass();
     $user = $model->findIdentityByAccessToken(\Yii::$app->request->get('access-token'));
     if ($user) {
         $post = \Yii::$app->request->post();
         $data = Coupons::checkCoupon($post['coupon']);
         return ApiHelper::successResponse($data);
     }
     return ApiHelper::errorResponse(["Incorrect Access."], 422);
 }