diff --git a/_routes.php b/_routes.php index 2eb30ec..ac9274d 100644 --- a/_routes.php +++ b/_routes.php @@ -261,70 +261,84 @@ $this->post( ) { $real_contrib = true; } - - //we'll now try to add the relevant cotisation - if ($post['payment_status'] == 'Completed') { - /** - * We will use the following parameters: - * - mc_gross: the amount - * - custom: member id - * - item_number: contribution type id - * - * If no member id is provided, we only send to post contribution - * script, Galette does not handle anonymous contributions - */ - $args = array( - 'type' => $post['item_number'], - 'adh' => $post['custom'], - 'payment_type' => PaymentType::PAYPAL - ); - if ($this->preferences->pref_membership_ext != '') { - $args['ext'] = $this->preferences->pref_membership_ext; - } - $contrib = new Contribution($this->zdb, $this->login, $args); - $contrib->amount = $post['mc_gross']; - - //all goes well, we can proceed - if ($contrib->isCotis() && $real_contrib) { - // Check that membership fees does not overlap - $overlap = $contrib->checkOverlap(); - if ($overlap !== true) { - if ($overlap === false) { + + if ($ph->isProcessed($post)) + { + $ph->setState('already'); + Analog::log( + 'A paypal payment notification has been received, but it is already processed!', + Analog::WARNING + ); + } + else + { + //we'll now try to add the relevant cotisation + if ($post['payment_status'] == 'Completed') { + /** + * We will use the following parameters: + * - mc_gross: the amount + * - custom: member id + * - item_number: contribution type id + * + * If no member id is provided, we only send to post contribution + * script, Galette does not handle anonymous contributions + */ + $args = array( + 'type' => $post['item_number'], + 'adh' => $post['custom'], + 'payment_type' => PaymentType::PAYPAL + ); + if ($this->preferences->pref_membership_ext != '') { + $args['ext'] = $this->preferences->pref_membership_ext; + } + $contrib = new Contribution($this->zdb, $this->login, $args); + $contrib->amount = $post['mc_gross']; + + //all goes well, we can proceed + if ($contrib->isCotis() && $real_contrib) { + // Check that membership fees does not overlap + $overlap = $contrib->checkOverlap(); + if ($overlap !== true) { + if ($overlap === false) { + Analog::log( + 'An eror occured checking overlaping fees :(', + Analog::ERROR + ); + } else { + //method directly return error message + Analog::log( + 'Error while calculating overlaping fees from paypal payment: ' . $overlap, + Analog::ERROR + ); + } + } + } + + if ($real_contrib) { + $store = $contrib->store(); + if ($store === true) { + $ph->setState('processed'); + //contribution has been stored :) Analog::log( - 'An eror occured checking overlaping fees :(', - Analog::ERROR + 'Paypal payment has been successfully registered as a contribution', + Analog::INFO ); } else { - //method directly return error message + //something went wrong :'( + $ph->setState('errorstoring'); Analog::log( - 'Error while calculating overlaping fees from paypal payment: ' . $overlap, + 'An error occured while storing a new contribution from Paypal payment', Analog::ERROR ); } } + } else { + $ph->setState('notcompleted'); + Analog::log( + 'A paypal payment notification has been received, but is not completed!', + Analog::WARNING + ); } - - if ($real_contrib) { - $store = $contrib->store(); - if ($store === true) { - //contribution has been stored :) - Analog::log( - 'Paypal payment has been successfully registered as a contribution', - Analog::INFO - ); - } else { - //something went wrong :'( - Analog::log( - 'An error occured while storing a new contribution from Paypal payment', - Analog::ERROR - ); - } - } - } else { - Analog::log( - 'A paypal payment notification has been received, but is not completed!', - Analog::WARNING - ); } } else { Analog::log( diff --git a/lib/GalettePaypal/PaypalHistory.php b/lib/GalettePaypal/PaypalHistory.php index 0cab2b7..c2b2810 100644 --- a/lib/GalettePaypal/PaypalHistory.php +++ b/lib/GalettePaypal/PaypalHistory.php @@ -63,6 +63,8 @@ class PaypalHistory extends History public const TABLE = 'history'; public const PK = 'id_paypal'; + private $_id; + protected $_types = array( 'text', 'date', @@ -110,13 +112,18 @@ class PaypalHistory extends History 'history_date' => date('Y-m-d H:i:s'), 'amount' => $request['mc_gross'], 'comments' => $request['item_name'], - 'request' => serialize($request) + 'request' => serialize($request), + 'signature' => $request['verify_sign'], + 'state' => '' ); + //ALTER TABLE `galetteT1_paypal_history` ADD `signature` VARCHAR(255) NOT NULL AFTER `id_paypal`; + //ALTER TABLE `galetteT1_paypal_history` ADD `state` VARCHAR(20) NOT NULL AFTER `signature`; $insert = $this->zdb->insert($this->getTableName()); $insert->values($values); $this->zdb->execute($insert); - + $this->_id = $this->zdb->driver->getLastGeneratedValue(); + Analog::log( 'An entry has been added in paypal history', Analog::INFO @@ -216,4 +223,32 @@ class PaypalHistory extends History return $order; } + + public function isProcessed($request) + { + $select = $this->zdb->select($this->getTableName()); + $select->where([ + 'signature' => $request['verify_sign'], + 'state' => 'processed' + ]); + $results = $this->zdb->execute($select); + return count($results)>0; + } + + public function setState($state) + { + try { + $update = $this->zdb->update($this->getTableName()); + $update->set(['state' => $state]) + ->where(['id_paypal' => $this->_id]); + $this->zdb->execute($update); + } + catch (\Exception $e) { + Analog::log( + 'An error occurred when updating state field | ' . $e->getMessage(), + Analog::ERROR + ); + } + } + }