|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (c) 2025 BitPay |
| 5 | + **/ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace BitPaySDK\Client; |
| 10 | + |
| 11 | +use BitPaySDK\Exceptions\BitPayApiException; |
| 12 | +use BitPaySDK\Exceptions\BitPayExceptionProvider; |
| 13 | +use BitPaySDK\Exceptions\BitPayGenericException; |
| 14 | +use BitPaySDK\Model\Subscription\Subscription; |
| 15 | +use BitPaySDK\Model\Facade; |
| 16 | +use BitPaySDK\Tokens; |
| 17 | +use BitPaySDK\Util\JsonMapperFactory; |
| 18 | +use BitPaySDK\Util\RESTcli\RESTcli; |
| 19 | +use Exception; |
| 20 | + |
| 21 | +/** |
| 22 | + * Handles interactions with the subscriptions endpoints. |
| 23 | + * |
| 24 | + * @package BitPaySDK\Client |
| 25 | + * @author BitPay Integrations <[email protected]> |
| 26 | + * @license http://www.opensource.org/licenses/mit-license.php MIT |
| 27 | + */ |
| 28 | +class SubscriptionClient |
| 29 | +{ |
| 30 | + private static ?self $instance = null; |
| 31 | + private Tokens $tokenCache; |
| 32 | + private RESTcli $restCli; |
| 33 | + |
| 34 | + private function __construct(Tokens $tokenCache, RESTcli $restCli) |
| 35 | + { |
| 36 | + $this->tokenCache = $tokenCache; |
| 37 | + $this->restCli = $restCli; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Factory method for Subscription Client. |
| 42 | + * |
| 43 | + * @param Tokens $tokenCache |
| 44 | + * @param RESTcli $restCli |
| 45 | + * @return static |
| 46 | + */ |
| 47 | + public static function getInstance(Tokens $tokenCache, RESTcli $restCli): self |
| 48 | + { |
| 49 | + if (!self::$instance) { |
| 50 | + self::$instance = new self($tokenCache, $restCli); |
| 51 | + } |
| 52 | + |
| 53 | + return self::$instance; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a BitPay Subscription. |
| 58 | + * |
| 59 | + * @param Subscription $subscription A Subscription object with request parameters defined. |
| 60 | + * @return Subscription Created Subscription object |
| 61 | + * @throws BitPayApiException |
| 62 | + * @throws BitPayGenericException |
| 63 | + */ |
| 64 | + public function create(Subscription $subscription): Subscription |
| 65 | + { |
| 66 | + $subscription->setToken($this->tokenCache->getTokenByFacade(Facade::MERCHANT)); |
| 67 | + |
| 68 | + $responseJson = $this->restCli->post("subscriptions", $subscription->toArray()); |
| 69 | + |
| 70 | + try { |
| 71 | + return $this->mapJsonToSubscriptionClass($responseJson); |
| 72 | + } catch (Exception $e) { |
| 73 | + BitPayExceptionProvider::throwDeserializeResourceException('Subscription', $e->getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Retrieve a BitPay subscription by its resource ID. |
| 79 | + * |
| 80 | + * @param $subscriptionId string The id of the subscription to retrieve. |
| 81 | + * @return Subscription Retrieved Subscription object |
| 82 | + * @throws BitPayApiException |
| 83 | + * @throws BitPayGenericException |
| 84 | + */ |
| 85 | + public function get(string $subscriptionId): Subscription |
| 86 | + { |
| 87 | + $params = []; |
| 88 | + $params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT); |
| 89 | + |
| 90 | + $responseJson = $this->restCli->get("subscriptions/" . $subscriptionId, $params); |
| 91 | + |
| 92 | + try { |
| 93 | + return $this->mapJsonToSubscriptionClass($responseJson); |
| 94 | + } catch (Exception $e) { |
| 95 | + BitPayExceptionProvider::throwDeserializeResourceException('Subscription', $e->getMessage()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Retrieve a collection of BitPay subscriptions. |
| 101 | + * |
| 102 | + * @param string|null $status The status to filter the subscriptions. |
| 103 | + * @return Subscription[] Filtered list of Subscription objects |
| 104 | + * @throws BitPayApiException |
| 105 | + * @throws BitPayGenericException |
| 106 | + */ |
| 107 | + public function getSubscriptions(?string $status = null): array |
| 108 | + { |
| 109 | + $params = []; |
| 110 | + $params["token"] = $this->tokenCache->getTokenByFacade(Facade::MERCHANT); |
| 111 | + if ($status) { |
| 112 | + $params["status"] = $status; |
| 113 | + } |
| 114 | + |
| 115 | + $responseJson = $this->restCli->get("subscriptions", $params); |
| 116 | + |
| 117 | + try { |
| 118 | + $mapper = JsonMapperFactory::create(); |
| 119 | + return $mapper->mapArray( |
| 120 | + json_decode($responseJson, true, 512, JSON_THROW_ON_ERROR), |
| 121 | + [], |
| 122 | + Subscription::class |
| 123 | + ); |
| 124 | + } catch (Exception $e) { |
| 125 | + BitPayExceptionProvider::throwDeserializeResourceException('Subscription', $e->getMessage()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Update a BitPay Subscription. |
| 131 | + * |
| 132 | + * @param Subscription $subscription A Subscription object with the parameters to update defined. |
| 133 | + * @param string $subscriptionId The ID of the Subscription to update. |
| 134 | + * @return Subscription Updated Subscription object |
| 135 | + * @throws BitPayApiException |
| 136 | + * @throws BitPayGenericException |
| 137 | + */ |
| 138 | + public function update(Subscription $subscription, string $subscriptionId): Subscription |
| 139 | + { |
| 140 | + $subscriptionToken = $this->get($subscription->getId())->getToken(); |
| 141 | + $subscription->setToken($subscriptionToken); |
| 142 | + |
| 143 | + $responseJson = $this->restCli->update("subscriptions/" . $subscriptionId, $subscription->toArray()); |
| 144 | + |
| 145 | + try { |
| 146 | + $mapper = JsonMapperFactory::create(); |
| 147 | + |
| 148 | + return $mapper->map( |
| 149 | + json_decode($responseJson, true, 512, JSON_THROW_ON_ERROR), |
| 150 | + $subscription |
| 151 | + ); |
| 152 | + } catch (Exception $e) { |
| 153 | + BitPayExceptionProvider::throwDeserializeResourceException('Subscription', $e->getMessage()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param string|null $responseJson |
| 159 | + * @return Subscription |
| 160 | + * @throws \JsonException |
| 161 | + * @throws \JsonMapper_Exception |
| 162 | + */ |
| 163 | + private function mapJsonToSubscriptionClass(?string $responseJson): Subscription |
| 164 | + { |
| 165 | + $mapper = JsonMapperFactory::create(); |
| 166 | + |
| 167 | + return $mapper->map( |
| 168 | + json_decode($responseJson, true, 512, JSON_THROW_ON_ERROR), |
| 169 | + new Subscription() |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments