123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- require_once 'ItemPagopar.php';
- require_once 'BuyerPagopar.php';
- class OrderPagopar{
- public $order = null;
- public $idOrder;
- public $publicKey;
- public $privateKey;
- public $typeOrder;
- public $desc;
- public $periodOfDaysForPayment = null;
- public $periodOfHoursForPayment = null;
-
- public function __construct($id){
- $this->idOrder = $id;
- $this->order = (object) [
- 'tipo_pedido' => null,
- 'fecha_maxima_pago' => null,
- 'public_key' => null,
- 'id_pedido_comercio' => null,
- 'monto_total' => null,
- 'token' => null,
- 'descripcion_resumen' => null,
- 'comprador' => null,
- 'compras_items' => null
- ];
-
- }
-
- public function addPagoparItem($item){
- $error = $this->validateItemAttributes($item->name, $item->qty, $item->price, $item->cityId, $item->productId);
- if($error['status']){
- throw new Exception($error['msg']);
- }
- $this->order->compras_items[] = $item->formatToArray();
- }
-
- private function validateItemAttributes($name, $qty, $price, $cityId, $productId){
- $error = ['status'=>false,'msg'=>''];
- if(empty($name)){
- $error['status'] = true; $error['msg'] = "Hay un error con el nombre de algún producto";
- return $error;
- }
- if(empty($qty) || !is_numeric($qty) || $qty < 0){
- $error['status'] = true; $error['msg'] = "Hay un error en la cantidad del producto '{$name}'";
- return $error;
- }
- if(empty($price) || !is_numeric($price) || $price < 0){
- $error['status'] = true; $error['msg'] = "Hay un error en el precio del producto '{$name}'";
- return $error;
- }
- if(empty($cityId) || !is_numeric($cityId) || $cityId < 0){
- $error['status'] = true; $error['msg'] = "Hay un error en el ID de la ciudad del producto '{$name}'";
- return $error;
- }
- if(empty($productId) || !is_numeric($productId) || $productId < 0){
- $error['status'] = true; $error['msg'] = "Hay un error en el ID del producto '{$name}'";
- return $error;
- }
- return $error;
- }
-
- public function addPagoparBuyer($buyer){
- $error = $this->validateBuyerAttributes($buyer->name,$buyer->email,$buyer->cityId);
- if($error['status']){
- throw new Exception($error['msg']);
- }
- $this->order->comprador = $buyer->formatToArray();
- }
-
- private function validateBuyerAttributes($name,$email,$cityId){
- $error = ['status'=>false,'msg'=>''];
- if(empty($name)){
- $error['status'] = true; $error['msg'] = "Hay un error con el nombre del comprador";
- return $error;
- }
- if(empty($email)){
- $error['status'] = true; $error['msg'] = "Hay un error con el email del comprador";
- return $error;
- }
- if(empty($cityId) || !is_numeric($cityId) || $cityId < 0){
- $error['status'] = true; $error['msg'] = "Hay un error en el ID de la ciudad del comprador";
- return $error;
- }
- return $error;
- }
-
- public function validateOrderAttributes($publicKey,$privateKey,$typeOrder){
- $error = ['status'=>false,'msg'=>''];
-
- if(empty($publicKey)){
- $error['status'] = true;$error['msg'] = "Hay un error con la clave pública";
- }
- if(empty($privateKey)){
- $error['status'] = true;$error['msg'] = "Hay un error con la clave privada";
- }
-
- if(empty($typeOrder)){
- $error['status'] = true;$error['msg'] = "Hay un error con el tipo de Pedido";
- }
- return $error;
- }
-
- public function generateOrderHash($id = null, $amount = 0, $private_key = null){
- return sha1($private_key . $id . $amount);
- }
-
- private function makeMaxDateForPayment(){
- date_default_timezone_set("America/Asuncion");
-
- $daysToHours = ($this->periodOfDaysForPayment)?($this->periodOfDaysForPayment*24):0;
- return date("Y-m-d H:i:s",mktime(date("H")+$this->periodOfHoursForPayment+$daysToHours,
- date("i"),date("s"),date("m"),date("d"),date("Y")));
- }
-
- private function getTotalAmount($items){
- $totalAmount = 0;
- foreach ($items as $item){
- $totalAmount += $item['precio_total'];
- }
- return $totalAmount;
- }
-
- public function makeOrder(){
-
-
- $this->periodOfDaysForPayment =
- ($this->periodOfDaysForPayment>=0 && is_numeric($this->periodOfDaysForPayment))?$this->periodOfDaysForPayment:1;
-
- $this->periodOfHoursForPayment =
- ($this->periodOfHoursForPayment>=0 && is_numeric($this->periodOfHoursForPayment))?$this->periodOfHoursForPayment:0;
- $error = $this->validateOrderAttributes($this->publicKey,$this->privateKey,$this->typeOrder);
- if($error['status']){
- throw new Exception($error['msg']);
- }
- $totalAmount = $this->getTotalAmount($this->order->compras_items);
-
- $this->order->public_key = $this->publicKey;
- $this->order->tipo_pedido = $this->typeOrder;
- $this->order->fecha_maxima_pago = $this->makeMaxDateForPayment();
- $this->order->id_pedido_comercio = $this->idOrder;
- $this->order->monto_total = $totalAmount;
- $this->order->token = $this->generateOrderHash($this->idOrder,$totalAmount,$this->privateKey);
- $this->order->descripcion_resumen = $this->desc;
- return $this->order;
- }
- }
|