dimanche 6 juin 2021

Cache On Delivery and Credit , Strategy Pattern

i have an API Back-end with PHP - Laravel

i have multiple payment gateways

  1. PayPal
  2. Stripe
  3. Cache On Delivery ( No Gateway )

My Question Is How To Implement That Using Strategy Pattern ??!


My Suggestion But Not Completed

  • first we create an abstract class that all gateways will inherit from it

abstract class PaymentStrategy
{
    protected Order $order;

    public function __construct(Order $order)
    {
        $this->order = $order; // extract user_id and amount from order
    }


    public abstract function getIFrameUrl(): string;

    /**
     * this is fired after client (front-end) end browser payment
     * we should make sure that the payment is sync in DB
     */
    public abstract function callback(Request $request);
}
  • second we start adding all gateways and make them inherit from PaymentStrategy
class PayPalPayment extends PaymentStrategy
{

    public function getIFrameUrl(): string
    {
        return "url for front end";
    }

    public function callback(Request $request)
    {
        // if requested data is valid
        // if data is matched with order
        // if order is found after redirect to callback

        // then update order
    }
}

class StripePayment extends PaymentStrategy
{

    public function getIFrameUrl(): string
    {
        return "url for front end";
    }

    public function callback(Request $request)
    {
        // TODO: Implement callback() method.
    }
}


class CacheOnDeliveryPayment extends PaymentStrategy
{

    public function getIFrameUrl(): string
    {
        // What To Do Here
        // and how to response to front-end dynamically
    }

    public function callback(Request $request)
    {
        // What To Do Here
    }
}

  • third i don't know how to complete the last class

Aucun commentaire:

Enregistrer un commentaire