From 76243f0883c72208c03fd22e89d89eae015d6b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurice=20Preu=C3=9F?= Date: Thu, 29 Sep 2022 14:46:17 +0200 Subject: [PATCH] Create Subscriptions.php --- .../BitinflowPaymentsWallet/Subscriptions.php | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/Accounts/Traits/BitinflowPaymentsWallet/Subscriptions.php diff --git a/src/Accounts/Traits/BitinflowPaymentsWallet/Subscriptions.php b/src/Accounts/Traits/BitinflowPaymentsWallet/Subscriptions.php new file mode 100644 index 0000000..0418c80 --- /dev/null +++ b/src/Accounts/Traits/BitinflowPaymentsWallet/Subscriptions.php @@ -0,0 +1,107 @@ +asPaymentsUser('GET', 'subscriptions'); + } + + /** + * @param string $id + * @return object|null + */ + public function get(string $id): ?object + { + return $this->asPaymentsUser('GET', sprintf('subscriptions/%s', $id)); + } + + /** + * Create a new subscription. + * + * @param array $attributes array which requires following attributes: + * name, description, period, price + * and following attributes are optional: + * vat, payload, ends_at, webhook_url, webhook_secret + * @param array $payload optional data that is stored in the subscription + * @param bool $checkout optional checkout it directly + * @return object|false the subscription object + * @throws GuzzleException + */ + public function create(string $name, array $attributes, array $payload = [], bool $checkout = false): object|false + { + $defaults = ['period' => 'monthly']; + $attributes = array_merge(array_merge($defaults, $attributes), [ + 'payload' => array_merge($payload, [ + 'name' => $name, + ]), + 'checkout' => $checkout + ]); + + return $this->asPaymentsUser('POST', 'subscriptions', $attributes)->data; + } + + /** + * @param string $name + * @return bool + */ + public function has(string $name = 'default'): bool + { + $subscription = $this->getSubscription($name); + + return $subscription && $subscription->status === 'settled' || $subscription && $subscription->resumeable; + } + + /** + * Checkout given subscription. + * + * @param string $id + * @return bool + */ + public function checkout(string $id): bool + { + $this->asPaymentsUser('PUT', sprintf('subscriptions/%s/checkout', $id)); + + return true; + } + + /** + * Revoke a running subscription. + * + * @param string $id + * @return bool + */ + public function revoke(string $id): bool + { + $this->asPaymentsUser('PUT', sprintf('subscriptions/%s/revoke', $id)); + + return true; + } + + /** + * Resume a running subscription. + * + * @param string $id + * @return bool + */ + public function resume(string $id): bool + { + $this->asPaymentsUser('PUT', sprintf('subscriptions/%s/resume', $id)); + + return true; + } +}