attributes['datetime']); } public function user() { return User::where('id', $this->attributes['userid'])->first(); } public function shop() { return Shop::where("id", $this->attributes['shopid'])->first(); } public function coupon_code() { $metadata = $this->purchase_metadata(); if ($metadata !== null) { return (string) ($metadata['coupon_code'] ?? ''); } return (string) $this->attributes['coupon']; } public function is_refund() { return (float) $this->attributes['price'] < 0; } public function settlement_amount() { return number_format(abs((float) $this->attributes['price']), 2, '.', ''); } private function purchase_metadata() { $metadata = json_decode((string) $this->attributes['coupon'], true); if (!is_array($metadata) || ($metadata['_format'] ?? '') !== self::PURCHASE_META_FORMAT) { return null; } return $metadata; } }