From aea197779e656de5fa8871eb9e72a62cb5993ad0 Mon Sep 17 00:00:00 2001 From: maso Date: Thu, 9 Nov 2017 17:31:21 +0330 Subject: [PATCH 1/9] Coverage test --- .coveralls.yml | 3 +++ .travis.yml | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..ac3f05c --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,3 @@ +service_name: travis-ci +coverage_clover: tests/clover.xml +json_path: tests/coveralls-upload.json \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a815eb5..bd61490 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,9 @@ language: php php: - '5.6' - + - '7.1' +git: + depth: 1 mysql: database: test username: root @@ -9,16 +11,22 @@ mysql: before_install: - sudo apt-get update > /dev/null - - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" - - php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" - - php composer-setup.php - - php -r "unlink('composer-setup.php');" install: - - php composer.phar install + # Install composer packages, will also trigger dump-autoload + - travis_retry composer install --no-interaction + # Install coveralls.phar + - wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar + - chmod +x coveralls.phar + - php coveralls.phar --version script: - cd tests - php run.php - - php coverage-checker.php clover.xml 70 + - cd .. +after_success: +# Submit coverage report to Coveralls servers, see .coveralls.yml + - travis_retry php coveralls.phar -v +# Submit coverage report to codecov.io + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file From 1b129e685ef159f4728b417fd04d70c0bd1e7fee Mon Sep 17 00:00:00 2001 From: maso Date: Thu, 9 Nov 2017 17:42:32 +0330 Subject: [PATCH 2/9] Update documents --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 67d7d59..dd62559 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # tenant [![Build Status](https://travis-ci.org/pluf/tenant.svg?branch=master)](https://travis-ci.org/pluf/tenant) +[![codecov](https://codecov.io/gh/pluf/tenant/branch/master/graph/badge.svg)](https://codecov.io/gh/pluf/tenant) +[![Coverage Status](https://coveralls.io/repos/github/pluf/tenant/badge.svg?branch=develop)](https://coveralls.io/github/pluf/tenant?branch=develop) + +# Develop + +[![Build Status](https://travis-ci.org/pluf/tenant.svg?branch=develop)](https://travis-ci.org/pluf/tenant) It is a Pluf module to manage current tenant. From ee9f191bbe83a90220cc12da65795dd32d612b78 Mon Sep 17 00:00:00 2001 From: hadi Date: Sat, 11 Nov 2017 01:49:06 +0330 Subject: [PATCH 3/9] correct a typo --- src/Tenant/Invoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tenant/Invoice.php b/src/Tenant/Invoice.php index ce63940..37daacc 100755 --- a/src/Tenant/Invoice.php +++ b/src/Tenant/Invoice.php @@ -36,7 +36,7 @@ function init() 'blank' => false, 'is_null' => false ), - 'due_dtiem' => array( + 'due_dtime' => array( 'type' => 'Pluf_DB_Field_Date', 'blank' => false, 'is_null' => false From 51fcb87e489f614f90c40d6eadb34194eb3317c2 Mon Sep 17 00:00:00 2001 From: maso Date: Sun, 12 Nov 2017 02:10:44 +0330 Subject: [PATCH 4/9] update readme and add coverage links --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dd62559..15e5b73 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,12 @@ [![Build Status](https://travis-ci.org/pluf/tenant.svg?branch=master)](https://travis-ci.org/pluf/tenant) [![codecov](https://codecov.io/gh/pluf/tenant/branch/master/graph/badge.svg)](https://codecov.io/gh/pluf/tenant) -[![Coverage Status](https://coveralls.io/repos/github/pluf/tenant/badge.svg?branch=develop)](https://coveralls.io/github/pluf/tenant?branch=develop) +[![Coverage Status](https://coveralls.io/repos/github/pluf/tenant/badge.svg?branch=master)](https://coveralls.io/github/pluf/tenant?branch=master) # Develop + [![Build Status](https://travis-ci.org/pluf/tenant.svg?branch=develop)](https://travis-ci.org/pluf/tenant) +[![Coverage Status](https://coveralls.io/repos/github/pluf/supertenant/badge.svg?branch=develop)](https://coveralls.io/github/pluf/supertenant?branch=develop) It is a Pluf module to manage current tenant. From 4cf1b861916e78ac4a6967f5d9173950508ac80f Mon Sep 17 00:00:00 2001 From: maso Date: Sun, 12 Nov 2017 02:18:16 +0330 Subject: [PATCH 5/9] Typo: fix link of coverage test --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 15e5b73..d6ab166 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,5 @@ [![Build Status](https://travis-ci.org/pluf/tenant.svg?branch=develop)](https://travis-ci.org/pluf/tenant) -[![Coverage Status](https://coveralls.io/repos/github/pluf/supertenant/badge.svg?branch=develop)](https://coveralls.io/github/pluf/supertenant?branch=develop) It is a Pluf module to manage current tenant. From e607ad7e6afdeea4490556068765365b8ccf7d29 Mon Sep 17 00:00:00 2001 From: maso Date: Tue, 14 Nov 2017 00:26:15 +0330 Subject: [PATCH 6/9] Update projcet build script --- .travis.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bd61490..5890d7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,13 @@ language: php php: - '5.6' - '7.1' + +services: + - mysql + git: depth: 1 + mysql: database: test username: root @@ -11,7 +16,8 @@ mysql: before_install: - sudo apt-get update > /dev/null - + - mysql -e 'CREATE DATABASE IF NOT EXISTS test;' + install: # Install composer packages, will also trigger dump-autoload - travis_retry composer install --no-interaction From 720df3d73c492792a00e8bc55fb837718dab650e Mon Sep 17 00:00:00 2001 From: maso Date: Wed, 15 Nov 2017 01:18:08 +0330 Subject: [PATCH 7/9] chage new comment view to add user into the comment. --- src/Tenant/Views/Ticket.php | 48 +++++++++++++++++++++++++++++++++++++ src/Tenant/urls.php | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Tenant/Views/Ticket.php diff --git a/src/Tenant/Views/Ticket.php b/src/Tenant/Views/Ticket.php new file mode 100644 index 0000000..0a24473 --- /dev/null +++ b/src/Tenant/Views/Ticket.php @@ -0,0 +1,48 @@ +. + */ +Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel'); + +/** + * Invoices view + * + * @author maso + */ +class Tenant_Views_Ticket +{ + + /** + * Create new comment + * + * @param Pluf_HTTP_Request $request + * @param array $match + * @param array $p + */ + public function createManyToOne($request, $match) + { + $parent = Pluf_Shortcuts_GetObjectOr404('Tenant_Ticket', $match['parentId']); + $object = new Tenant_Comment(); + $form = Pluf_Shortcuts_GetFormForModel($object, $request->REQUEST); + $object = $form->save(false); + $object->ticket = $parent; + $object->author = $request->user; + $object->create(); + return $object; + } +} \ No newline at end of file diff --git a/src/Tenant/urls.php b/src/Tenant/urls.php index 19edc8f..18c06de 100644 --- a/src/Tenant/urls.php +++ b/src/Tenant/urls.php @@ -169,7 +169,7 @@ ), array( 'regex' => '#^/ticket/(?P\d+)/comment/new$#', - 'model' => 'Pluf_Views', + 'model' => 'Tenant_Views_Ticket', 'method' => 'createManyToOne', 'http-method' => 'POST', 'precond' => array( From 82f5cfb218784ecefba148ac5c316aa3de5f3998 Mon Sep 17 00:00:00 2001 From: hadi Date: Wed, 15 Nov 2017 10:11:22 +0330 Subject: [PATCH 8/9] test APIs. complete API for pay an invoice. --- composer.json | 4 +- src/Tenant/Invoice.php | 17 +++--- src/Tenant/Views.php | 2 +- src/Tenant/Views/Invoice.php | 101 ++++++++++++++++++++++++++++++++++ src/Tenant/Views/Invoices.php | 29 ---------- src/Tenant/urls.php | 66 ++++++++++++---------- 6 files changed, 151 insertions(+), 68 deletions(-) create mode 100644 src/Tenant/Views/Invoice.php delete mode 100644 src/Tenant/Views/Invoices.php diff --git a/composer.json b/composer.json index 9ee3c91..da3b6d3 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "minimum-stability" : "stable", "require" : { "pluf/core" : "^2.1.0", - "pluf/user" : "^2.1.0" + "pluf/user" : "^2.1.0", + "pluf/discount" : "^2.1.0", + "pluf/bank" : "^2.1.0" }, "require-dev" : { "pluf/test" : "^2.1.0", diff --git a/src/Tenant/Invoice.php b/src/Tenant/Invoice.php index 37daacc..9fb09d4 100755 --- a/src/Tenant/Invoice.php +++ b/src/Tenant/Invoice.php @@ -131,15 +131,14 @@ function getStatus() function setStatus($status) { - // if ($this->status === 'payed') { - // return; - // } - // // It is first time to activate invoice - // // Note: Hadi - 1396-04: time is base on day - // $day = Setting_Service::get(Tenant_Constants::SETTING_KEY_LINK_VALID_DAY, '30'); - // $expiryDay = ' +' . $day . ' day'; - // $this->expiry = date('Y-m-d H:i:s', strtotime($expiryDay)); - // $this->active = true; + if ($this->status === 'payed') { + return; + } + // 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)); $this->status = $status; $this->update(); } diff --git a/src/Tenant/Views.php b/src/Tenant/Views.php index 27bb3e7..72d2019 100644 --- a/src/Tenant/Views.php +++ b/src/Tenant/Views.php @@ -46,7 +46,7 @@ public static function current ($request, $match) public static function update ($request, $match) { $model = $request->tenant; - $form = Pluf_Shortcuts_GetFormForModel($model, $request->REQUEST, + $form = Pluf_Shortcuts_GetFormForUpdateModel($model, $request->REQUEST, array()); return new Pluf_HTTP_Response_Json($form->save()); } diff --git a/src/Tenant/Views/Invoice.php b/src/Tenant/Views/Invoice.php new file mode 100644 index 0000000..e346598 --- /dev/null +++ b/src/Tenant/Views/Invoice.php @@ -0,0 +1,101 @@ +. + */ + +/** + * Invoices view + * + * @author maso + * @author hadi + */ +class Tenant_Views_Invoice +{ + + /** + * + * @param Pluf_HTTP_Request $request + * @param array $match + */ + public static function payment($request, $match) + { + $invoice = Pluf_Shortcuts_GetObjectOr404('Tenant_Invoice', $match['modelId']); + + $user = $request->user; + $url = $request->REQUEST['callback']; + $backend = $request->REQUEST['backend']; + $price = $invoice->amount; + + // check for discount + if (isset($request->REQUEST['discount_code'])) { + $discountCode = $request->REQUEST['discount_code']; + $price = Discount_Service::getPrice($price, $discountCode, $request); + $discount = Discount_Service::consumeDiscount($discountCode); + $invoice->discount_code = $discountCode; + } + + $receiptData = array( + 'amount' => $price, // مقدار پرداخت به تومان + 'title' => $invoice->title, + 'description' => $invoice->id . ' - ' . $invoice->title, + 'email' => $user->email, + // 'phone' => $user->phone, + 'phone' => '', + 'callbackURL' => $url, + 'backend' => $backend + ); + + $payment = Bank_Service::create($receiptData, 'tenant-invoice', $invoice->id); + + $invoice->payment = $payment; + $invoice->update(); + return new Pluf_HTTP_Response_Json($payment); + } + + /** + * + * @param Pluf_HTTP_Request $request + * @param array $match + */ + public static function checkPaymentState($request, $match) + { + $invoice = Pluf_Shortcuts_GetObjectOr404('Tenant_Invoice', $match['modelId']); + $invoice = Tenant_Views_Invoice::updatePaymentStatus($invoice); + return new Pluf_HTTP_Response_Json($invoice); + } + + /** + * Checks + * + * @param Tenant_Invoice $invoice + * @return Tenant_Invoice + */ + private static function updatePaymentStatus($invoice) + { + if (! $invoice->payment || $invoice->getStatus() === 'payed') { + return $invoice; + } + $receipt = $invoice->get_payment(); + Bank_Service::update($receipt); + if ($invoice->get_payment()->isPayed()){ + $invoice->setStatus('payed'); + $invoice->update(); + } + return $invoice; + } +} \ No newline at end of file diff --git a/src/Tenant/Views/Invoices.php b/src/Tenant/Views/Invoices.php deleted file mode 100644 index 607ae79..0000000 --- a/src/Tenant/Views/Invoices.php +++ /dev/null @@ -1,29 +0,0 @@ -. - */ - -/** - * Invoices view - * - * @author maso - */ -class Tenant_Views_Invoices -{ - // TODO: maso, 2017: adding payment -} \ No newline at end of file diff --git a/src/Tenant/urls.php b/src/Tenant/urls.php index 19edc8f..d9e8b8f 100644 --- a/src/Tenant/urls.php +++ b/src/Tenant/urls.php @@ -17,17 +17,15 @@ * along with this program. If not, see . */ return array( - /* - * Tenant - */ - array( + // **************************************************************** Current Tenant + array( // Get 'regex' => '#^/tenant/current$#', 'model' => 'Tenant_Views', 'method' => 'current', 'http-method' => 'GET', 'precond' => array() ), - array( + array( // Update 'regex' => '#^/tenant/current$#', 'model' => 'Tenant_Views', 'method' => 'update', @@ -36,7 +34,7 @@ 'Pluf_Precondition::ownerRequired' ) ), - array( + array( // Delete 'regex' => '#^/tenant/current$#', 'model' => 'Tenant_Views', 'method' => 'delete', @@ -45,10 +43,8 @@ 'Pluf_Precondition::ownerRequired' ) ), - /* - * Ticket - */ - array( + // **************************************************************** Ticket + array( // Find 'regex' => '#^/ticket/find$#', 'model' => 'Pluf_Views', 'method' => 'findObject', @@ -81,7 +77,7 @@ ) ) ), - array( + array( // Create 'regex' => '#^/ticket/new$#', 'model' => 'Pluf_Views', 'method' => 'createObject', @@ -93,7 +89,7 @@ 'model' => 'Tenant_Ticket' ) ), - array( + array( // Get info 'regex' => '#^/ticket/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'getObject', @@ -105,7 +101,7 @@ 'model' => 'Tenant_Ticket' ) ), - array( + array( // Update 'regex' => '#^/ticket/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'updateObject', @@ -117,7 +113,7 @@ 'model' => 'Tenant_Ticket' ) ), - array( + array( // Delete 'regex' => '#^/ticket/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'deleteObject', @@ -129,10 +125,8 @@ 'model' => 'Tenant_Ticket' ) ), - /* - * Comments of ticket - */ - array( + // **************************************************************** Comments of ticket + array( // Find 'regex' => '#^/ticket/(?P\d+)/comment/find$#', 'model' => 'Pluf_Views', 'method' => 'findManyToOne', @@ -167,7 +161,7 @@ ) ) ), - array( + array( // Create 'regex' => '#^/ticket/(?P\d+)/comment/new$#', 'model' => 'Pluf_Views', 'method' => 'createManyToOne', @@ -181,7 +175,7 @@ 'parentKey' => 'ticket' ) ), - array( + array( // Get 'regex' => '#^/ticket/(?P\d+)/comment/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'getManyToOne', @@ -195,7 +189,7 @@ 'parentKey' => 'ticket' ) ), - array( + array( // Update 'regex' => '#^/ticket/(?P\d+)/comment/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'updateManyToOne', @@ -209,7 +203,7 @@ 'parentKey' => 'ticket' ) ), - array( + array( // Delete 'regex' => '#^/ticket/(?P\d+)/comment/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'deleteManyToOne', @@ -224,10 +218,8 @@ ) ), - /* - * invoices - */ - array( + // **************************************************************** Invoices + array( // Find 'regex' => '#^/invoice/find$#', 'model' => 'Pluf_Views', 'method' => 'findObject', @@ -260,7 +252,7 @@ ) ) ), - array( + array( // Get 'regex' => '#^/invoice/(?P\d+)$#', 'model' => 'Pluf_Views', 'method' => 'getObject', @@ -271,5 +263,23 @@ 'params' => array( 'model' => 'Tenant_Invoice' ) - ) + ), + array( // pay invoice for tenant + 'regex' => '#^/invoice/(?P\d+)/pay$#', + 'model' => 'Tenant_Views_Invoice', + 'method' => 'payment', + 'http-method' => 'POST', + 'precond' => array( + 'Pluf_Precondition::ownerRequired' + ) + ), + array( // check payment state + 'regex' => '#^/invoice/(?P\d+)/state$#', + 'model' => 'Tenant_Views_Invoice', + 'method' => 'checkPaymentState', + 'http-method' => 'GET', + 'precond' => array( + 'Pluf_Precondition::ownerRequired' + ) + ), ); From 35205e0b7ee75edf23a507e24258c522c3170a60 Mon Sep 17 00:00:00 2001 From: hadi Date: Thu, 30 Nov 2017 01:29:07 +0330 Subject: [PATCH 9/9] Bank and receipt API is added to this module to pay invoices of a tenant --- composer.json | 3 +- src/Tenant/BankBackend.php | 20 ++++++ src/Tenant/BankReceipt.php | 53 ++++++++++++++ src/Tenant/BankService.php | 67 ++++++++++++++++++ src/Tenant/Form/BankReceiptNew.php | 94 +++++++++++++++++++++++++ src/Tenant/Invoice.php | 8 +-- src/Tenant/Shortcuts.php | 23 ++++++ src/Tenant/Views/BankBackend.php | 47 +++++++++++++ src/Tenant/Views/Invoice.php | 9 ++- src/Tenant/Views/Receipt.php | 44 ++++++++++++ src/Tenant/urls.php | 109 +++++++++++++++++++++++++++++ 11 files changed, 470 insertions(+), 7 deletions(-) create mode 100755 src/Tenant/BankBackend.php create mode 100644 src/Tenant/BankReceipt.php create mode 100755 src/Tenant/BankService.php create mode 100644 src/Tenant/Form/BankReceiptNew.php create mode 100755 src/Tenant/Shortcuts.php create mode 100644 src/Tenant/Views/BankBackend.php create mode 100644 src/Tenant/Views/Receipt.php diff --git a/composer.json b/composer.json index da3b6d3..0ad9678 100644 --- a/composer.json +++ b/composer.json @@ -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/" diff --git a/src/Tenant/BankBackend.php b/src/Tenant/BankBackend.php new file mode 100755 index 0000000..f30442c --- /dev/null +++ b/src/Tenant/BankBackend.php @@ -0,0 +1,20 @@ +_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 + ) + )); + } +} \ No newline at end of file diff --git a/src/Tenant/BankReceipt.php b/src/Tenant/BankReceipt.php new file mode 100644 index 0000000..1090f97 --- /dev/null +++ b/src/Tenant/BankReceipt.php @@ -0,0 +1,53 @@ +. + */ + +/** + * ساختارهای داده‌ای برای رسید را ایجاد می‌کند. + * + * رسید عبارت است از یک مجموعه از داده‌ها که برای پرداخت به بانک ارسال + * می‌شود. این رسید زمانی که بانک تایید کند به روز شده و اطلاعات دریافتی + * از بانک نیز به آن اضافه می شود. + * + * رسید در اینجا مثل رسید در ماژول بانک است با این تفاوت که این رسید تنها برای پرداخت‌های مربوط به 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' + ) + )); + } + +} \ No newline at end of file diff --git a/src/Tenant/BankService.php b/src/Tenant/BankService.php new file mode 100755 index 0000000..4b34859 --- /dev/null +++ b/src/Tenant/BankService.php @@ -0,0 +1,67 @@ + + * $param = array( + * 'amount' => 1000, // مقدار پرداخت به ریال + * 'title' => 'payment title', + * 'description' => 'description', + * 'email' => 'user@email.address', + * 'phone' => '0917222222', + * 'callbackURL' => 'http://.....', + * 'backend' => 2 + * ); + * + * + *
    + *
  • *amount: مقدار بر اساس ریال
  • + *
  • *title: عنوان پرداخت
  • + *
  • *description: توضیحات
  • + *
  • email: رایانامه مشتری
  • + *
  • phone: شماره تماس مشتری
  • + *
  • callbackURL: آدرسی که بعد از تکمیل باید فراخوانی شود
  • + *
  • *backend: درگاه پرداخت مورد نظر
  • + *
+ * + * در نهایت باید موجودیتی تعیین بشه که این پرداخت رو می‌خواهیم براش ایجاد + * کنیم. + * + * نکته مهم اینکه در این پیاده‌سازی 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; + } + +} \ No newline at end of file diff --git a/src/Tenant/Form/BankReceiptNew.php b/src/Tenant/Form/BankReceiptNew.php new file mode 100644 index 0000000..2e3ed22 --- /dev/null +++ b/src/Tenant/Form/BankReceiptNew.php @@ -0,0 +1,94 @@ +. + */ + +/** + * This form is same as Bank_Form_RecieptNew form except that in this form given backend + * sould blong to main tenant + * + * @author hadi + * + */ +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; + } +} + diff --git a/src/Tenant/Invoice.php b/src/Tenant/Invoice.php index 9fb09d4..9f0e282 100755 --- a/src/Tenant/Invoice.php +++ b/src/Tenant/Invoice.php @@ -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, @@ -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(); } diff --git a/src/Tenant/Shortcuts.php b/src/Tenant/Shortcuts.php new file mode 100755 index 0000000..5b6a902 --- /dev/null +++ b/src/Tenant/Shortcuts.php @@ -0,0 +1,23 @@ +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; +} \ No newline at end of file diff --git a/src/Tenant/Views/BankBackend.php b/src/Tenant/Views/BankBackend.php new file mode 100644 index 0000000..cb3b698 --- /dev/null +++ b/src/Tenant/Views/BankBackend.php @@ -0,0 +1,47 @@ +. + */ + +/** + * + * @author hadi + * + */ +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; + } +} diff --git a/src/Tenant/Views/Invoice.php b/src/Tenant/Views/Invoice.php index e346598..ca1c70a 100644 --- a/src/Tenant/Views/Invoice.php +++ b/src/Tenant/Views/Invoice.php @@ -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']; @@ -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(); diff --git a/src/Tenant/Views/Receipt.php b/src/Tenant/Views/Receipt.php new file mode 100644 index 0000000..6b33215 --- /dev/null +++ b/src/Tenant/Views/Receipt.php @@ -0,0 +1,44 @@ +. + */ + +/** + * + * @author hadi + * + */ +class Tenant_Views_Receipt +{ + + /** + * + * @param Pluf_HTTP_Request $request + * @param array $match + */ + public function getBySecureId($request, $match) + { + $receipt = new Bank_Receipt(); + $sql = new Pluf_SQL('secure_id=%s', array( + $match['secure_id'] + )); + $receipt = $receipt->getOne($sql->gen()); + return new Pluf_HTTP_Response_Json($receipt); + } + +} diff --git a/src/Tenant/urls.php b/src/Tenant/urls.php index c9cde3a..ed6122c 100644 --- a/src/Tenant/urls.php +++ b/src/Tenant/urls.php @@ -16,6 +16,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +Pluf::loadFunction('Tenant_Shortcuts_GetMainTenant'); + return array( // **************************************************************** Current Tenant array( // Get @@ -282,4 +284,111 @@ 'Pluf_Precondition::ownerRequired' ) ), + // **************************************************************** Bank Backend + array( // Find + 'regex' => '#^/backend/find$#', + 'model' => 'Pluf_Views', + 'method' => 'findObject', + 'http-method' => 'GET', + 'precond' => array( + 'Pluf_Precondition::loginRequired' + ), + 'params' => array( + 'model' => 'Tenant_BankBackend', + 'model_view' => 'global', + 'sql' => new Pluf_SQL('tenant = ' . Tenant_Shortcuts_GetMainTenant()->id), + 'listFilters' => array( + 'id', + 'title', + 'home', + 'engine' + ), + 'listDisplay' => array(), + 'searchFields' => array( + 'title', + 'description' + ), + 'sortFields' => array( + 'id', + 'title', + 'creation_dtime' + ), + 'sortOrder' => array( + 'creation_dtime', + 'DESC' + ) + ) + ), + array( // Get + 'regex' => '#^/backend/(?P\d+)$#', + 'model' => 'Tenant_Views_BankBackend', + 'method' => 'get', + 'http-method' => 'GET', + 'precond' => array(), + 'params' => array( + 'model' => 'Tenant_BankBackend' + ) + ), + // **************************************************************** Receipt + array( // Find + 'regex' => '#^/receipt/find$#', + 'model' => 'Pluf_Views', + 'method' => 'findObject', + 'http-method' => 'GET', + 'precond' => array( + 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Bank_Receipt', + 'sql' => new Pluf_SQL('owner_class="tenant-invoice"'), + 'listFilters' => array( + 'id', + 'title', + 'secure_id', + 'backend' + ), + 'listDisplay' => array(), + 'searchFields' => array( + 'title', + 'description' + ), + 'sortFields' => array( + 'id', + 'title', + 'creation_dtime' + ), + 'sortOrder' => array( + 'creation_dtime', + 'DESC' + ) + ) + ), + // array( // Create + // 'regex' => '#^/receipt/new$#', + // 'model' => 'Bank_Views_Receipt', + // 'method' => 'create', + // 'http-method' => array( + // 'POST' + // ) + // ), + array( // Get + 'regex' => '#^/receipt/(?P\d+)$#', + 'model' => 'Pluf_Views', + 'method' => 'getObject', + 'http-method' => 'GET', + 'precond' => array( + 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Bank_Receipt' + ) + ), + array( // Get (by secure id) + 'regex' => '#^/receipt/(?P.+)$#', + 'model' => 'Tenant_Views_Receipt', + 'method' => 'getBySecureId', + 'http-method' => array( + 'GET' + ) + ) );