diff --git a/README.md b/README.md index 56103f6..942bf3e 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,16 @@ public function isEmailExisting(string $email): Result ## Billable +### ManagesAddresses + +```php +public function addresses(): Result +public function createAddress(array $attributes = []): Result +public function address(string $addressId): Result +public function updateAddress(string $addressId, array $attributes = []): Result +public function deleteAddress(string $addressId): Result +``` + ### ManagesBalance ```php @@ -316,7 +326,7 @@ public function charge(float $amount, string $paymentMethodId, array $options = ```php public function invoices(): Result public function invoice(string $invoiceId): Result -public function getInvoiceDownloadUrl(string $invoiceId): string +public function getInvoiceTemporaryUrl(string $invoiceId): string ``` ### ManagesOrders @@ -356,6 +366,7 @@ public function createSubscription(array $attributes): Result public function checkoutSubscription(string $subscriptionId): Result public function revokeSubscription(string $subscriptionId): Result public function resumeSubscription(string $subscriptionId): Result +public function deleteSubscription(string $subscriptionId): Result ``` ### ManagesTaxation diff --git a/src/Id/Billable.php b/src/Id/Billable.php index 52e4b03..626cbb0 100644 --- a/src/Id/Billable.php +++ b/src/Id/Billable.php @@ -3,6 +3,7 @@ namespace Anikeen\Id; use Anikeen\Id\ApiOperations\Request; +use Anikeen\Id\Concerns\ManagesAddresses; use Anikeen\Id\Concerns\ManagesBalance; use Anikeen\Id\Concerns\ManagesInvoices; use Anikeen\Id\Concerns\ManagesOrders; @@ -17,6 +18,7 @@ use stdClass; trait Billable { + use ManagesAddresses; use ManagesBalance; use ManagesInvoices; use ManagesOrders; diff --git a/src/Id/Concerns/ManagesAddresses.php b/src/Id/Concerns/ManagesAddresses.php new file mode 100644 index 0000000..2cc3198 --- /dev/null +++ b/src/Id/Concerns/ManagesAddresses.php @@ -0,0 +1,132 @@ +request('GET', 'v1/addresses'); + } + + /** + * Creates a new address for the current user. + * + * @param array{ + * company_name: null|string, + * first_name: string, + * last_name: string, + * address: string, + * address_2: null|string, + * house_number: null|string, + * postal_code: string, + * city: string, + * state: null|string, + * country_iso: string, + * phone_number: null|string, + * email: null|string, + * primary: bool, + * primary_billing_address: bool + * } $attributes The address data: + *   - company_name: Company name (optional) + *   - first_name: First name + *   - last_name: Last name + *   - address: Address line 1 (e.g. street address, P.O. Box, etc.) + *   - address_2: Address line 2 (optional, e.g. apartment number, c/o, etc.) + *   - house_number: House number (optional) + *   - postal_code: Postal code + *   - city: City + *   - state: State (optional, e.g. province, region, etc.) + *   - country_iso: Country ISO code (e.g. US, CA, etc.) + *   - phone_number: Phone number (optional) + *   - email: Email address (optional, e.g. for delivery notifications) + *   - primary: Whether this address should be the primary address (optional) + *   - primary_billing_address: Whether this address should be the primary billing address (optional) + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function createAddress(array $attributes = []): Result + { + return $this->request('POST', 'v1/addresses', $attributes); + } + + /** + * Get given address from the current user. + * + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function address(string $addressId): Result + { + return $this->request('GET', sprintf('v1/addresses/%s', $addressId)); + } + + /** + * Update given address from the current user. + * + * VAT is calculated based on the billing address and shown in the address response. + * + * @param string $addressId The address ID. + * @param array{ + * company_name: null|string, + * first_name: string, + * last_name: string, + * address_2: null|string, + * address: string, + * house_number: null|string, + * postal_code: string, + * city: string, + * state: null|string, + * country_iso: string, + * phone_number: null|string, + * email: null|string, + * primary: bool, + * primary_billing_address: bool + * } $attributes The address data: + * - company_name: Company name (optional) + * - first_name: First name (required when set) + * - last_name: Last name (required when set) + * - address: Address line 1 (e.g. street address, P.O. Box, etc.) + * - address_2: Address line 2 (optional, e.g. apartment number, c/o, etc.) + * - house_number: House number (optional) + * - postal_code: Postal code (required when set) + * - city: City (required when set) + * - state: State (optional, e.g. province, region, etc.) + * - country_iso: Country ISO code (required when set, e.g. US, CA, etc.) + * - phone_number: Phone number (optional) + * - email: Email address (optional, e.g. for delivery notifications) + * - primary: Whether this address should be the primary address (optional) + * - primary_billing_address: Whether this address should be the primary billing address (optional) + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function updateAddress(string $addressId, array $attributes = []): Result + { + return $this->request('PUT', sprintf('v1/addresses/%s', $addressId), $attributes); + } + + /** + * Delete given address from the current user. + * + * @param string $addressId The address ID. + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function deleteAddress(string $addressId): Result + { + return $this->request('DELETE', sprintf('v1/addresses/%s', $addressId)); + } +} \ No newline at end of file diff --git a/src/Id/Concerns/ManagesInvoices.php b/src/Id/Concerns/ManagesInvoices.php index f69baf3..c4ff84c 100644 --- a/src/Id/Concerns/ManagesInvoices.php +++ b/src/Id/Concerns/ManagesInvoices.php @@ -35,14 +35,14 @@ trait ManagesInvoices } /** - * Get download url from given invoice. + * Get temporary download url from given invoice. * * @param string $invoiceId The invoice ID * @throws RequestRequiresClientIdException * @throws GuzzleException */ - public function getInvoiceDownloadUrl(string $invoiceId): string + public function getInvoiceTemporaryUrl(string $invoiceId): string { - return $this->request('PUT', sprintf('v1/invoices/%s', $invoiceId))->data->download_url; + return $this->request('PUT', sprintf('v1/invoices/%s', $invoiceId))->data->temporary_url; } } \ No newline at end of file diff --git a/src/Id/Concerns/ManagesOrders.php b/src/Id/Concerns/ManagesOrders.php index 97cd516..e5ae30e 100644 --- a/src/Id/Concerns/ManagesOrders.php +++ b/src/Id/Concerns/ManagesOrders.php @@ -27,6 +27,11 @@ trait ManagesOrders * * VAT is calculated based on the billing address and shown in the order response. * + * The billing and shipping addresses are each persisted as standalone Address entities + * in the database, but are also embedded (deep-copied) into the Order object itself + * rather than merely referenced. This guarantees that the order retains its own snapshot + * of both addresses for future reference. + * * @param array{ * billing_address: array{ * company_name: null|string, @@ -92,6 +97,11 @@ trait ManagesOrders * * VAT is calculated based on the billing address and shown in the order response. * + * The billing and shipping addresses are each persisted as standalone Address entities + * in the database, but are also embedded (deep-copied) into the Order object itself + * rather than merely referenced. This guarantees that the order retains its own snapshot + * of both addresses for future reference. + * * @param string $orderId The order ID. * @param array{ * billing_address: array{ diff --git a/src/Id/Concerns/ManagesPaymentMethods.php b/src/Id/Concerns/ManagesPaymentMethods.php index 5775758..67a5146 100644 --- a/src/Id/Concerns/ManagesPaymentMethods.php +++ b/src/Id/Concerns/ManagesPaymentMethods.php @@ -41,7 +41,7 @@ trait ManagesPaymentMethods */ public function hasDefaultPaymentMethod(): bool { - return $this->defaultPaymentMethod()->count() > 0; + return (bool)$this->defaultPaymentMethod()->data; } /** @@ -65,7 +65,7 @@ trait ManagesPaymentMethods */ public function billingPortalUrl(string $returnUrl, array $options): string { - return $this->request('POST', 'v1/stripe/billing-portal', [ + return $this->request('POST', 'v1/billing/portal', [ 'return_url' => $returnUrl, 'options' => $options, ])->data->url; diff --git a/src/Id/Concerns/ManagesSubscriptions.php b/src/Id/Concerns/ManagesSubscriptions.php index 4866cc6..ad79498 100644 --- a/src/Id/Concerns/ManagesSubscriptions.php +++ b/src/Id/Concerns/ManagesSubscriptions.php @@ -100,4 +100,16 @@ trait ManagesSubscriptions { return $this->request('PUT', sprintf('v1/subscriptions/%s/resume', $subscriptionId)); } + + /** + * Delete a given subscription from the current user. + * + * @param string $subscriptionId The subscription ID. + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function deleteSubscription(string $subscriptionId): Result + { + return $this->request('DELETE', sprintf('v1/subscriptions/%s', $subscriptionId)); + } } \ No newline at end of file