Subiendo proyecto completo sin restricciones de git ignore
This commit is contained in:
13
vendor/sebacarrasco93/laravel-payku/src/Facades/LaravelPayku.php
vendored
Normal file
13
vendor/sebacarrasco93/laravel-payku/src/Facades/LaravelPayku.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class LaravelPayku extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'laravel-payku';
|
||||
}
|
||||
}
|
||||
44
vendor/sebacarrasco93/laravel-payku/src/Http/Controllers/PaykuController.php
vendored
Normal file
44
vendor/sebacarrasco93/laravel-payku/src/Http/Controllers/PaykuController.php
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use SebaCarrasco93\LaravelPayku\Facades\LaravelPayku;
|
||||
use SebaCarrasco93\LaravelPayku\Models\PaykuTransaction;
|
||||
|
||||
class PaykuController
|
||||
{
|
||||
public function create(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'email' => 'required|email',
|
||||
'order' => 'required|unique:payku_transactions,id',
|
||||
'subject' => 'required',
|
||||
'amount' => 'required|int',
|
||||
]);
|
||||
|
||||
return LaravelPayku::create($data['order'], $data['subject'], $data['amount'], $data['email']);
|
||||
}
|
||||
|
||||
public function return($order)
|
||||
{
|
||||
$detail = LaravelPayku::return($order);
|
||||
|
||||
return $detail;
|
||||
}
|
||||
|
||||
public function notify($order)
|
||||
{
|
||||
$result = LaravelPayku::notify($order);
|
||||
$routeName = config('laravel-payku.route_finish_name');
|
||||
|
||||
$routeExists = Route::has($routeName);
|
||||
|
||||
if ($routeExists) {
|
||||
return redirect()->route($routeName, $result);
|
||||
}
|
||||
|
||||
return view('payku::notify.missing-route', compact('result', 'routeName'));
|
||||
}
|
||||
}
|
||||
175
vendor/sebacarrasco93/laravel-payku/src/LaravelPayku.php
vendored
Normal file
175
vendor/sebacarrasco93/laravel-payku/src/LaravelPayku.php
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku;
|
||||
|
||||
use SebaCarrasco93\LaravelPayku\Models\PaykuTransaction;
|
||||
use SebaCarrasco93\LaravelPayku\Traits\DatabaseSimulation;
|
||||
use SebaCarrasco93\LaravelPayku\Traits\PrepareOrders;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class LaravelPayku
|
||||
{
|
||||
use DatabaseSimulation, PrepareOrders;
|
||||
|
||||
// From Response API
|
||||
public $hasValidResponse = false;
|
||||
public $status, $id, $created_at, $order, $email, $subject, $amount;
|
||||
|
||||
// URLs
|
||||
const URL_API_DEV = 'https://des.payku.cl/api';
|
||||
const URL_API_PROD = 'https://app.payku.cl/api';
|
||||
|
||||
// For API Interaction...
|
||||
public $client;
|
||||
public $minimumApiKeys = ['public_token', 'private_token'];
|
||||
|
||||
// For handle with API
|
||||
public $allowedTransactionsStatuses = ['register', 'pending', 'success', 'failed'];
|
||||
// public $allowedTransactionsKeys = ['status', 'id', 'created_at', 'order', 'email', 'subject', 'amount'];
|
||||
public $allowedPaymentKeys = [
|
||||
'start', 'end', 'media', 'transaction_id', 'verification_key', 'authorization_code',
|
||||
'last_4_digits', 'installments', 'card_type', 'additional_parameters', 'currency',
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new GuzzleClient([
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . config('laravel-payku.public_token'),
|
||||
'User-Agent' => 'sebacarrasco93/laravel-payku'
|
||||
],
|
||||
'base_uri' => $this->apiRoute() . '/'
|
||||
]);
|
||||
|
||||
$this->hasValidConfig();
|
||||
}
|
||||
|
||||
public function apiRoute()
|
||||
{
|
||||
if (config('laravel-payku.base_url')) {
|
||||
return config('laravel-payku.base_url');
|
||||
}
|
||||
|
||||
if (config('app.env') == 'production') {
|
||||
return self::URL_API_PROD;
|
||||
}
|
||||
|
||||
return self::URL_API_DEV;
|
||||
}
|
||||
|
||||
public function findApiKeys()
|
||||
{
|
||||
$found = [];
|
||||
|
||||
foreach ($this->minimumApiKeys as $key) {
|
||||
$found[$key] = config('laravel-payku')[$key];
|
||||
}
|
||||
|
||||
return array_filter($found);
|
||||
}
|
||||
|
||||
public function hasValidConfig()
|
||||
{
|
||||
$count = count($this->minimumApiKeys);
|
||||
|
||||
if (count($this->findApiKeys()) == $count) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new \Exception('Please set all PAYKU keys in your .env file.');
|
||||
}
|
||||
|
||||
public function postApi(string $transaction_id, string $subject, int $amountCLP, string $email)
|
||||
{
|
||||
$body = $this->client->request('POST', 'transaction', [
|
||||
'json' => $this->prepareOrder($transaction_id, $subject, $amountCLP, $email),
|
||||
])->getBody();
|
||||
|
||||
return json_decode($body, true);
|
||||
}
|
||||
|
||||
public function getApi(PaykuTransaction $transaction)
|
||||
{
|
||||
$body = $this->client->request('GET', 'transaction/' . $transaction->id)->getBody();
|
||||
|
||||
return json_decode($body, true);
|
||||
}
|
||||
|
||||
public function handleAPIResponse($response)
|
||||
{
|
||||
if (! in_array($response['status'], $this->allowedTransactionsStatuses)) {
|
||||
throw new \Exception("Invalid response status: " . $response['status']);
|
||||
}
|
||||
|
||||
$this->hasValidResponse = true;
|
||||
|
||||
// $this->status = $response['status'];
|
||||
foreach ($response as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function saveAPIResponse($response, $transaction_id = null)
|
||||
{
|
||||
$response = collect($response);
|
||||
$this->handleAPIResponse($response);
|
||||
// dd($response);
|
||||
|
||||
if ($transaction_id) { // Creating...
|
||||
$response['order'] = $transaction_id;
|
||||
}
|
||||
|
||||
$firstResponse = $response->except('payment', 'gateway_response')->toArray();
|
||||
|
||||
if ($firstResponse['status'] != 'failed') {
|
||||
$transaction = PaykuTransaction::updateOrCreate(['id' => $response['id']], $firstResponse);
|
||||
|
||||
if (isset($response['payment'])) {
|
||||
$payment = collect($response['payment']);
|
||||
if ($payment->count()) {
|
||||
$transaction->payment()->create($payment->toArray());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("Can't create your transaction with ID ${transaction_id}");
|
||||
}
|
||||
}
|
||||
|
||||
public function create(string $transaction_id, string $subject, int $amountCLP, string $email)
|
||||
{
|
||||
$response = $this->postApi($transaction_id, $subject, $amountCLP, $email);
|
||||
$database = $this->saveAPIResponse($response, $transaction_id);
|
||||
|
||||
return redirect()->away($response['url']);
|
||||
}
|
||||
|
||||
public function return($order)
|
||||
{
|
||||
$found = PaykuTransaction::whereOrder($order)->firstOrFail();
|
||||
$response = $this->getApi($found);
|
||||
$this->saveAPIResponse($response);
|
||||
|
||||
return redirect()->route('payku.notify', $order);
|
||||
}
|
||||
|
||||
public function notify($order)
|
||||
{
|
||||
return PaykuTransaction::whereOrder($order)->firstOrFail();
|
||||
}
|
||||
|
||||
public function findById($id)
|
||||
{
|
||||
return PaykuTransaction::whereId($id)->firstOrFail();
|
||||
}
|
||||
|
||||
public function hasStatusSuccess($id)
|
||||
{
|
||||
return $this->findById($id)->status == 'success';
|
||||
}
|
||||
|
||||
public function hasStatusPending($id)
|
||||
{
|
||||
return $this->findById($id)->status == 'pending';
|
||||
}
|
||||
}
|
||||
43
vendor/sebacarrasco93/laravel-payku/src/LaravelPaykuServiceProvider.php
vendored
Normal file
43
vendor/sebacarrasco93/laravel-payku/src/LaravelPaykuServiceProvider.php
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class LaravelPaykuServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
|
||||
|
||||
$this->loadMigrationsFrom($this->basePath('database/migrations'));
|
||||
|
||||
$this->loadViewsFrom($this->basePath('resources/views'), 'payku');
|
||||
|
||||
$this->loadTranslationsFrom($this->basePath('resources/lang'), 'laravel-payku');
|
||||
|
||||
$this->publishes([
|
||||
$this->basePath('config/laravel-payku.php') => base_path('config/laravel-payku.php')
|
||||
], 'laravel-payku-config');
|
||||
|
||||
$this->publishes([
|
||||
$this->basePath('database/migrations') => database_path('migrations')
|
||||
], 'laravel-payku-migrations');
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('laravel-payku', function() {
|
||||
return new LaravelPayku;
|
||||
});
|
||||
|
||||
$this->mergeConfigFrom(
|
||||
$this->basePath('config/laravel-payku.php'), 'laravel-payku'
|
||||
);
|
||||
}
|
||||
|
||||
public function basePath($path = '')
|
||||
{
|
||||
return __DIR__ . '/../' . $path;
|
||||
}
|
||||
}
|
||||
10
vendor/sebacarrasco93/laravel-payku/src/Models/PaykuPayment.php
vendored
Normal file
10
vendor/sebacarrasco93/laravel-payku/src/Models/PaykuPayment.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PaykuPayment extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
43
vendor/sebacarrasco93/laravel-payku/src/Models/PaykuTransaction.php
vendored
Normal file
43
vendor/sebacarrasco93/laravel-payku/src/Models/PaykuTransaction.php
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PaykuTransaction extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
public $incrementing = false;
|
||||
|
||||
public function payment()
|
||||
{
|
||||
return $this->hasOne(PaykuPayment::class, 'transaction_id');
|
||||
}
|
||||
|
||||
public function scopeRegister($query)
|
||||
{
|
||||
$query->where('status', 'register');
|
||||
}
|
||||
|
||||
public function scopeSuccess($query)
|
||||
{
|
||||
$query->where('status', 'success');
|
||||
}
|
||||
|
||||
public function scopePending($query)
|
||||
{
|
||||
$query->where('status', 'pending');
|
||||
}
|
||||
|
||||
public function markAsNotified()
|
||||
{
|
||||
return $this->update(['notified_at' => now()]);
|
||||
}
|
||||
|
||||
public function notifyForFirstTime()
|
||||
{
|
||||
if (! $this->notified_at) {
|
||||
return $this->markAsNotified();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
vendor/sebacarrasco93/laravel-payku/src/RouteServiceProvider.php
vendored
Normal file
19
vendor/sebacarrasco93/laravel-payku/src/RouteServiceProvider.php
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected $namespace = 'SebaCarrasco93\LaravelPayku\Http\Controllers';
|
||||
|
||||
public function map()
|
||||
{
|
||||
Route::namespace($this->namespace)
|
||||
->name('payku.')
|
||||
->prefix(config('laravel-payku.route_prefix')) // "payku"
|
||||
->group(__DIR__ . '/../routes/web.php');
|
||||
}
|
||||
}
|
||||
31
vendor/sebacarrasco93/laravel-payku/src/Traits/DatabaseSimulation.php
vendored
Normal file
31
vendor/sebacarrasco93/laravel-payku/src/Traits/DatabaseSimulation.php
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Traits;
|
||||
|
||||
use SebaCarrasco93\LaravelPayku\Models\PaykuTransaction;
|
||||
|
||||
trait DatabaseSimulation
|
||||
{
|
||||
public function completeTransaction($order_id, $response)
|
||||
{
|
||||
$response = collect($response);
|
||||
|
||||
$transaction = new PaykuTransaction();
|
||||
|
||||
$updated_transaction = $transaction->complete($order_id, [
|
||||
'status' => 'success',
|
||||
'order_id' => 100,
|
||||
'subject' => 'Test',
|
||||
'email' => 'seba@sextanet.cl',
|
||||
]);
|
||||
}
|
||||
|
||||
public function storePayment($response)
|
||||
{
|
||||
$response = collect($response)->toArray();
|
||||
|
||||
$found = PaykuTransaction::firstWhere('order_id', $response['id']);
|
||||
|
||||
return $found->payment()->create($response);
|
||||
}
|
||||
}
|
||||
21
vendor/sebacarrasco93/laravel-payku/src/Traits/PrepareOrders.php
vendored
Normal file
21
vendor/sebacarrasco93/laravel-payku/src/Traits/PrepareOrders.php
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace SebaCarrasco93\LaravelPayku\Traits;
|
||||
|
||||
use SebaCarrasco93\LaravelPayku\Models\PaykuTransaction;
|
||||
|
||||
trait PrepareOrders
|
||||
{
|
||||
public function prepareOrder(string $order, string $subject, int $amountCLP, string $email, $paymentId = 1)
|
||||
{
|
||||
return [
|
||||
'email' => $email,
|
||||
'order' => $order,
|
||||
'subject' => $subject,
|
||||
'amount' => $amountCLP,
|
||||
'payment' => $paymentId, // Weppay
|
||||
'urlreturn' => route('payku.return', $order),
|
||||
'urlnotify' => route('payku.notify', $order),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user