Created
February 20, 2018 22:33
-
-
Save danieluyo/a50d95112037fe3c724813f82eb7fabc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Libreria\Cashier; | |
use Conekta\Conekta; | |
use Conekta\Customer; | |
use Conekta\Plan; | |
use Conekta\Order; | |
class Cashier { | |
protected $apiKey; | |
protected $apiVersion; | |
protected $apiLocale; | |
protected $conekta; | |
public function __construct() | |
{ | |
$this->apiKey = env('CONEKTA_PRIVATE'); | |
$this->apiVersion = '2.0.0'; | |
$this->locale = 'es'; | |
Conekta::setApiKey(env('CONEKTA_PRIVATE')); | |
Conekta::setApiVersion('2.0.0'); | |
Conekta::setLocale('es'); | |
} | |
private function metodoPago($metodo, $data = []) | |
{ | |
$obj= []; | |
if($metodo === 'debito'){ | |
$obj = [ | |
'payment_method' => [ | |
"type" => "card", | |
"payment_source_id"=>$data['conekta_uid'] | |
] | |
]; | |
}elseif ($metodo === 'oxxo'){ | |
$obj = [ | |
'payment_method' => [ | |
"type" => "oxxo_cash" | |
] | |
]; | |
} | |
return $obj; | |
} | |
public function conektaStart() | |
{ | |
Conekta::setApiKey($this->apiKey); | |
Conekta::setApiVersion($this->apiVersion); | |
Conekta::setLocale($this->apiLocale); | |
} | |
public function crearCliente($usuario) | |
{ | |
$this->conektaStart(); | |
$cliente = Customer::create([ | |
'name' => $usuario->nombre, | |
'email' => $usuario->email, | |
]); | |
return $cliente; | |
} | |
public function actualizarCliente($usuario, $informacion){ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$cliente->update($informacion); | |
return $cliente; | |
} | |
public function agregarTarjeta($usuario, $tokens = []) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$source = $cliente->createPaymentSource($tokens); | |
return $source; | |
} | |
public function editarTarjeta($usuario, $informacion,$indice = 0) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$source = $cliente->payment_sources[$indice]->update($informacion); | |
return $source; | |
} | |
public function eliminarTarjeta($usuario, $indice) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$cliente->payment_sources[$indice]->delete(); | |
} | |
public function crearPlan($plan) | |
{ | |
$expiry =[ | |
'month' => 1, | |
'year' => 12 | |
]; | |
$conektaPlan = Plan::create([ | |
'id' => $plan->slug, | |
'name' => $plan->nombre, | |
'amount' => $plan->monto, | |
'currency' => strtoupper($plan->moneda), | |
'interval' => $plan->duracion, | |
'frequency' => 1, | |
'trial_period_days' => 0, | |
'expiry_count' => $expiry[$plan->duracion] | |
]); | |
return $conektaPlan; | |
} | |
public function actualizarPlan($plan, $informacion = []) | |
{ | |
$plan = Plan::find($plan->slug); | |
$plan->update($informacion); | |
return $plan; | |
} | |
public function crearSuscripcion($usuario, $plan) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$subscription = $cliente->createSubscription(['plan'=>$plan->slug]); | |
return $subscription; | |
} | |
public function actualizarSuscripcion($usuario, $plan) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$subscription = $cliente->subscription->update(['plan'=>$plan->slug]); | |
return $subscription; | |
} | |
public function cancelarSuscripcion($usuario, $plan) | |
{ | |
$cliente = Customer::find($usuario->conekta_uid); | |
$subscription = $cliente->subscription->cancel(); | |
return $subscription; | |
} | |
public function crearOrden($usuario, $plan, $metodoPago) | |
{ | |
$metodo = $this->metodoPago($metodoPago); | |
$orden = Order::create([ | |
'currency' => $plan->moneda, | |
'customer_info' => [ | |
'customer_id' => $usuario->conekta_uid | |
], | |
'line_items' => [ | |
[ | |
'name' => $plan->nombre, | |
'unit_price' => $plan->monto, | |
'quantity' => 1 | |
] | |
], | |
'charges' => [ | |
[ | |
'payment_method' => [ | |
'type' => 'default' | |
] | |
] | |
] | |
]); | |
return $orden; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment