Laravel Bind Multiple Class to One Contract in The Service Provider -


so trying have eccomerce site handle both paypal , stripe payments. however, not sure how done.

this have.

class billingprovider extends serviceprovider  {      /**       * bootstrap application services.       *       * @return void       */      public function boot()      {          //      }        /**       * register application services.       *       * @return void       */      public function register()      {          $this->app->bind('app\contracts\billinginterface','app\services\billing\paypalbilling');      }  }

this works fine if need paypal. however, need both paypal , stripe.

any idea how can go implementing this? when typehint can either paypal or stripe.

public function stripe(request $request, billinginterface $bill){// handle stripe payment.}  public function paypal(request $request, billinginterface $bill){// handle paypal payment.} 

imho, there many ways it.

first of all, take contextual binding of service container. if you're using different classes on checkout procedure, can set resolve strategy based on using contract.

however, choose different.

let's apply strategy design pattern this.

basically, in 2 separate steps:

  • create "top" class named, example, billingmanager;
  • a class each payment type (imho, make paypal() , stripe() method in single class wrong because of this) implement billing interface. let's interface has 'process($orderid)` method;

now, billingmanager class called. probably, call general method pay take, parameter, understand strategy use.

something like

public function pay($strategy, $orderid) {     // use $strategy understand service must used.     // raw example:      $strategy = app()->make($strategy . 'strategy');     $strategy->process($orderid); } 

of course, need safe mechanism understand service need use. after that, able use billingmanager class every time, many different payment systems, without worry else.


Comments

Popular posts from this blog

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -