|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Payments\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Payments\Contracts\Chargeable; |
|
6
|
|
|
use App\Containers\Payments\Exceptions\ObjectNonChargeableException; |
|
7
|
|
|
use App\Containers\Payments\Exceptions\PaymentMethodNotFoundException; |
|
8
|
|
|
use App\Containers\Payments\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
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.