· 6 years ago · Mar 10, 2019, 05:32 AM
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use App\Strategy\Payment\Payment;
7use App\Strategy\Payment\PayByStripe;
8use App\Strategy\Payment\PayByEsewa;
9
10class PaymentController extends Controller
11{
12 public function index()
13 {
14 $payment = new Payment( new PayByStripe($amount = 3) );
15 $payment->pay();
16 }
17}
18
19<?php
20namespace App\Strategy\Payment;
21
22interface PaymentInterface {
23 public function pay();
24}
25
26
27<?php
28namespace App\Strategy\Payment;
29use App\Strategy\Payment\PaymentInterface;
30
31class PayByStripe implements PaymentInterface
32{
33 private $secret_key;
34 private $total_amount;
35 private $token;
36
37 public function __construct($total_amount)
38 {
39 $this->secret_key = 'stripe_secret_key';
40 $this->total_amount = $total_amount;
41 $this->token = $this->generateToken();
42 }
43
44 public function generateToken(){
45 return 'token';
46 }
47 public function pay()
48 {
49 var_dump('secret_key'. $this->secret_key);
50 var_dump('total_amount'. $this->total_amount);
51 var_dump('token'. $this->token);
52 var_dump('pay with stripe');
53 }
54}
55
56<?php
57namespace App\Strategy\Payment;
58
59class Payment {
60 private $strategy = NULL;
61 public function __construct(PaymentInterface $strategy) {
62
63 $this->strategy = $strategy;
64 }
65
66 public function pay()
67 {
68 return $this->strategy->pay();
69 }
70}
71
72<?php
73namespace App\Strategy\Payment;
74use App\Strategy\Payment\PaymentInterface;
75
76class PayByEsewa implements PaymentInterface
77{
78 public function pay()
79 {
80 var_dump('pay with esewa');
81 }
82}