PATH:
home
/
letacommog
/
letaweb
/
admin
/
classes
/
payments
<?php class paypalPayment extends payment_base { var $title = 'Paypal'; public function init() { $basePath = 'application.classes.payments'; if(!file_exists(Yii::getPathofAlias('application.classes.payments'))) { $basePath = 'site_app.classes.payments'; } Yii::import($basePath.'.paypal-merchant-sdk.lib.PayPalAPIInterfaceService.*'); Yii::import($basePath.'.paypal-merchant-sdk.lib.services.*'); Yii::import($basePath.'.paypal-sdk-core.lib.*'); Yii::import($basePath.'.paypal-sdk-core.lib.auth.*'); Yii::import($basePath.'.paypal-sdk-core.lib.common.*'); Yii::import($basePath.'.paypal-sdk-core.lib.exceptions.*'); Yii::import($basePath.'.paypal-sdk-core.lib.formatters.*'); Yii::import($basePath.'.paypal-sdk-core.lib.handlers.*'); Yii::import($basePath.'.paypal-sdk-core.lib.ipn.*'); Yii::import($basePath.'.paypal-sdk-core.lib.transport.*'); } public function renderInlineCheckoutForm() { Yii::app()->controller->renderPartial("checkout/paypal_form"); } public function settings() { return array( 'username'=>array('type'=>'text','label'=>'User Name'), 'password'=>array('type'=>'password','label'=>'Password'), 'signature'=>array('type'=>'text', 'label'=>'Signature'), 'transaction_server'=>array('type'=>'select','values'=>array('Live'=>'Live', 'Sandbox'=>'Sandbox'),'label'=>'Transaction Mode'), 'isActive'=>array('type'=>'checkbox','label'=>'Is Active'), ); } public function doCancel($subscription) { $response = $this->ManageRecurringPaymentsProfileStatus(array( 'ProfileID' => $subscription->profile_id, 'Action' => 'Cancel', )); if (($response->Ack == 'Failure')) { return $response->Errors[0]->LongMessage; //throw new CHttpException(501, $error); } $subscription->is_active = 0; $subscription->status = 'Cancel'; $subscription->save(); return true; } public function handleReturn() { $action = $_GET["action"]; if($action =="cancel") { Yii::app()->controller->render("cancel"); } else if($action =="success") { $siteId = $_REQUEST['siteId']; $site = Site::model()->findByPk($siteId); if(!isset($site)) { $url = Yii::app()->controller->createUrl('page/index'); Yii::app()->controller->redirect($url); } $token = $_REQUEST['token']; $type = $_REQUEST['wwwt']; $planId = $_REQUEST['planId']; $plan = Plan::model()->findByPk($planId); if(!isset($plan)) { $url = Yii::app()->controller->createUrl('page/index'); Yii::app()->controller->redirect($url); } $price = $plan->monthly_amount; if ($type == 'annual') { $price = $plan->yearly_amount * 12; } $paypal_express = $this; $activeSubscriptions = Subscription::model()->findAll('site_id=:site_id and is_active=1 and payment_type=:type', array(':type'=>'paypal',':site_id' => $siteId)); 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' => $siteId, )); $profileID = $CreateRecurringPaymentsProfileResponse->CreateRecurringPaymentsProfileResponseDetails->ProfileID; $transactionID = $doExpressCheckoutPaymentResponse->DoExpressCheckoutPaymentResponseDetails->PaymentInfo[0]->TransactionID; $siteModel = $site; $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->payment_type = 'paypal'; $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(); Yii::app()->controller->render('success'); } else { $error = $doExpressCheckoutPaymentResponse->Errors[0]->LongMessage; echo $error; } } else { $error = $response->Errors[0]->LongMessage; echo $error; } } } public function processFormData() { $siteId = $_REQUEST['siteId']; $site = Site::model()->findByPk($siteId); if(!isset($site)) { Yii::app()->controller->redirect('checkout'); } $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); if(!isset($plan)) { Yii::app()->controller->redirect('checkout'); } $price = $plan->monthly_amount; if ($type == 'annual') { $price = $plan->yearly_amount * 12; } $paypal_express = $this; $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' => Yii::app()->controller->createAbsoluteUrl('checkout/return', array('siteId'=>$siteId,'payment_type'=>'paypal','action'=>'cancel', 'planId' => $planId, 'wwwt' => $type)), 'return_url' => Yii::app()->controller->createAbsoluteUrl('checkout/return', array('siteId'=>$siteId,'payment_type'=>'paypal','action'=>'success','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')) { Yii::app()->controller->redirect($paypal_url.'&token='.$response_array->Token.'&useraction=commit'); } else { $error = $response_array->Errors[0]->LongMessage; throw new CHttpException(501, $error); } } public function setExpressCheckout($params) { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $paymentDetails = new PaymentDetailsType(); $items = $params['items']; foreach ($items as $item) { $itemDetails = new PaymentDetailsItemType(); $itemDetails->Name = $item['name']; $itemAmount = $item['amount']; $itemDetails->Amount = $itemAmount; $itemQuantity = $item['quantity']; $itemDetails->Quantity = $itemQuantity; $paymentDetails->PaymentDetailsItem[] = $itemDetails; } $orderTotal = new BasicAmountType(); $orderTotal->currencyID = isset($params['currency']) ? $params['currency'] : "USD" ; $orderTotal->value = $itemAmount * $itemQuantity; $paymentDetails->OrderTotal = $orderTotal; $paymentDetails->PaymentAction = 'Sale'; $setECReqDetails = new SetExpressCheckoutRequestDetailsType(); $setECReqDetails->PaymentDetails[0] = $paymentDetails; $setECReqDetails->CancelURL = $params['cancel_url'];//; $setECReqDetails->ReturnURL = $params['return_url'];//; if (isset($params['is_recurrent_payment']) && $params['is_recurrent_payment']) { $billingAgreementDetails = new BillingAgreementDetailsType('RecurringPayments'); $billingAgreementDetails->BillingAgreementDescription = $params['recurringbilling_description']; $setECReqDetails->BillingAgreementDetails = array($billingAgreementDetails); } $setECReqType = new SetExpressCheckoutRequestType(); $setECReqType->Version = $this->api_version; $setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails; $setECReq = new SetExpressCheckoutReq(); $setECReq->SetExpressCheckoutRequest = $setECReqType; $setECResponse = $paypalService->SetExpressCheckout($setECReq); if (($setECResponse->Ack != 'Success') && ($setECResponse->Ack != 'SuccessWithWarning')) { $this->sendDebugEmail(); } return $setECResponse; } public function IPNListener() { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $ipnMessage = new PPIPNMessage(null, $config); $log = ''; foreach ($ipnMessage->getRawData() as $key => $value) { $log.=("IPN: $key => $value"); } Yii::log(($log), 'info', 'Paypal IPN Message' ); if ($ipnMessage->validate()) { return $ipnMessage; } else { return false; } } public function handleIPN() { $response = $this->IPNListener(); if ($response == false) { return; } $type = $response->getTransactionType(); $data = $response->getRawData(); switch ($type) { case 'recurring_payment_profile_cancel': if (isset($data['recurring_payment_id'])) { $profile_id = $data['recurring_payment_id']; $subscription = Subscription::model()->find('is_active=1 and profile_id=:profile_id', array(':profile_id' => $profile_id)); if (!isset($subscription)) { break; } $subscription->is_active = 0; $subscription->status = 'Cancel'; $subscription->save(); $this->sendPaypalDebugEmail($subscription->id, $data, "paypal_recurring_cancel"); } break; case 'recurring_payment': if (isset($data['recurring_payment_id']) && isset($data['payment_status']) && ($data['payment_status'] == 'Completed' || $data['payment_status'] == 'Pending')) { $profile_id = $data['recurring_payment_id']; $subscription = Subscription::model()->find('is_active=1 and profile_id=:profile_id', array(':profile_id' => $profile_id)); if (!isset($subscription)) { return; } //$transaction_id = $response->getTransactionId(); //$purchase = Purchase::model()->find('transaction_id=:transaction_id', array(':transaction_id' => $transaction_id)); // if (isset($purchase)) { //return; // } $site = Site::model()->findByPk($subscription->site_id); if (!isset($site)) { return; } $site->plan_id = $subscription->plan_id; $site->can_expire = 1; $subscription->cycle == 'annual' ? $site->extendByYear(1) : $site->extendByMonth(1); $site->save(); $purchase = new Purchase(); $purchase->subscription_id = $subscription->id; $purchase->site_id = $site->id; $purchase->cycle = $subscription->cycle; $purchase->amount = $data['amount']; $purchase->currency_code = $data['currency_code']; $purchase->token = $GetExpressCheckoutDetailsResponseDetails->Token; $purchase->transaction_id = $response->getTransactionId(); $now = date('Y-m-d H:i:s'); $purchase->date_added = $now; $purchase->date_modified = $now; $purchase->creator_id = $subscription->creator_id; $purchase->creator_id = $subscription->creator_id; $purchase->save(); $this->sendPaypalDebugEmail($subscription->id,$data, "paypal_recurring_success"); } break; case 'recurring_payment_profile_created': if (isset($data['recurring_payment_id'])) { $profile_id = $data['recurring_payment_id']; $subscription = Subscription::model()->find('is_active=1 and profile_id=:profile_id', array(':profile_id' => $profile_id)); if (!isset($subscription)) { return; } $this->sendPaypalDebugEmail($subscription->id, $data, "paypal_recurring_created"); } break; } } protected function sendPaypalDebugEmail($subscription_id,$message,$type) { $this->logMessage($subscription_id, $message, $type); try { $subscription = Subscription::model()->findByPk((int)$subscription_id); if(isset($subscription)) { return; } //$this->logMessage($subscription_id, $message, $type); $email_body = '$HTTP_POST_VARS:'."\n\n"; $template = MailTemplate::model()->find("name=:name", array(":name"=> $type)); if (isset($template)) { $message_dump = ""; foreach ($message as $k => $v) { $message_dump .= $key.'='.$value."\n"; } $title = $template->title; $content = $template->content; $params = array( 'message_dump' => $message_dump, ); $content = Functions::replaceParams($template->content, $params); $user = $subscription->user; Yii::app()->mailer->AddAddress($user->email); Yii::app()->mailer->Subject = $title; Yii::app()->mailer->MsgHTML($content); Yii::app()->mailer->Send(); } } catch(Exception $e) { } } public function getExpressCheckoutDetails($token) { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $getExpressCheckoutDetailsReq = new GetExpressCheckoutDetailsReq(); $getExpressCheckoutDetailsRequestType = new GetExpressCheckoutDetailsRequestType(); $getExpressCheckoutDetailsRequestType->Token = $token; $getExpressCheckoutDetailsReq->GetExpressCheckoutDetailsRequest = $getExpressCheckoutDetailsRequestType; $response = $paypalService->GetExpressCheckoutDetails($getExpressCheckoutDetailsReq); if (($response->Ack != 'Success') && ($response->Ack != 'SuccessWithWarning')) { $this->sendDebugEmail(); } return $response; } public function createPaymentRecurrentProfile($params) { $profileDetails = new RecurringPaymentsProfileDetailsType(); $profileDetails->BillingStartDate = $params['StartDate'];//gmdate("Y-m-d\TH:i:s\Z"); if (isset($params['ProfileReference'])) { $profileDetails->ProfileReference = $params['ProfileReference']; } $paymentBillingPeriod = new BillingPeriodDetailsType(); $paymentBillingPeriod->BillingFrequency = $params['Frequency']; $paymentBillingPeriod->BillingPeriod = $params['Period']; $paymentBillingPeriod->Amount = new BasicAmountType($params['CurrencyID'], $params['Amount']); $scheduleDetails = new ScheduleDetailsType(); $scheduleDetails->Description = $params['Description']; $scheduleDetails->PaymentPeriod = $paymentBillingPeriod; $createRPProfileRequestDetails = new CreateRecurringPaymentsProfileRequestDetailsType(); $createRPProfileRequestDetails->Token = $params['Token']; $createRPProfileRequestDetails->ScheduleDetails = $scheduleDetails; $createRPProfileRequestDetails->RecurringPaymentsProfileDetails = $profileDetails; $createRPProfileRequest = new CreateRecurringPaymentsProfileRequestType(); $createRPProfileRequest->CreateRecurringPaymentsProfileRequestDetails = $createRPProfileRequestDetails; $createRPProfileReq = new CreateRecurringPaymentsProfileReq(); $createRPProfileReq->CreateRecurringPaymentsProfileRequest = $createRPProfileRequest; $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $createRPProfileResponse = $paypalService->CreateRecurringPaymentsProfile($createRPProfileReq); return $createRPProfileResponse; } public function doExpressCheckoutPayment($parameters) { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $DoExpressCheckoutPaymentReq = new DoExpressCheckoutPaymentReq(); $DoExpressCheckoutPaymentReq->DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType(); $DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType(); $DoExpressCheckoutPaymentRequestDetails->Token = $parameters['Token']; $DoExpressCheckoutPaymentRequestDetails->PayerID = $parameters['PayerID']; $DoExpressCheckoutPaymentRequestDetails->PaymentDetails = $parameters['PaymentDetails']; $DoExpressCheckoutPaymentReq->DoExpressCheckoutPaymentRequest->DoExpressCheckoutPaymentRequestDetails = $DoExpressCheckoutPaymentRequestDetails; $response = $paypalService->doExpressCheckoutPayment($DoExpressCheckoutPaymentReq); // parse_str($response, $response_array); if (($response->Ack != 'Success') && ($response->Ack != 'SuccessWithWarning')) { $this->sendDebugEmail(); } return $response; } public function ManageRecurringPaymentsProfileStatus($params) { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $ManageRecurringPaymentsProfileStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $ManageRecurringPaymentsProfileStatusReq->ManageRecurringPaymentsProfileStatusRequest = new ManageRecurringPaymentsProfileStatusRequestType(); $ManageRecurringPaymentsProfileStatusRequestDetails = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); $ManageRecurringPaymentsProfileStatusRequestDetails->ProfileID = $params['ProfileID']; $ManageRecurringPaymentsProfileStatusRequestDetails->Action = $params['Action']; $ManageRecurringPaymentsProfileStatusReq->ManageRecurringPaymentsProfileStatusRequest->ManageRecurringPaymentsProfileStatusRequestDetails = $ManageRecurringPaymentsProfileStatusRequestDetails; $response = $paypalService->ManageRecurringPaymentsProfileStatus($ManageRecurringPaymentsProfileStatusReq); if (($response->Ack != 'Success') && ($response->Ack != 'SuccessWithWarning')) { $this->sendDebugEmail(); } return $response; } public function RefundTransaction($params) { $config = array( 'mode' => $this->transaction_server , 'acct1.UserName' => $this->username, 'acct1.Password' => $this->password, 'acct1.Signature' => $this->signature, ); $paypalService = new PayPalAPIInterfaceServiceService($config); $refundTransactionReq = new RefundTransactionReq(); $RefundTransactionRequest = new RefundTransactionRequestType(); $RefundTransactionRequest->TransactionID = $params['TransactionID']; $RefundTransactionRequest->Amount = $params['Amount']; $refundTransactionReq->RefundTransactionRequest = $RefundTransactionRequest; $response = $paypalService->RefundTransaction($refundTransactionReq); if (($response->Ack != 'Success') && ($response->Ack != 'SuccessWithWarning')) { $this->sendDebugEmail(); } return $response; } }
[+]
..
[+]
paypal-merchant-sdk
[+]
paypal-sdk-core
[-] payment_base.php
[edit]
[-] paypal.inc.php
[edit]