modules/api/v2/controllers/UserSubscriptionController.php
/**
     * Creates a new UserSubscription model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new UserSubscription model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new UserSubscription model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new UserSubscription();
        $postData = \Yii::$app->request->post();
        $userSubscriptionData['UserSubscription'] = $postData;
        if ($model->load($userSubscriptionData) && $model->validate()) {
            $model->user_id = Yii::$app->user->identity->id;
            $modelAddress = (!empty($model->user) && !empty($model->user->userAddresses) && !empty($model->user->userAddresses[0])) ? $model->user->userAddresses[0] : '';
            if ($model->save()) {
                $subscription_package_id = $model->subscription_id;
                $price = $model->subscription->amount;
                $packageName = $model->subscription->name;
                $ownerId = $model->user_id;
                $user_subdcription_id = $model->id;
                $cardType = UserSubscription::CARD_TYPE_VISA;
                if (!empty($model->card_number)) {
                    if ($model->card_number[0] == UserSubscription::CARD_TYPE_VISA_NUMBER) {
                        $cardType = UserSubscription::CARD_TYPE_VISA;
                    } else if (in_array($model->card_number[0], [UserSubscription::CARD_TYPE_MASTER_NUMBER_ONE, UserSubscription::CARD_TYPE_MASTER_NUMBER_TWO])) {
                        $cardType = UserSubscription::CARD_TYPE_MASTER;
                    } else if ($model->card_number[0] == UserSubscription::CARD_TYPE_AMEX_NUMBER) {
                        $cardType = UserSubscription::CARD_TYPE_AMEX;
                    } else if ($model->card_number[0] == UserSubscription::CARD_TYPE_DISCOVER_NUMBER) {
                        $cardType = UserSubscription::CARD_TYPE_DISCOVER;
                    }
                }
                $expMontYear = explode("/", $model->expiry_month_year);
                $cardHoderName = explode(" ", $model->card_holder_name);
                $modelSubscriptionPackage = $model->subscription;
                $paymentRequestData = [
                    'subscription_package_id' => $subscription_package_id,
                    'total' => $price,
                    'package_name' => $packageName,
                    'user_id' => $ownerId,
                    'user_subscription_id' => $user_subdcription_id,
                    'card_type' => $cardType,
                    'card_exp_month' => $expMontYear[0],
                    'card_exp_year' => $expMontYear[1],
                    'card_first_name' => $cardHoderName[0],
                    'card_last_name' => (!empty($cardHoderName[1])) ? $cardHoderName[1] : "Seller",
                    'sub_total' => $modelSubscriptionPackage->amount,
                    'user' => Yii::$app->user->identity,
                    'user_Address' => $modelAddress,
                ];
                $response = $this->makeSubscriptionPayment(array_merge($postData, $paymentRequestData));
                if (!empty($response)) {
                    $model->payment_response = (!empty($response) && !empty($response->getState()) && $response->getState() == 'created') ? $response : "";
                    $model->payment_status = (!empty($response->getState())) ? $response->getState() : 'failed';
                    $model->transaction_id = (!empty($response->getId())) ? $response->getId() : "";
                    $model->save(false);
                }
            }
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new UserSubscription ( ) ; $ postData = \ Yii :: $ app -> request -> post ( ) ; $ userSubscriptionData [ 'UserSubscription' ] = $ postData ; if ( $ model -> load ( $ userSubscriptionData ) && $ model -> validate ( ) ) { $ model -> user_id = Yii :: $ app -> user -> identity -> id ; $ modelAddress = ( ! empty ( $ model -> user ) && ! empty ( $ model -> user -> userAddresses ) && ! empty ( $ model -> user -> userAddresses [ 0 ] ) ) ? $ model -> user -> userAddresses [ 0 ] : '' ; if ( $ model -> save ( ) ) { $ subscription_package_id = $ model -> subscription_id ; $ price = $ model -> subscription -> amount ; $ packageName = $ model -> subscription -> name ; $ ownerId = $ model -> user_id ; $ user_subdcription_id = $ model -> id ; $ cardType = UserSubscription :: CARD_TYPE_VISA ; if ( ! empty ( $ model -> card_number ) ) { if ( $ model -> card_number [ 0 ] == UserSubscription :: CARD_TYPE_VISA_NUMBER ) { $ cardType = UserSubscription :: CARD_TYPE_VISA ; } else if ( in_array ( $ model -> card_number [ 0 ] , [ UserSubscription :: CARD_TYPE_MASTER_NUMBER_ONE , UserSubscription :: CARD_TYPE_MASTER_NUMBER_TWO ] ) ) { $ cardType = UserSubscription :: CARD_TYPE_MASTER ; } else if ( $ model -> card_number [ 0 ] == UserSubscription :: CARD_TYPE_AMEX_NUMBER ) { $ cardType = UserSubscription :: CARD_TYPE_AMEX ; } else if ( $ model -> card_number [ 0 ] == UserSubscription :: CARD_TYPE_DISCOVER_NUMBER ) { $ cardType = UserSubscription :: CARD_TYPE_DISCOVER ; } } $ expMontYear = explode ( " / " , $ model -> expiry_month_year ) ; $ cardHoderName = explode ( " " , $ model -> card_holder_name ) ; $ modelSubscriptionPackage = $ model -> subscription ; $ paymentRequestData = [ 'subscription_package_id' => $ subscription_package_id , 'total' => $ price , 'package_name' => $ packageName , 'user_id' => $ ownerId , 'user_subscription_id' => $ user_subdcription_id , 'card_type' => $ cardType , 'card_exp_month' => $ expMontYear [ 0 ] , 'card_exp_year' => $ expMontYear [ 1 ] , 'card_first_name' => $ cardHoderName [ 0 ] , 'card_last_name' => ( ! empty ( $ cardHoderName [ 1 ] ) ) ? $ cardHoderName [ 1 ] : " Seller " , 'sub_total' => $ modelSubscriptionPackage -> amount , 'user' => Yii :: $ app -> user -> identity , 'user_Address' => $ modelAddress , ] ; $ response = $ this -> makeSubscriptionPayment ( array_merge ( $ postData , $ paymentRequestData ) ) ; if ( ! empty ( $ response ) ) { $ model -> payment_response = ( ! empty ( $ response ) && ! empty ( $ response -> getState ( ) ) && $ response -> getState ( ) == 'created' ) ? $ response : " " ; $ model -> payment_status = ( ! empty ( $ response -> getState ( ) ) ) ? $ response -> getState ( ) : 'failed' ; $ model -> transaction_id = ( ! empty ( $ response -> getId ( ) ) ) ? $ response -> getId ( ) : " " ; $ model -> save ( false ) ; } } } return $ model ; }
Creates a new UserSubscription model.
Creates a new UserSubscription model .
// Paypal payment