Skip to content

Commit

Permalink
Bank and receipt API is added to this module to pay invoices of a tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
hadi committed Nov 29, 2017
1 parent 83aa3b7 commit 35205e0
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 7 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"pluf/bank" : "^2.1.0"
},
"require-dev" : {
"pluf/test" : "^2.1.0",
"pluf/collection" : "^2.1.0"
"pluf/test" : "^2.1.0"
},
"include-path" : [
"src/"
Expand Down
20 changes: 20 additions & 0 deletions src/Tenant/BankBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Tenant_BankBackend extends Bank_Backend
{

function init()
{
parent::init();
$this->_a['multitenant'] = false;
$this->_a['cols'] = array_merge($this->_a['cols'], array(
'tenant' => array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'Pluf_Tenant',
'blank' => false,
'unique' => true,
'editable' => false
)
));
}
}
53 changes: 53 additions & 0 deletions src/Tenant/BankReceipt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of Pluf Framework, a simple PHP Application Framework.
* Copyright (C) 2010-2020 Phoinex Scholars Co. (http://dpq.co.ir)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* ساختارهای داده‌ای برای رسید را ایجاد می‌کند.
*
* رسید عبارت است از یک مجموعه از داده‌ها که برای پرداخت به بانک ارسال
* می‌شود. این رسید زمانی که بانک تایید کند به روز شده و اطلاعات دریافتی
* از بانک نیز به آن اضافه می شود.
*
* رسید در اینجا مثل رسید در ماژول بانک است با این تفاوت که این رسید تنها برای پرداخت‌های مربوط به tenant است.
*
* @author hadi
*
*/
class Tenant_BankReceipt extends Bank_Receipt
{

/**
* @see Bank_Receipt::init()
*/
function init()
{
parent::init();
// Change class type of backend foreingkey from Bank_Backend to Tenant_BankBackend
$this->_a['cols'] = array_merge($this->_a['cols'], array(
'backend' => array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'Tenant_BankBackend',
'blank' => false,
'relate_name' => 'backend'
)
));
}

}
67 changes: 67 additions & 0 deletions src/Tenant/BankService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

class Tenant_BankService
{

/**
* یک پرداخت جدید ایجاد می‌کند
*
* روالی که برای ایجاد یک پرداخت دنبال می‌شه می‌تونه خیلی متفاوت باشه
* و ساختارهای رو برای خودش ایجاد کنه. برای همین ما پارامترهای ارسالی در
* در خواست رو هم ارسال می‌کنیم.
*
* پرداخت ایجاد شده بر اساس اطلاعاتی است که با متغیر $reciptParam ارسال
* می‌شود. این پارامترها
* باید به صورت یک آرایه بوده و شامل موارد زیر باشد:
*
* <pre><code>
* $param = array(
* 'amount' => 1000, // مقدار پرداخت به ریال
* 'title' => 'payment title',
* 'description' => 'description',
* 'email' => '[email protected]',
* 'phone' => '0917222222',
* 'callbackURL' => 'http://.....',
* 'backend' => 2
* );
* </code></pre>
*
* <ul>
* <li>*amount: مقدار بر اساس ریال</li>
* <li>*title: عنوان پرداخت</li>
* <li>*description: توضیحات</li>
* <li>email: رایانامه مشتری</li>
* <li>phone: شماره تماس مشتری</li>
* <li>callbackURL: آدرسی که بعد از تکمیل باید فراخوانی شود</li>
* <li>*backend: درگاه پرداخت مورد نظر</li>
* </ul>
*
* در نهایت باید موجودیتی تعیین بشه که این پرداخت رو می‌خواهیم براش ایجاد
* کنیم.
*
* نکته مهم اینکه در این پیاده‌سازی backend باید مربوط به ملک اصلی باشه.
*
* @param array $param
* @param Pluf_Model $owner
* @return Bank_Receipt
*/
public static function create ($param, $owner = null, $ownerId = null)
{
$form = new Tenant_Form_BankReceiptNew($param);
$receipt = $form->save(false);
// $backend = Pluf::factory('Tenant_BankBackend', $receipt->backend);
$backend = $receipt->get_backend();
$engine = $backend->get_engine();
$engine->create($receipt);
if ($owner instanceof Pluf_Model) { // Pluf module
$receipt->owner_class = $owner->getClass();
$receipt->owner_id = $owner->getId();
} elseif (! is_null($owner)) { // module
$receipt->owner_class = $owner;
$receipt->owner_id = $ownerId;
}
$receipt->create();
return $receipt;
}

}
94 changes: 94 additions & 0 deletions src/Tenant/Form/BankReceiptNew.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of Pluf Framework, a simple PHP Application Framework.
* Copyright (C) 2010-2020 Phoinex Scholars Co. (http://dpq.co.ir)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* This form is same as Bank_Form_RecieptNew form except that in this form given backend
* sould blong to main tenant
*
* @author hadi <[email protected]>
*
*/
class Tenant_Form_BankReceiptNew extends Bank_Form_ReceiptNew
{

function clean_backend()
{
$backend = Pluf::factory('Tenant_BankBackend', $this->cleaned_data['backend']);
if ($backend->isAnonymous()) {
throw new Pluf_Exception('backend not found');
}
// Check if backend blong to main tenant
$mainTenant = Tenant_Shortcuts_GetMainTenant();
if ($backend->tenant !== $mainTenant->getId()) {
throw new Pluf_Exception('backend is not valid for this payment');
}
// XXX: maso, 1395: گرفتن پشتوانه
return $backend->id;
}

/**
*
* @param string $commit
* @throws Pluf_Exception
* @return Tenant_BankBackend
*/
function save ($commit = true)
{
if (! $this->isValid()) {
// TODO: maso, 1395: باید از خطای مدل فرم استفاده شود.
throw new Pluf_Exception(
'Cannot save a receipt from an invalid form.');
}
// Set attributes
$receipt = new Tenant_BankReceipt();
$receipt->setFromFormData($this->cleaned_data);
$receipt->secure_id = $this->getSecureKey();
// موجودیت قرار گیرد.
if ($commit) {
if (! $receipt->create()) {
throw new Pluf_Exception('fail to create the recipt.');
}
}
return $receipt;
}

/**
* یک کد جدید برای موجودیت ایجاد می‌کند.
*
* @return Tenant_BankReceipt
*/
private function getSecureKey ()
{
$recipt = new Tenant_BankReceipt();
while (1) {
$key = sha1(
microtime() . rand(0, 123456789) . Pluf::f('secret_key'));
$sess = $recipt->getList(
array(
'filter' => 'secure_id=\'' . $key . '\''
));
if (count($sess) == 0) {
break;
}
}
return $key;
}
}

8 changes: 4 additions & 4 deletions src/Tenant/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function init()
// relations
'payment' => array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'Bank_Receipt',
'model' => 'Tenant_BankReceipt',
'blank' => false,
'editable' => false,
'readable' => true,
Expand Down Expand Up @@ -136,9 +136,9 @@ function setStatus($status)
}
// It is first time to update status of invoice
// Note: Hadi - 1396-04: time is base on day
$day = Setting_Service::get(Tenant_Constants::SETTING_KEY_INVOICE_VALID_DAY, '30');
$expiryDay = ' +' . $day . ' day';
$this->expiry = date('Y-m-d H:i:s', strtotime($expiryDay));
// $day = Setting_Service::get(Tenant_Constants::SETTING_KEY_INVOICE_VALID_DAY, '30');
// $expiryDay = ' +' . $day . ' day';
// $this->expiry = date('Y-m-d H:i:s', strtotime($expiryDay));
$this->status = $status;
$this->update();
}
Expand Down
23 changes: 23 additions & 0 deletions src/Tenant/Shortcuts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

function Tenant_Shortcuts_GetMainTenant ()
{
$subdomain = Pluf::f('tenant_default', null);
if($subdomain === null){
throw new Pluf_Exception_DoesNotExist('tenant_default is not set!');
}
$tenant = Pluf_Tenant::bySubDomain($subdomain);
if ($tenant == null || $tenant->id <= 0) {
throw new Pluf_Exception_DoesNotExist(
"Tenant not found (subdomain:" . $subdomain . ")");
}
return $tenant;
}

function Tenant_Shortcuts_NormalizeItemPerPage ($request)
{
$count = array_key_exists('_px_c', $request->REQUEST) ? intval($request->REQUEST['_px_c']) : 30;
if($count > 30)
$count = 30;
return $count;
}
47 changes: 47 additions & 0 deletions src/Tenant/Views/BankBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Pluf Framework, a simple PHP Application Framework.
* Copyright (C) 2010-2020 Phoinex Scholars Co. (http://dpq.co.ir)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
*
* @author hadi <[email protected]>
*
*/
class Tenant_Views_BankBackend
{

/**
* Returns bank-backend determined by given id.
* This method returns backend if and only if backend with given id blongs to main tenant
* else it throws not found exception
*
* @param Pluf_HTTP_Request $request
* @param array $match
* @param array $p
*/
public function get($request, $match, $p)
{
$backend = Pluf_Views::getObject($request, $match, $p);
if ($backend->tenant !== Tenant_Shortcuts_GetMainTenant()->id) {
throw new Pluf_HTTP_Error404("Object not found (" . $p['model'] . "," . $backend->id . ")");
;
}
return $backend;
}
}
9 changes: 8 additions & 1 deletion src/Tenant/Views/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public static function payment($request, $match)
$backend = $request->REQUEST['backend'];
$price = $invoice->amount;

// Check backend
$be = Pluf::factory('Tenant_BankBackend')->getOne('id=' . $backend);
$mainTenant = Tenant_Shortcuts_GetMainTenant();
if($be->id !== $mainTenant->id){
throw new Pluf_Exception('Invalid backend. Backend should be blong to main tenant.');
}

// check for discount
if (isset($request->REQUEST['discount_code'])) {
$discountCode = $request->REQUEST['discount_code'];
Expand All @@ -60,7 +67,7 @@ public static function payment($request, $match)
'backend' => $backend
);

$payment = Bank_Service::create($receiptData, 'tenant-invoice', $invoice->id);
$payment = Tenant_BankService::create($receiptData, 'tenant-invoice', $invoice->id);

$invoice->payment = $payment;
$invoice->update();
Expand Down
Loading

0 comments on commit 35205e0

Please sign in to comment.