PHP code example of potelo / gu-payment
1. Go to this page and download the library: Download potelo/gu-payment library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
potelo / gu-payment example snippets
Potelo\GuPayment\GuPaymentServiceProvider::class,
use Potelo\GuPayment\GuPaymentTrait;
class User extends Authenticatable
{
use GuPaymentTrait;
}
'iugu' => [
'model' => App\User::class,
'key' => env('IUGU_APIKEY'),
'signature_table' => env('GUPAYMENT_SIGNATURE_TABLE'),
'model_foreign_key' => env('IUGU_MODEL_FOREIGN_KEY'),
]
$user = User::find(1);
$user->newSubscription('main', 'gold')->create($creditCardToken);
$user = User::find(1);
$user->newSubscription('main', 'gold')
->chargeOnSuccess()
->create($creditCardToken);
$subItems = [
[
'description' => 'Desconto recorrente',
'price_cents' => -900,
'quantity' => 1,
'recurrent' => true,
],
[
'description' => 'Adicional não recorrente',
'price_cents' => 250,
'quantity' => 1,
'recurrent' => false,
]
];
// Create Subscription
$user->newSubscription('main', 'gold')
->subItems($subItems)
->create($creditCardToken);
$subItem = [
'description' => 'Desconto recorrente',
'price_cents' => -900,
'quantity' => 1,
'recurrent' => true,
];
// Create Subscription
$user->newSubscription('main', 'gold')
->addSubItem($subItem)
->create($creditCardToken);
$user = User::find(1);
$user->newSubscription('main', 'gold', [
'adicional_assinatura' => 'boa assinatura'
])->create(NULL);
$user = User::find(1);
'$user->newSubscription('main', 'gold', [], ['ignore_due_email' => true])
->create(NULL, [
'name' => $user->nome,
'notes' => 'Anotações gerais'
]);
$user = User::find(1);
$subscriptionBuilder = $user->newSubscription('main', 'gold');
$subscription = $subscriptionBuilder->trialDays(20)->create($creditCardToken);
if ($subscription) {
// TUDO ok
} else {
$erros = $subscriptionBuilder->getLastError();
if (is_array($erros)) {
// array
} else {
// string
}
}
if ($user->subscribed('main')) {
//
}
public function handle($request, Closure $next)
{
if ($request->user() && ! $request->user()->subscribed('main')) {
// This user is not a paying customer...
return redirect('billing');
}
return $next($request);
}
if ($user->subscription('main')->onTrial()) {
//
}
if ($user->onPlan('gold')) {
//
}
if ($user->subscription('main')->cancelled()) {
//
}
if ($user->subscription('main')->onGracePeriod()) {
//
}
$user->subscription('main')->asIuguSubscription();
$user = App\User::find(1);
$user->subscription('main')->swap('silver');
$simulation = $user->subscription('main')->swapPlanSimulation('silver');
$cost = $simulation->cost;
$discount = $simulation->discount;
$cycles = $simulation->cycles;
$oldPlan = $simulation->old_plan;
$newPlan = $simulation->new_plan;
$expiresAt = $simulation->expires_at;
$user = App\User::find(1);
$skipCharge = true;
$user->subscription('main')->swap('silver', $skipCharge);
$user = App\User::find(1);
$skipCharge = true;
$nextDue = Carbon::now()->addDays(10);
$user->subscription('main')->swap('silver', $skipCharge, $nextDue);
$user->subscription('main')->cancel();
$user->subscription('main')->cancelNow();
$user->subscription('main')->resume();
$user = User::find(1);
$user->newSubscription('main', 'gold')
->trialDays(10)
->create($creditCardToken);
if ($user->subscription('main')->onTrial()) {
//
}
Route::post('webhook', '\Potelo\GuPayment\Http\Controllers\WebhookController@handleWebhook');
protected $except = [
'webhook',
];
Route::post('webhook', 'MeuWebhookController@handleWebhook');
namespace App\Http\Controllers;
use Potelo\GuPayment\Http\Controllers\WebhookController;
class MeuWebhookController extends WebhookController {
public function handleInvoiceCreated(array $payload)
{
return 'Fatura criada: ' . $payload['data']['id'];
}
}
$invoices = $user->invoices();
$invoices = $user->invoices(true);
return $user->downloadInvoice($invoiceId, [
'vendor' => 'Sua Empresa',
'product' => 'Seu Produto',
]);
$invoice = $user->createInvoice($priceCents, $dueDate, $description, $options);
$invoiceBuilder = $user->newInvoice($dueDate);
foreach ($itens as $item) {
$invoiceBuilder->addItem($item['priceCents'], $item['description'], $item['quantity']);
}
$invoice = $invoiceBuilder->create($options);
// Iugu aceita cobranças em centavos
$user->refund($invoiceId);
// Envie o id da fatura e as configurações
$config = [
'due_date' => "2021-12-16",
'keep_early_payment_discount' => true,
'current_fines_option' => true,
'ignore_canceled_email' => true,
'ignore_due_email' => true
];
$user->duplicate($invoiceId, $config);
// Criar cliente no Iugu
$user->createAsIuguCustomer();
// Criar cliente no Iugu com token do cartão de crédito
$user->createAsIuguCustomer($creditCardToken);
$iuguCustomer = $user->asIuguCustomer();
$user->createCard($creditCardToken);
$user->createCard($creditCardToken, [
'set_as_default' => false,
]);
// Coleção de cartões
$user->cards();
// Um cartão ou null
$card = $user->findCard($cardId);
try {
$card = $user->findCardOrFail($cardId);
} catch(Exception $e) {
//
}
$card = $user->findCard($cardId);
$user->deleteCard($card);
$user->deleteCards();
// Iugu aceita cobranças em centavos
$user->charge(100);
$user->charge(100, [
'customer_payment_method_id' => $card->id,
]);
$user->charge(null, [
'items' => [
['description' => 'Primeiro Item', 'quantity' => 10, 'price_cents' => 200],
['description' => 'Segundo Item', 'quantity' => 2, 'price_cents' => 200],
]
]);
php artisan vendor:publish --tag=migrations
$user = $this->createUser();
// Create Subscription
$user->newSubscription('main', 'gold')->validateCard()->create($this->getTestToken());
html
<table>
@foreach ($user->invoices() as $invoice)
<tr>
<td>{{ $invoice->date() }}</td>
<td>{{ $invoice->total() }}</td>
<td><a href="/user/invoice/{{ $invoice->id }}">Download</a></td>
</tr>
@endforeach
</table>