escolalms/payments

Escola LMS Payments Package.

Installs: 1 030

Dependents: 3

Suggesters: 0

Security: 0

Stars: 0

Watchers: 4

Forks: 0

Open Issues: 2

Type:package

0.1.8 2021-11-19 13:20 UTC

README

swagger codecov phpunit downloads downloads downloads Maintainability

Facades

Payments Facade

Use this facade for starting payment processing. You can create PaymentProcessor` either from a model using Payable trait or from precreated Payment object.

use EscolaLms\Cart\Models\Cart;
use EscolaLms\Payments\Dtos\PaymentMethodDto;
use EscolaLms\Payments\Facades\Payments;

$payable = Cart::find($id); // Cart must implement Payable interface and use Payable trait
$paymentMethodDto = PaymentMethodDto::instantiateFromRequest($request);
$processor = Payments::processPayment($payable);
$processor->purchase($paymentMethodDto); // will emit PaymentPaid event on success
if($payment->status->is(PaymentStatus::PAID)){
    // ...
}

PaymentGateway Facade

With this facade you can call payment provider gateways directly.

For existing payment you can for example do:

use EscolaLms\Payments\Dtos\PaymentMethodDto;
use EscolaLms\Payments\Facades\PaymentGateway;
use EscolaLms\Payments\Models\Payment;

$payment = Payment::find($id);
$paymentMethodDto = PaymentMethodDto::instantiateFromRequest($request);
$paymentDto = PaymentDto::instantiateFromPayment($payment); // or you can create it manually
PaymentGateway::purchase($paymentDto, $paymentMethodDto); // will use default payment driver

Important: This will not save Payment object.

To use specific driver, you can call

PaymentGateway::driver('stripe')->purchase($paymentDto, $paymentMethodDto);

Available payemtn drivers:

  • stripe (using Stripe Payment Intent)
  • free
  • TODO: stripe-checkout

Traits

Billable

Billable trait and interface must be included in User model (or any other model that represents an entity that pays for the Payment).

Payable

Payable trait and interface are the core of this package, enabling simplified calling of PaymentsService and GatewayManager. When you include it in your model that represents a Payable (for example Cart or Order or Product) you can begin payment processing for that Payable by calling $payable->process() which calls Payments::processPayable($this) and automatically creates a Payment and returns a PaymentProcessor instance for that Payment.

PaymentProcessor

Let's you process payments, with automatically setting payment status after purchase and emiting events.

use EscolaLms\Payments\Dtos\PaymentMethodDto;
use EscolaLms\Payments\Entities\PaymentProcessor;
use EscolaLms\Payments\Models\Payment;

$payment = Payment::find($id);
$paymentMethodDto = PaymentMethodDto::instantiateFromRequest($request);
$processor = new PaymentProcessor($payment); // instead of using Payments facade
$processor->purchase($paymentMethodDto);

PaymentProcessor automatically selects free driver when payment amount equals 0.