1
|
|
|
<?php |
2
|
|
|
namespace EventEspresso\core\services\commands\transaction; |
3
|
|
|
|
4
|
|
|
use EE_Checkout; |
5
|
|
|
use EE_Error; |
6
|
|
|
use EE_Line_Item; |
7
|
|
|
use EE_Transaction; |
8
|
|
|
use EEH_Line_Item; |
9
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
10
|
|
|
use EventEspresso\core\services\commands\CommandHandler; |
11
|
|
|
use EventEspresso\core\services\commands\CommandInterface; |
12
|
|
|
|
13
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CreateTransactionCommandHandler |
19
|
|
|
* generates and validates a Transaction and it's associated top-level Line Items |
20
|
|
|
* |
21
|
|
|
* @package Event Espresso |
22
|
|
|
* @author Brent Christensen |
23
|
|
|
* @since $VID:$ |
24
|
|
|
*/ |
25
|
|
|
class CreateTransactionCommandHandler extends CommandHandler |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param CommandInterface $command |
31
|
|
|
* @return mixed |
32
|
|
|
* @throws EE_Error |
33
|
|
|
* @throws InvalidEntityException |
34
|
|
|
*/ |
35
|
|
|
public function handle(CommandInterface $command) |
36
|
|
|
{ |
37
|
|
|
/** @var CreateTransactionCommand $command */ |
38
|
|
|
if (! $command instanceof CreateTransactionCommand) { |
39
|
|
|
throw new InvalidEntityException(get_class($command), 'CreateTransactionCommand'); |
40
|
|
|
} |
41
|
|
|
$transaction_details = $command->transactionDetails(); |
42
|
|
|
$cart_total = null; |
43
|
|
|
if ($command->checkout() instanceof EE_Checkout) { |
44
|
|
|
// ensure cart totals have been calculated |
45
|
|
|
$command->checkout()->cart->get_grand_total()->recalculate_total_including_taxes(); |
46
|
|
|
// grab the cart grand total |
47
|
|
|
$cart_total = $command->checkout()->cart->get_cart_grand_total(); |
48
|
|
|
$transaction_details['TXN_reg_steps'] = $command->checkout()->initialize_txn_reg_steps_array(); |
49
|
|
|
$transaction_details['TXN_total'] = $cart_total > 0 ? $cart_total : 0; |
50
|
|
|
} |
51
|
|
|
// create new TXN and save it so it has an ID |
52
|
|
|
$transaction = EE_Transaction::new_instance($transaction_details); |
53
|
|
|
if (! $transaction instanceof EE_Transaction) { |
54
|
|
|
throw new InvalidEntityException(get_class($transaction), 'EE_Transaction'); |
55
|
|
|
} |
56
|
|
|
$transaction->save(); |
57
|
|
|
// ensure grand total line item created |
58
|
|
|
$cart_total = $cart_total instanceof EE_Line_Item |
59
|
|
|
? $cart_total |
60
|
|
|
: EEH_Line_Item::create_total_line_item($transaction); |
61
|
|
|
if (! $cart_total instanceof EE_Line_Item) { |
62
|
|
|
throw new InvalidEntityException(get_class($cart_total), 'EE_Line_Item'); |
63
|
|
|
} |
64
|
|
|
$cart_total->save_this_and_descendants_to_txn($transaction->ID()); |
65
|
|
|
return $transaction; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
// End of file CreateTransactionCommandHandler.php |
71
|
|
|
// Location: EventEspresso\core\services\commands\transaction/CreateTransactionCommandHandler.php |