Skip to content

Commit

Permalink
Ajout du service TRANSFER
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgu74 committed Oct 2, 2013
1 parent 12278fb commit a6c2c23
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/Payutc/Bom/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use \Payutc\Exception\GingerFailure;
use \Payutc\Exception\UpdateFailed;
use \Payutc\Exception\LoginError;
use \Payutc\Exception\TransferException;
use \Payutc\Bom\Blocked;
use \Payutc\Bom\MsgPerso;
use \Payutc\Log;
Expand Down Expand Up @@ -363,6 +364,49 @@ public function canReload($amount=1000) {
($this->getCredit() + $amount) <= Config::get('credit_max');
}

/**
* VIREMENT
*
* @param int $amount montant du virement en centimes
* @param int $userID Id de la personne a qui l'on vire de l'argent.
* @param string $message
* @return int $error (1 c'est que tout va bien sinon faut aller voir le code d'erreur)
*/
public function transfer($amount, $userID, $message="") {
if($amount < 0) {
Log::warn("TRANSFERT D'ARGENT : TENTATIVE DE FRAUDE... Montant négatif par l'userID ".$this->getId()." vers l'user ".$userID);
throw TransferException("C'est pas fair play de voler de l'argent à ces petits camarades...");
} else if($this->getCredit() < $amount) {
throw TransferException("Pas assez d'argent pour effectuer le virement.");
} else if($this->User->getId() == $userID) {
throw TransferException("Petit malin, se virer de l'argent à soi même n'a aucun sens !");
} else {
if(!User::userExistById($userID)) {
throw TransferException("Il n'y a pas d'utilisateur à qui verser l'argent...");
} else {
$conn = Dbal::conn();
$conn->beginTransaction();
try {
User::incCreditById($userID, $amount);
$this->decCredit($amount);
$conn->insert('t_virement_vir',
array(
"vir_date" => "NOW()",
"vir_amount" => $amount,
"usr_id_from" => $this->getId(),
"usr_id_to" => $userID,
"vir_message" => $message));
$conn->commit();
return 1;
} catch (\Exception $e) {
$conn->rollback();
Log::error("Error during transfer from ".$this->getId()." to ".$userID." amount:".$amount);
throw TransferException("Erreur pendant le virement...");
}
}
}
}

/**
* Returns the last purchases from the user (to allow the seller to cancel them)
*
Expand Down
3 changes: 2 additions & 1 deletion src/Payutc/Mapping/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Services {
'GESARTICLE',
'PAYLINE',
'RELOAD',
'MYACCOUNT'
'MYACCOUNT',
'TRANSFER'
);

public static function get($name) {
Expand Down
7 changes: 7 additions & 0 deletions src/Payutc/Service/ADMINRIGHT.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ public function getServices() {
"desc" => "Permet de consulter un historique, et de bloquer/débloquer sa carte",
"user" => false,
"app" => false
), array(
"service" => "TRANSFER",
"name" => "Virer de l'argent",
"desc" => "Permet de virer de l'argent vers un autre utilisateur",
"user" => false,
"app" => false
)

);
}

Expand Down
31 changes: 31 additions & 0 deletions src/Payutc/Service/TRANSFER.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Payutc\Service;

/**
* TRANSFER.php
*
* Ce service permet d'effectuer des virements.
*
*/

class TRANSFER extends \ServiceBase {

/**
* Faire un virement à quelqu'un
* @param int $amount en centimes
* @param int $userID userID a qui on vire l'argent
* @param string $message message a mettre avec le virement
* @return 1 or exception
*/
public function transfer($amount, $userID, $message="") {
// On a une appli qui a les droits ?
$this->checkRight(false, true, true, null);
// on a un user ?
if(!$this->user()) {
throw new \Payutc\Exception\CheckRightException("Vous devez connecter un utilisateur ! (method loginCas)");
}

return $this->user()->transfer($amount, $userID, $message);
}
}

0 comments on commit a6c2c23

Please sign in to comment.