1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\ProductEnded; |
6
|
|
|
use Bavix\Wallet\Interfaces\Product; |
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
|
12
|
|
|
trait CanBePaid |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
use HasWallet; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Product $product |
19
|
|
|
* @param bool $force |
20
|
|
|
* @return Transfer |
21
|
|
|
* @throws |
22
|
|
|
*/ |
23
|
5 |
|
public function pay(Product $product, bool $force = false): Transfer |
24
|
|
|
{ |
25
|
5 |
|
if (!$product->canBuy($this, $force)) { |
|
|
|
|
26
|
2 |
|
throw new ProductEnded('The product is out of stock'); |
27
|
|
|
} |
28
|
|
|
|
29
|
5 |
|
if ($force) { |
30
|
1 |
|
return $this->forceTransfer($product, $product->getAmountProduct(), $product->getMetaProduct()); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
return $this->transfer($product, $product->getAmountProduct(), $product->getMetaProduct()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Product $product |
38
|
|
|
* @param bool $force |
39
|
|
|
* @return Transfer|null |
40
|
|
|
*/ |
41
|
1 |
|
public function safePay(Product $product, bool $force = false): ?Transfer |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
1 |
|
return $this->pay($product, $force); |
45
|
1 |
|
} catch (\Throwable $throwable) { |
46
|
1 |
|
return null; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Product $product |
52
|
|
|
* @return Transfer |
53
|
|
|
* @throws |
54
|
|
|
*/ |
55
|
1 |
|
public function forcePay(Product $product): Transfer |
56
|
|
|
{ |
57
|
1 |
|
return $this->pay($product, true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Product $product |
62
|
|
|
* @return null|Transfer |
63
|
|
|
*/ |
64
|
4 |
|
public function paid(Product $product): ?Transfer |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @var Model $product |
68
|
|
|
*/ |
69
|
4 |
|
return $this->transfers() |
70
|
4 |
|
->where('to_type', $product->getMorphClass()) |
71
|
4 |
|
->where('to_id', $product->getKey()) |
72
|
4 |
|
->where('refund', 0) |
73
|
4 |
|
->orderBy('id', 'desc') |
74
|
4 |
|
->first(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Product $product |
79
|
|
|
* @param bool $force |
80
|
|
|
* @return bool |
81
|
|
|
* @throws |
82
|
|
|
*/ |
83
|
2 |
|
public function refund(Product $product, bool $force = false): bool |
84
|
|
|
{ |
85
|
2 |
|
$transfer = $this->paid($product); |
86
|
|
|
|
87
|
2 |
|
if (!$transfer) { |
88
|
1 |
|
throw (new ModelNotFoundException()) |
89
|
1 |
|
->setModel($this->transfers()->getMorphClass()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return DB::transaction(function() use ($product, $transfer, $force) { |
93
|
2 |
|
if ($force) { |
94
|
1 |
|
$product->forceTransfer($this, $product->getAmountProduct(), $product->getMetaProduct()); |
|
|
|
|
95
|
|
|
} else { |
96
|
2 |
|
$product->transfer($this, $product->getAmountProduct(), $product->getMetaProduct()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return $transfer->update(['refund' => 1]); |
100
|
2 |
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Product $product |
105
|
|
|
* @param bool $force |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
2 |
|
public function safeRefund(Product $product, bool $force = false): bool |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
2 |
|
return $this->refund($product, $force); |
112
|
2 |
|
} catch (\Throwable $throwable) { |
113
|
2 |
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Product $product |
119
|
|
|
* @return bool |
120
|
|
|
* @throws |
121
|
|
|
*/ |
122
|
1 |
|
public function forceRefund(Product $product): bool |
123
|
|
|
{ |
124
|
1 |
|
return $this->refund($product, true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|