PaysafeCard
Unlike the reference-based methods, PaysafeCard is a redirect flow: Eupago
returns a payment url that you must redirect the customer to, along with a
reference for the payment (there is no entity). You also pass your own id
(e.g. the order id), which Eupago echoes back in the callback as identificador,
and you may optionally pass a url_retorno to control where the customer lands
after paying.
References are stored against your own models, so add the HasPaysafeCardReferences trait to each
model that takes PaysafeCard payments:
use CodeTech\EuPago\Traits\HasPaysafeCardReferences;
class Order extends Model
{
use HasPaysafeCardReferences;
}
Then create the payment and save it through the trait's relationship:
use CodeTech\EuPago\PaysafeCard\PaysafeCard;
$order = Order::find(1);
$paysafeCard = new PaysafeCard(
$order->value,
$order->id,
route('checkout.return') // optional url_retorno
);
try {
$paysafeCardReferenceData = $paysafeCard->create();
if ($paysafeCard->hasErrors()) {
// handle errors
}
$reference = $order->paysafeCardReferences()->create($paysafeCardReferenceData);
// Redirect the customer to PaysafeCard to complete the payment
return redirect()->away($reference->url);
} catch (\Exception $e) {
// handle exception
}
$paysafeCardReferenceData contains the normalized payment information:
[
'success' => true,
'state' => 0,
'response' => "OK",
'identifier' => "order-49",
'reference' => "000017428",
'url' => "https://clientes.eupago.pt/paysafecard/pay/...",
'value' => 25.00,
]
Creating and saving in one call
The trait can also create and persist a reference in a single call. It
returns the persisted reference (whose url you redirect to) on success, or the errors
on failure:
$reference = $order->createPaysafeCardReference($value, $id, $returnUrl);
Retrieve the PaysafeCard references:
$paysafeCardReferences = $order->paysafeCardReferences;
When the payment is completed, the callback fires a PaysafeCardReferencePaid
event and stores the Eupago transaction on the reference, so a paid reference can be
refunded through $reference->transaction_id.