|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\LaravelGoldenpay\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
7
|
|
|
use Orkhanahmadov\Goldenpay\Enums\CardType; |
|
8
|
|
|
use Orkhanahmadov\Goldenpay\Enums\Language; |
|
9
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Goldenpay; |
|
10
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Models\Payment; |
|
11
|
|
|
|
|
12
|
|
|
trait Payable |
|
13
|
|
|
{ |
|
14
|
|
|
public function payments(): MorphMany |
|
15
|
|
|
{ |
|
16
|
|
|
return $this->morphMany(Payment::class, 'payable'); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function successfulPayments(): MorphMany |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->morphMany(Payment::class, 'payable')->successful(); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $amount |
|
26
|
|
|
* @param CardType $cardType |
|
27
|
|
|
* @param string|null $description |
|
28
|
|
|
* @param Language|null $lang |
|
29
|
|
|
* |
|
30
|
|
|
* @return Payment |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
33
|
|
|
* @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function createPayment( |
|
36
|
|
|
int $amount, |
|
37
|
|
|
CardType $cardType, |
|
38
|
|
|
?string $description = null, |
|
39
|
|
|
?Language $lang = null |
|
40
|
|
|
): Payment { |
|
41
|
|
|
/** @var Goldenpay $goldenpay */ |
|
42
|
|
|
$goldenpay = Container::getInstance()->make(Goldenpay::class); |
|
43
|
|
|
|
|
44
|
|
|
$payment = $goldenpay->payment($amount, $cardType, $description ?: $this->description(), $lang); |
|
45
|
|
|
|
|
46
|
|
|
$payment->payable_type = self::class; |
|
47
|
|
|
$payment->payable_id = $this->getKey(); |
|
|
|
|
|
|
48
|
|
|
$payment->save(); |
|
49
|
|
|
|
|
50
|
|
|
return $payment; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Define description for this model's payments. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract public function description(): string; |
|
59
|
|
|
} |
|
60
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.