[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [JoinTheGame/v2] b3bdfb8: Payment stuff. With Paypal (expresscheckout
From:       Emil Sedgh <emilsedgh () gmail ! com>
Date:       2011-01-16 22:16:18
Message-ID: 20110116221618.51ED4A6092 () git ! kde ! org
[Download RAW message or body]


	A	 protected/views/payment/create.php	 [License: Trivialfile.]


	A	 protected/views/payment/admin.php	 [License: Trivialfile.]


	A	 protected/views/payment/SumColumn.php	 [License: Trivialfile.]


	A	 protected/controllers/PaymentController.php	 [License: Trivialfile.]

commit b3bdfb8d4edbf22526b02297b13963ef633d8a65
Author: Emil Sedgh <emilsedgh@gmail.com>
Date:   Mon Jan 17 01:43:57 2011 +0330

    Payment stuff. With Paypal (expresscheckout API) support. and casual payment \
management stuff (CRUD).

diff --git a/protected/controllers/PaymentController.php \
b/protected/controllers/PaymentController.php new file mode 100644
index 0000000..d92d899
--- /dev/null
+++ b/protected/controllers/PaymentController.php
@@ -0,0 +1,59 @@
+<?php
+Class PaymentController Extends Controller {
+	public function actionResume() {
+		Payment::model('Paypal');
+
+		$paypal = New PaypalPayment;
+		$paypal->initFromToken($_GET['token']);
+		$paypal->member = Yii::app()->user->id;
+		$result = $paypal->saveByToken(True, $_GET['token'], $_GET['PayerID']);
+
+		if($result)
+			$this->redirect(Yii::app()->session->get($paypal->getSessionNameForToken($_GET['token'])));
 +	}
+
+	public function actionAdmin() {
+		$this->render('admin');
+	}
+
+	public function actionCreate() {
+		$model = New Payment;
+		$model->scenario = 'create';
+
+		if($_POST['Payment']) {
+			$model->attributes = $_POST['Payment'];
+			$model->date = CDateTimeParser::parse($_POST['Payment']['formattedDate']);
+
+			if($model->validate()) {
+				$name = $_POST['Payment']['method'].'Payment';
+				$model = New $name;
+				$model->scenario = 'create';
+				$model->attributes = $_POST['Payment'];
+				$model->date = CDateTimeParser::parse($_POST['Payment']['formattedDate']);
+
+				if($model->save())
+					$this->redirect(Controller::createUrl('/payment/admin'));
+			}
+		}
+
+		$this->render('create', array('model' => $model));
+	}
+
+	public function actionUpdate() {
+		$model = Payment::model()->findbyPK($_GET['id']);
+		$model->scenario = 'create';
+
+		if($_POST[get_class($model)]) {
+			$model->attributes = $_POST[get_class($model)];
+			$model->date = CDateTimeParser::parse($_POST[get_class($model)]['formattedDate']);
 +			$model->save();
+		}
+
+		$this->render('create', array('model' => $model));
+	}
+
+	public function actionDelete() {
+		$payment = Payment::model()->findByPK($_GET['id']);
+		$payment->delete();
+	}
+}
\ No newline at end of file
diff --git a/protected/models/Payment.php b/protected/models/Payment.php
index 36a58ae..81c515d 100644
--- a/protected/models/Payment.php
+++ b/protected/models/Payment.php
@@ -1,10 +1,11 @@
 <?php
-Abstract Class Payment Extends CActiveRecord {
+Class Payment Extends CActiveRecord {
 	const ANNUALY = 'annualy';
 	const QUARTERLY = 'quarterly';
 
-	const DONE = 'done';
-	const WAITING = 'waiting';
+	const Done = 'Done';
+	const Pending = 'Pending';
+	const Failed = 'Failed';
 
 	public static function model($method = __CLASS__) {
 		if($method == __CLASS__)
@@ -13,10 +14,23 @@ Abstract Class Payment Extends CActiveRecord {
 			return parent::model($method.'Payment');
 	}
 
+	public function init() {
+		$this->setAttribute('method', $this->getMethod());
+
+		$this->status = self::Done; //Default status is done.
+	}
+
 	public function tableName() {
 		return 'payment';
 	}
 
+	public function rules() {
+		return Array(
+			Array('member, amount, date', 'required', 'on' => 'create'),
+			Array('member, amount', 'safe', 'on' => 'create'),
+		);
+	}
+
 	public static function getMethods() {
 		return Array(New PaypalPayment, New DirectTransferPayment, New \
DirectDebitAuthorizationPayment);  }
@@ -42,9 +56,20 @@ Abstract Class Payment Extends CActiveRecord {
 
 	public function relations() {
 		return array(
-			'member' => array(self::BELONGS_TO, 'Member', 'id')
+			'owner' => array(self::BELONGS_TO, 'Member', 'member')
 		);
 	}
+
+	public function instantiate($attributes) {
+		$method = $attributes['method'];
+		$name = $method.'Payment';
+		return New $name(null);
+	}
+
+	public function getFormattedDate() {
+		if($this->date)
+			return Yii::app()->format->formatDate($this->date);
+	}
 }
 
 Class DirectDebitAuthorizationPayment Extends Payment {
@@ -84,11 +109,145 @@ Class DirectTransferPayment Extends Payment {
 }
 
 Class PaypalPayment Extends Payment {
+	//Methods
+	const SetExpressCheckout = 'SetExpressCheckout';
+	const GetExpressCheckoutDetails = 'GetExpressCheckoutDetails';
+	const DoExpressCheckoutPayment = 'DoExpressCheckoutPayment';
+
+	// Request parameters
+	const Method = 'METHOD';
+	const Version = 'VERSION';
+	const User = 'USER';
+	const Password = 'PWD';
+	const Signature = 'SIGNATURE';
+	const Payer = 'PAYERID';
+	const Action = 'PAYMENTACTION';
+
+	const Amount = 'AMT';
+	const ReturnUrl = 'RETURNURL';
+	const CancelUrl = 'CANCELURL';
+	const Currency = 'CURRENCYCODE';
+
+	const Euro = 'EUR';
+	const Usd = 'USD';
+
+	const Sale = 'Sale';
+
+	//Response parameters
+	const Token = 'TOKEN';
+	const Time = 'TIMESTAMP';
+	const Correlation = 'CORRELATIONID';
+	const Ack = 'ACK';
+	const Build = 'BUILD';
+	const Status = 'PAYMENTSTATUS';
+	const Transaction = 'TRANSACTIONID';
+
+
+	//Response values
+	const Success = 'Success';
+	const Pending = 'Pending';
+	const Completed = 'Completed';
+	const Failed = 'Failed';
+
+
 	public function getTitle() {
 		return Yii::t('payment', 'Paypal');
 	}
 
-	public function pay() {
+	public function pay($redirectUri) {
+		$response = $this->request(self::SetExpressCheckout, array(
+			self::Amount => $this->amount,
+			self::ReturnUrl => Yii::app()->createAbsoluteUrl('/payment/resume'),
+			self::CancelUrl => Yii::app()->createAbsoluteUrl('/payment/cancel'),
+			self::Currency => self::Euro
+		));
+
+		if($response[self::Ack] != self::Success)
+			return False;
+
+		$token = $response[self::Token];
+
+		Yii::app()->session->add($this->getSessionNameForToken($token), $redirectUri);
+
+		Yii::app()->request->redirect('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token='.$token);
 +	}
+
+	public function getSessionNameForToken($token) {
+		return 'redirect_uri_for_'.$token;
+	}
+
+	private function makeNvp($array) {
+		$str = '';
+		foreach($array as $name => $value)
+			$str .= $name.'='.urlencode($value).'&';
+
+		return $str;
+	}
+
+	private function request($method, Array $params) {
+		$uri = 'https://api-3t.sandbox.paypal.com/nvp?';
+
+		$params[self::Method] = $method;
+
+		$params[self::Version] = '56.0';
+		$params[self::User] = 'emilse_1295180949_biz_api1.gmail.com';
+		$params[self::Password] = '1295180958';
+		$params[self::Signature] = \
'AS4OkSBsj-xW-b0wG.HK19.sWXg9AEL2SWnfC0YfAMDlcb6X4S6ah7MT'; +
+		$querystring = $this->makeNvp($params);
+
+		$response = file_get_contents($uri.$querystring);
+		$response = explode('&', $response);
+		$array = Array();
+		foreach($response as $part) {
+			$part = explode('=', $part);
+			$array[$part[0]] = $part[1];
+		}
+
+		return $array;
+	}
+
+	public function initFromToken($token) {
+		$response = $this->request(self::GetExpressCheckoutDetails, array(
+			self::Token => $token
+		));
+
+		if($response[self::Ack] != self::Success)
+			return False;
+
+		$this->amount = (float)$response[self::Amount];
+		$this->date = time();
+	}
+
+	public function saveByToken($runValidations = True, $token, $payerId) {
+		$response = $this->request(self::DoExpressCheckoutPayment, Array(
+			self::Token => $token,
+			self::Payer => $payerId,
+			self::Action => self::Sale,
+			self::Amount => $this->amount,
+			self::Currency => self::Euro
+		));
+
+		if($response[self::Ack] != self::Success)
+			return False;
+
+		Switch ($response[self::Status]) {
+			case self::Completed:
+			case Null:
+				$this->status = self::Done;
+			break;
+
+			case self::Pending:
+				$this->status = self::Pending;
+			break;
+
+			case self::Failed:
+				$this->status = self::Failed;
+			break;
+		}
+
+		$this->transaction = $response[self::Transaction];
 
+		return parent::save($runValidations);
 	}
 }
\ No newline at end of file
diff --git a/protected/views/payment/SumColumn.php \
b/protected/views/payment/SumColumn.php new file mode 100644
index 0000000..e179dc4
--- /dev/null
+++ b/protected/views/payment/SumColumn.php
@@ -0,0 +1,21 @@
+<?php
+Yii::import('zii.widgets.grid.CDataColumn');
+
+Class SumColumn Extends CDataColumn {
+	public $name = 'amount';
+	public $sum = 0;
+
+	public function getHasFooter() {
+		return True;
+	}
+
+	public function renderDataCellContent($row, $data) {
+		$amount = $data->{$this->name};
+		echo $amount;
+		$this->sum += (float)$amount;
+	}
+
+	public function renderFooterCellContent() {
+		echo $this->sum;
+	}
+}
\ No newline at end of file
diff --git a/protected/views/payment/admin.php b/protected/views/payment/admin.php
new file mode 100644
index 0000000..182facf
--- /dev/null
+++ b/protected/views/payment/admin.php
@@ -0,0 +1,28 @@
+<a href="<?php echo Controller::createUrl('/payment/create');?>">
+	<?php echo Yii::t('payment', 'Add new payment');?>
+</a>
+
+<?php
+Yii::import('application.views.payment.SumColumn');
+
+$this->widget('zii.widgets.grid.CGridView', array(
+	'cssFile' => False,
+	'dataProvider' => New CActiveDataProvider('Payment', Array(
+		'pagination' => False,
+	)),
+	'rowCssClassExpression' => '$data->status',
+	'columns' => Array(
+		'id',
+		'owner.fullName',
+		'method',
+		'status',
+		array(
+			'class' => 'SumColumn'
+		),
+		array(
+			'class' => 'CButtonColumn',
+			'template' => '{update}{delete}'
+		)
+	)
+));
+?>
\ No newline at end of file
diff --git a/protected/views/payment/create.php b/protected/views/payment/create.php
new file mode 100644
index 0000000..675ef45
--- /dev/null
+++ b/protected/views/payment/create.php
@@ -0,0 +1,37 @@
+<?php $form = $this->beginWidget('CActiveForm'); ?>
+
+	<?php
+		$this->extra = CHtml::errorSummary($model);
+	?>
+
+	<div class="row">
+		<?php echo $form->labelEx($model, 'member'); ?>
+		<br />
+		<?php echo $form->dropDownList($model, 'member', \
CHtml::listData(Member::model()->findAll(), 'id', 'fullName')); ?> +	</div>
+
+	<div class="row">
+		<?php echo $form->labelEx($model, 'amount'); ?>
+		<br />
+		<?php echo $form->textField($model, 'amount'); ?>
+	</div>
+
+	<div class="row">
+		<?php echo $form->labelEx($model, 'date'); ?>
+		<br />
+		<?php $this->widget('zii.widgets.jui.CJuiDatePicker', Array(
+			'model' => $model,
+			'attribute' => 'formattedDate'
+		)); ?>
+	</div>
+
+	<div class="row">
+		<?php echo $form->labelEx($model, 'method'); ?>
+		<br />
+		<?php echo $form->dropDownList($model, 'method', \
CHtml::listData(Payment::getMethods(), 'method', 'method')); ?> +	</div>
+
+	<div class="row">
+		<?php echo CHtml::submitButton(); ?>
+	</div>
+<?php $this->endWidget(); ?>
\ No newline at end of file


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic