|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Payment\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Payment\Contracts\Chargeable; |
|
6
|
|
|
use App\Containers\Payment\Exceptions\ObjectNonChargeableException; |
|
7
|
|
|
use App\Containers\Payment\Exceptions\PaymentMethodNotFoundException; |
|
8
|
|
|
use App\Containers\Payment\Exceptions\UserNotSetInThePaymentTaskException; |
|
9
|
|
|
use App\Containers\Paypal\Tasks\ChargeWithPaypalTask; |
|
10
|
|
|
use App\Containers\Stripe\Tasks\ChargeWithStripeTask; |
|
11
|
|
|
use App\Containers\User\Models\User; |
|
12
|
|
|
use Illuminate\Support\Facades\App; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PaymentsFactory |
|
16
|
|
|
* |
|
17
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PaymentsFactory |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
protected $method = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param \App\Containers\User\Models\User $user |
|
26
|
|
|
* @param $amount |
|
27
|
|
|
* @param string $currency |
|
28
|
|
|
* |
|
29
|
|
|
* @return mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public function charge(User $user, $amount, $currency = 'USD') |
|
32
|
|
|
{ |
|
33
|
|
|
$this->setUserPaymentMethod($user); |
|
34
|
|
|
|
|
35
|
|
|
return $this->method->charge($user, $amount, $currency); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \App\Containers\User\Models\User $user |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setUserPaymentMethod(User $user) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($user->stripeAccount !== null) { |
|
44
|
|
|
$this->method = App::make(ChargeWithStripeTask::class); |
|
45
|
|
|
} elseif ($user->paypalAccount !== null) { |
|
46
|
|
|
$this->method = App::make(ChargeWithPaypalTask::class); |
|
47
|
|
|
} |
|
48
|
|
|
// elseif ($user->...Account !== null) { |
|
|
|
|
|
|
49
|
|
|
// $this->method = App::make(ChargeWith...Task::class); |
|
50
|
|
|
// } |
|
51
|
|
|
|
|
52
|
|
|
// validate a payment method was found |
|
53
|
|
|
if ($this->method == null) { |
|
54
|
|
|
throw new PaymentMethodNotFoundException(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// validate containing charge function |
|
58
|
|
|
if (!$this->method instanceof Chargeable) { |
|
59
|
|
|
throw (new ObjectNonChargeableException()) |
|
60
|
|
|
->debug('The payment services must implement the Chargeable interface'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return null |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getPaymentMethod() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->method; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.