PATH:
home
/
letacommog
/
letaweb
/
admin
/
controllers
<?php class PaypalController extends PController { public $layout = 'column_checkout'; public function init() { parent::init(); Yii::app()->errorHandler->errorAction = 'paypal/error'; } public function actionIndex() { $planId = $_REQUEST['planId']; $type = isset($_REQUEST['wwwt']) ? $_REQUEST['wwwt'] : 'month'; $prices = Yii::app()->params['price']['prices']; $indexes = Yii::app()->params['price']['indexes']; $plan = Plan::model()->findByPk($planId); $price = $plan->monthly_amount; if ($type == 'annual') { $price = $plan->yearly_amount * 12; } $paypal_express = Yii::app()->payment; $stype = ($type == 'month' ? 'Monthly' : 'Annually'); $description = $stype.$plan->name.'Membership'; $response_array = $paypal_express->setExpressCheckout( array( 'items' => array( array( 'name' => $plan->name, 'amount' => $price, 'quantity' => 1, ), ), 'cancel_url' => $this->createAbsoluteUrl('paypal/cancel', array('planId' => $planId, 'wwwt' => $type)), 'return_url' => $this->createAbsoluteUrl('paypal/confirm', array('planId' => $planId, 'wwwt' => $type)), 'is_recurrent_payment' => true, 'currency' => $plan->currency, 'recurringbilling_description' => $description, ) ); if ($paypal_express->transaction_server == 'Live') { $paypal_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='; } else { $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='; } if (($response_array->Ack == 'Success') || ($response_array->Ack == 'SuccessWithWarning')) { $this->redirect($paypal_url.'&token='.$response_array->Token.'&useraction=commit'); } else { $error = $response_array->Errors[0]->LongMessage; throw new CHttpException(501, $error); } } public function actionCancel() { $this->render('cancel'); } public function actionError() { if ($error = Yii::app()->errorHandler->error) { $this->render('error', array('error' => $error)); } } public function actionConfirm() { $token = $_REQUEST['token']; $type = $_REQUEST['wwwt']; $planId = $_REQUEST['planId']; $plan = Plan::model()->findByPk($planId); $price = $plan->monthly_amount; if ($type == 'annual') { $price = $plan->yearly_amount * 12; } $paypal_express = Yii::app()->payment; $activeSubscriptions = Subscription::model()->findAll('site_id=:site_id and is_active=1 and type=:type', array(':type'=>'paypal',':site_id' => Yii::app()->site->model->id)); foreach ($activeSubscriptions as $subscription) { $response = $paypal_express->ManageRecurringPaymentsProfileStatus(array( 'ProfileID' => $subscription->profile_id, 'Action' => 'Cancel', )); if (($response->Ack == 'Failure')) { $error = $response->Errors[0]->LongMessage; throw new CHttpException(501, $error); } $subscription->is_active = 0; $subscription->status = 'Cancel'; $subscription->save(); } $response = $paypal_express->getExpressCheckoutDetails($token); if (($response->Ack == 'Success') || ($response->Ack == 'SuccessWithWarning')) { $GetExpressCheckoutDetailsResponseDetails = $response->GetExpressCheckoutDetailsResponseDetails; $doExpressCheckoutPaymentResponse = $paypal_express->doExpressCheckoutPayment( array( 'Token' => $GetExpressCheckoutDetailsResponseDetails->Token, 'PaymentDetails' => $GetExpressCheckoutDetailsResponseDetails->PaymentDetails, 'PayerID' => $GetExpressCheckoutDetailsResponseDetails->PayerInfo->PayerID, )); if (($doExpressCheckoutPaymentResponse->Ack == 'Success') || ($doExpressCheckoutPaymentResponse->Ack == 'SuccessWithWarning')) { $totalAmount = 0; $CurrencyID = $plan->currency; $PaymentDetails = $GetExpressCheckoutDetailsResponseDetails->PaymentDetails; foreach ($PaymentDetails as $paymentDetail) { $totalAmount += $paymentDetail->OrderTotal->value; $CurrencyID = $paymentDetail->OrderTotal->currencyID; } $stype = ($type == 'month' ? 'Monthly' : 'Annually'); $description = $stype.$plan->name.'Membership'; $str_addtimes = $type == 'year' ? ' + 1 year' : ' +1 month'; $bill_start_date = gmdate("Y-m-d\TH:i:s\Z", strtotime($str_addtimes)); $CreateRecurringPaymentsProfileResponse = $paypal_express->createPaymentRecurrentProfile(array( 'StartDate' => $bill_start_date, 'Token' => $GetExpressCheckoutDetailsResponseDetails->Token, 'Period' => ($type == 'annual' ? 'Year' : 'Month'), 'Frequency' => 1, 'CurrencyID' => $CurrencyID, 'Amount' => $totalAmount, 'Description' => $description, 'ProfileReference' => Yii::app()->site->model->id, )); $profileID = $CreateRecurringPaymentsProfileResponse->CreateRecurringPaymentsProfileResponseDetails->ProfileID; $transactionID = $doExpressCheckoutPaymentResponse->DoExpressCheckoutPaymentResponseDetails->PaymentInfo[0]->TransactionID; $siteModel = Yii::app()->site->model; $siteModel->plan_id = $planId; $siteModel->can_expire = 1; $type == 'annual' ? $siteModel->extendByYear(1) : $siteModel->extendByMonth(1); $siteModel->save(); $subscription = new Subscription(); $subscription->profile_id = $profileID; $subscription->site_id = $siteModel->id; $subscription->plan_id = $planId; $subscription->cycle = $type; $subscription->is_active = 1; $subscription->last_transaction_id = $transactionID; $now = date('Y-m-d H:i:s'); $subscription->last_payment_date = $now; $subscription->last_payment_amount = $totalAmount; $subscription->date_added = $now; $subscription->date_modified = $now; $subscription->creator_id = Yii::app()->user->id; $subscription->last_modified_by = Yii::app()->user->id; $subscription->save(); $purchase = new Purchase(); $purchase->subscription_id = $subscription->id; $purchase->site_id = $siteModel->id; $purchase->cycle = $type; $purchase->amount = $totalAmount; $purchase->currency_code = $CurrencyID; $purchase->token = $GetExpressCheckoutDetailsResponseDetails->Token; $purchase->transaction_id = $transactionID; $now = date('Y-m-d H:i:s'); $purchase->date_added = $now; $purchase->date_modified = $now; $purchase->creator_id = Yii::app()->user->id; $purchase->last_modified_by = Yii::app()->user->id; $purchase->save(); $this->render('confirm'); } else { $error = $doExpressCheckoutPaymentResponse->Errors[0]->LongMessage; throw new CHttpException(501, $error); } } else { $error = $response->Errors[0]->LongMessage; throw new CHttpException(501, $error); } } }
[+]
..
[-] AjaxController.php
[edit]
[-] SiteController.php
[edit]
[-] IpnController.php
[edit]
[-] StatController.php
[edit]
[-] BuilderController.php
[edit]
[-] BlogController.php
[edit]
[-] UserController.php
[edit]
[-] CheckoutController.php
[edit]
[-] PaypalController.php
[edit]