PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / CustomPayment / PaymentItem.php

PaymentItem.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Services/CustomPayment/PaymentItem.php

141 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\CustomPayment;
4
5 use ReflectionClass;
6 use ReflectionProperty;
7
8 class PaymentItem
9 {
10 protected string $itemName;
11 protected int $lineTotal;
12 protected int $price = 0;
13 protected int $quantity = 1;
14 protected string $paymentType = 'onetime';
15 protected array $subscriptionInfo = [];
16
17 public function setItemName(string $productName): self
18 {
19 if (empty($productName)) {
20 throw new \InvalidArgumentException(esc_html__("Item name cannot be empty.", 'fluent-cart'));
21 }
22 $this->itemName = $productName;
23 return $this;
24 }
25
26 public function setQuantity(int $quantity): self
27 {
28 if ($quantity <= 0) {
29 throw new \InvalidArgumentException(esc_html__("Quantity must be greater than 0.", 'fluent-cart'));
30 }
31 $this->quantity = $quantity;
32 $this->updateLineTotal();
33 return $this;
34 }
35
36 public function setPrice(int $price): self
37 {
38 if ($price <= 0) {
39 throw new \InvalidArgumentException(esc_html__("Price must be greater than 0 and use in cent!.", 'fluent-cart'));
40 }
41 $this->price = $price;
42 $this->updateLineTotal();
43 return $this;
44 }
45
46 public function setPaymentType(string $paymentType): self
47 {
48 $validPaymentTypes = ['onetime', 'subscription'];
49 if (!in_array($paymentType, $validPaymentTypes, true)) {
50 throw new \InvalidArgumentException(esc_html__("Invalid payment type provided.", 'fluent-cart'));
51 }
52 $this->paymentType = $paymentType;
53 return $this;
54 }
55
56 public function setSubscriptionInfo(array $subscriptionInfo): self
57 {
58 if ($this->paymentType === 'onetime') {
59 throw new \InvalidArgumentException(esc_html__("Please set payment type subscription first!", 'fluent-cart'));
60 }
61
62 $requiredKeys = ['signup_fee', 'times', 'repeat_interval'];
63 foreach ($requiredKeys as $key) {
64 if (!array_key_exists($key, $subscriptionInfo)) {
65 throw new \InvalidArgumentException(
66 sprintf(
67 /* translators: %s is the required key name */
68 esc_html__('Missing required subscription info key: %s.', 'fluent-cart'),
69 esc_html($key)
70 )
71 );
72 }
73 }
74
75 if (!is_numeric($subscriptionInfo['signup_fee']) || $subscriptionInfo['signup_fee'] < 0) {
76 throw new \InvalidArgumentException(esc_html__("Invalid signup fee. It must be a non-negative number.", 'fluent-cart'));
77 }
78
79 if (!is_int($subscriptionInfo['times']) || $subscriptionInfo['times'] < 0) {
80 throw new \InvalidArgumentException(esc_html__("Invalid times. It must be a non-negative integer.", 'fluent-cart'));
81 }
82
83 if (!is_string($subscriptionInfo['repeat_interval']) || empty($subscriptionInfo['repeat_interval'])) {
84 throw new \InvalidArgumentException(esc_html__("Invalid repeat interval. It must be a non-empty string ie. daily, monthly, yearly..)", 'fluent-cart'));
85 }
86
87 $this->subscriptionInfo = $subscriptionInfo;
88 return $this;
89 }
90
91 public function getItem(): array
92 {
93 if (empty($this->itemName)) {
94 throw new \RuntimeException(esc_html__("Item name is not set.", 'fluent-cart'));
95 }
96
97 if (empty($this->price)) {
98 throw new \RuntimeException(esc_html__("price is not set.", 'fluent-cart'));
99 }
100
101 if ($this->paymentType === 'subscription' && empty($this->subscriptionInfo)) {
102 throw new \InvalidArgumentException(esc_html__("Please set subscription required infos ['signup_fee', 'times', 'repeat_interval'].", 'fluent-cart') );
103 }
104
105 $otherInfo = array_merge(
106 [
107 "payment_type" => $this->paymentType,
108 ],
109 $this->subscriptionInfo
110 );
111
112 $this->checkoutItem = [
113 "id" => null,
114 "quantity" => $this->quantity,
115 "title" => $this->itemName,
116 "price" => $this->price,
117 "line_total" => $this->lineTotal,
118 "other_info" => $otherInfo
119 ];
120
121 return $this->checkoutItem;
122 }
123
124 private function updateLineTotal(): void
125 {
126 $this->lineTotal = (int)($this->quantity * $this->price);
127 }
128
129 public function toArray() : array
130 {
131 return [
132 'item_name' => $this->itemName,
133 'line_total' => $this->lineTotal,
134 'price' => $this->price,
135 'quantity' => $this->quantity,
136 'payment_type' => $this->paymentType,
137 'subscription_info' => $this->subscriptionInfo,
138 ];
139 }
140 }
141