$this->getGateway()->key(), 'ref_id' => $this->getRefId(), 'gateway_ref_id' => $this->getGatewayRefId(), 'total' => $this->getTotal(), 'payable' => $this->getPayable(), 'currency' => $this->getCurrency(), 'pricing' => $this->getPricing(), 'coupon' => $this->getCoupon(), ]); } /** * @param string $JSON * @return self */ public function byJson(string $JSON): self { // Object $object = new stdClass(); // Data $data = json_decode(stripslashes($JSON), true); // Not Valid Array if (Arr::not($data)) return $this; // Populate Object foreach ($data as $key => $value) $object->{$key} = $value; return $this->byObject($object); } /** * @param stdClass $data * @return self */ private function byObject(stdClass $data): self { // Set Parameters foreach ($data as $key => $value) { if ($key === 'gateway') { // Get Gateway $value = is_string($value) ? \DayTidy\Payment\Gateway::get($value) : $value; $this->setGateway($value); } else if ($key === 'ref_id') $this->setRefId($value); else if ($key === 'gateway_ref_id') $this->setGatewayRefId($value); else if ($key === 'total') $this->setTotal($value); else if ($key === 'payable') $this->setPayable($value); else if ($key === 'currency') $this->setCurrency($value); else if ($key === 'pricing') $this->setPricing($value); else if ($key === 'coupon') $this->setCoupon($value); } return $this; } public function getGateway(): Gateway { return $this->gateway; } public function hasGateway(): bool { return isset($this->gateway); } /** * @param Gateway $gateway * @return Transaction */ public function setGateway(Gateway $gateway): self { $this->gateway = $gateway; return $this; } public function getRefId(): string { return $this->refId; } /** * @param string $refId * @return Transaction */ public function setRefId(string $refId): self { $this->refId = $refId; return $this; } /** * @return string[] */ public function getCoupon(): array { return $this->coupon; } /** * @param mixed[] $coupon * @return Transaction */ public function setCoupon(array $coupon): self { $this->coupon = $coupon; return $this; } public function getCouponCodes(): string { $data = $this->getCoupon(); $coupons = $data['codes'] ?? []; return count($coupons) ? implode(',', $coupons) : ''; } public function getTotal(): float { return $this->total; } /** * @param float $total * @return Transaction */ public function setTotal(float $total): self { $this->total = Price::normalize($total); return $this; } /** * @param float $payable * @return Transaction */ public function setPayable(float $payable): self { $this->payable = Price::normalize($payable); return $this; } public function getPayable(): float { return $this->payable; } public function getCurrency(): string { return $this->currency; } public function setCurrency(string $currency): self { $this->currency = strtoupper($currency); return $this; } /** * @return mixed[] */ public function getPricing(): array { return $this->pricing; } /** * @param mixed[] $pricing * @return Transaction */ public function setPricing(array $pricing): self { array_walk_recursive($pricing, function (&$value): void { if (is_numeric($value)) $value = Price::normalize((float) $value); }); $this->pricing = $pricing; return $this; } public function getGatewayRefId(): string { return $this->gatewayRefId; } /** * @param string $gatewayRefId * @return Transaction */ public function setGatewayRefId(string $gatewayRefId): self { $this->gatewayRefId = $gatewayRefId; return $this; } }