Skip to content

Commit 4bc640e

Browse files
committed
#19 add CanPay/CanPayFloat
1 parent 6baa318 commit 4bc640e

File tree

7 files changed

+249
-214
lines changed

7 files changed

+249
-214
lines changed

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Add trait `CanPay`.
10+
- Add trait `CanPayFloat`.
11+
12+
### Deprecated
13+
- Trait `CanBePaid`.
14+
- Trait `CanBePaidFloat`.
815

916
## [2.1.0] - 2018-11-22
1017
### Added

src/Models/Wallet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Bavix\Wallet\Interfaces\Customer;
66
use Bavix\Wallet\Interfaces\WalletFloat;
7-
use Bavix\Wallet\Traits\CanBePaidFloat;
7+
use Bavix\Wallet\Traits\CanPayFloat;
88
use Bavix\Wallet\Traits\HasGift;
99
use Bavix\Wallet\WalletProxy;
1010
use Illuminate\Database\Eloquent\Model;
@@ -21,7 +21,7 @@
2121
class Wallet extends Model implements Customer, WalletFloat
2222
{
2323

24-
use CanBePaidFloat;
24+
use CanPayFloat;
2525
use HasGift;
2626

2727
/**

src/Traits/CanBePaid.php

Lines changed: 7 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -2,197 +2,13 @@
22

33
namespace Bavix\Wallet\Traits;
44

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-
5+
/**
6+
* Trait CanBePaid
7+
* @package Bavix\Wallet\Traits
8+
* @deprecated use trait CanPay
9+
* @see https://github.com/bavix/laravel-wallet/issues/19
10+
*/
1211
trait CanBePaid
1312
{
14-
15-
use HasWallet;
16-
17-
/**
18-
* @param Product $product
19-
* @return Transfer
20-
*/
21-
public function payFree(Product $product): Transfer
22-
{
23-
if (!$product->canBuy($this)) {
24-
throw new ProductEnded(trans('wallet::errors.product_stock'));
25-
}
26-
27-
return $this->transfer($product, 0, $product->getMetaProduct());
28-
}
29-
30-
/**
31-
* @param Product $product
32-
* @param bool $force
33-
* @return Transfer
34-
* @throws
35-
*/
36-
public function pay(Product $product, bool $force = false): Transfer
37-
{
38-
if (!$product->canBuy($this, $force)) {
39-
throw new ProductEnded(trans('wallet::errors.product_stock'));
40-
}
41-
42-
if ($force) {
43-
return $this->forceTransfer($product, $product->getAmountProduct(), $product->getMetaProduct());
44-
}
45-
46-
return $this->transfer($product, $product->getAmountProduct(), $product->getMetaProduct());
47-
}
48-
49-
/**
50-
* @param Product $product
51-
* @param bool $force
52-
* @return Transfer|null
53-
*/
54-
public function safePay(Product $product, bool $force = false): ?Transfer
55-
{
56-
try {
57-
return $this->pay($product, $force);
58-
} catch (\Throwable $throwable) {
59-
return null;
60-
}
61-
}
62-
63-
/**
64-
* @param Product $product
65-
* @return Transfer
66-
* @throws
67-
*/
68-
public function forcePay(Product $product): Transfer
69-
{
70-
return $this->pay($product, true);
71-
}
72-
73-
/**
74-
* @param Product $product
75-
* @param bool $gifts
76-
* @return null|Transfer
77-
*/
78-
public function paid(Product $product, bool $gifts = false): ?Transfer
79-
{
80-
$status = [Transfer::STATUS_PAID];
81-
if ($gifts) {
82-
$status[] = Transfer::STATUS_GIFT;
83-
}
84-
85-
/**
86-
* @var Model $product
87-
* @var Transfer $query
88-
*/
89-
$query = $this->transfers();
90-
return $query
91-
->where('to_type', $product->getMorphClass())
92-
->where('to_id', $product->getKey())
93-
->whereIn('status', $status)
94-
->orderBy('id', 'desc')
95-
->first();
96-
}
97-
98-
/**
99-
* @param Product $product
100-
* @param bool $force
101-
* @param bool $gifts
102-
* @return bool
103-
* @throws
104-
*/
105-
public function refund(Product $product, bool $force = false, bool $gifts = false): bool
106-
{
107-
$transfer = $this->paid($product, $gifts);
108-
109-
if (!$transfer) {
110-
throw (new ModelNotFoundException())
111-
->setModel($this->transfers()->getMorphClass());
112-
}
113-
114-
return DB::transaction(function () use ($product, $transfer, $force) {
115-
$transfer->load('withdraw.payable');
116-
117-
if ($force) {
118-
$product->forceTransfer(
119-
$transfer->withdraw->payable,
120-
$transfer->deposit->amount,
121-
$product->getMetaProduct()
122-
);
123-
} else {
124-
$product->transfer(
125-
$transfer->withdraw->payable,
126-
$transfer->deposit->amount,
127-
$product->getMetaProduct()
128-
);
129-
}
130-
131-
return $transfer->update([
132-
'status' => Transfer::STATUS_REFUND,
133-
'status_last' => $transfer->status,
134-
]);
135-
});
136-
}
137-
138-
/**
139-
* @param Product $product
140-
* @param bool $force
141-
* @param bool $gifts
142-
* @return bool
143-
*/
144-
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool
145-
{
146-
try {
147-
return $this->refund($product, $force, $gifts);
148-
} catch (\Throwable $throwable) {
149-
return false;
150-
}
151-
}
152-
153-
/**
154-
* @param Product $product
155-
* @param bool $gifts
156-
* @return bool
157-
* @throws
158-
*/
159-
public function forceRefund(Product $product, bool $gifts = false): bool
160-
{
161-
return $this->refund($product, true, $gifts);
162-
}
163-
164-
/**
165-
* @param Product $product
166-
* @param bool $force
167-
* @return bool
168-
*/
169-
public function refundGift(Product $product, bool $force = false): bool
170-
{
171-
return $this->refund($product, $force, true);
172-
}
173-
174-
/**
175-
* @param Product $product
176-
* @param bool $force
177-
* @return bool
178-
*/
179-
public function safeRefundGift(Product $product, bool $force = false): bool
180-
{
181-
try {
182-
return $this->refundGift($product, $force);
183-
} catch (\Throwable $throwable) {
184-
return false;
185-
}
186-
}
187-
188-
/**
189-
* @param Product $product
190-
* @return bool
191-
* @throws
192-
*/
193-
public function forceRefundGift(Product $product): bool
194-
{
195-
return $this->refundGift($product, true);
196-
}
197-
13+
use CanPay;
19814
}

src/Traits/CanBePaidFloat.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@
22

33
namespace Bavix\Wallet\Traits;
44

5+
/**
6+
* Trait CanBePaidFloat
7+
* @package Bavix\Wallet\Traits
8+
* @deprecated use trait CanPayFloat
9+
* @see https://github.com/bavix/laravel-wallet/issues/19
10+
*/
511
trait CanBePaidFloat
612
{
7-
8-
use HasWalletFloat, CanBePaid {
9-
CanBePaid::checkAmount insteadof HasWalletFloat;
10-
CanBePaid::deposit insteadof HasWalletFloat;
11-
CanBePaid::withdraw insteadof HasWalletFloat;
12-
CanBePaid::canWithdraw insteadof HasWalletFloat;
13-
CanBePaid::forceWithdraw insteadof HasWalletFloat;
14-
CanBePaid::transfer insteadof HasWalletFloat;
15-
CanBePaid::safeTransfer insteadof HasWalletFloat;
16-
CanBePaid::forceTransfer insteadof HasWalletFloat;
17-
CanBePaid::assemble insteadof HasWalletFloat;
18-
CanBePaid::change insteadof HasWalletFloat;
19-
CanBePaid::transactions insteadof HasWalletFloat;
20-
CanBePaid::transfers insteadof HasWalletFloat;
21-
CanBePaid::wallet insteadof HasWalletFloat;
22-
CanBePaid::getBalanceAttribute insteadof HasWalletFloat;
23-
CanBePaid::addBalance insteadof HasWalletFloat;
24-
}
25-
13+
use CanPayFloat;
2614
}

0 commit comments

Comments
 (0)