diff --git a/doc/get-pay-urls-for-existing-payment.md b/doc/get-pay-urls-for-existing-payment.md new file mode 100644 index 0000000..b8b86a6 --- /dev/null +++ b/doc/get-pay-urls-for-existing-payment.md @@ -0,0 +1,22 @@ +# Get pay URLs for existing payments + +Returns an array of available payment methods with pay URLs for certain payment. + +```php +/** @var \ThePay\ApiClient\TheClient $client */ +$paymentMethod = $client->getPaymentUrlsForPayment('uid-454548', 'cs'); +``` + +### Preformatted buttons + +Method **getPaymentButtonsForPayment** returns HTML code. + +```php + // used default rendering + $paymentButtons = $client->getPaymentButtonsForPayment($paymentUid); +``` + +Payment method buttons should look like this, second image is with hover. + +![default](img/payment_method_button.png) +![hover](img/payment_method_button_hover.png) \ No newline at end of file diff --git a/doc/index.md b/doc/index.md index f5447a8..915920d 100644 --- a/doc/index.md +++ b/doc/index.md @@ -11,6 +11,7 @@ | getPayments | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/get-payments | | getPaymentButtons | | | getPaymentButton | | +| getPaymentButtonsForPayment | | | createPayment | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/create-new-payment | | realizePreauthorizedPayment | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/realize-preauthorized-payment | | cancelPreauthorizedPayment | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/cancel-preauthorized-payment | @@ -21,6 +22,7 @@ | realizeIrregularSubscriptionPayment | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/realize-irregular-subscription-payment | | realizeUsageBasedSubscriptionPayment | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/realize-usage-based-subscription-payment | | realizePaymentBySavedAuthorization | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/realize-payment-by-saved-authorization | +| getPaymentUrlsForPayment | https://dataapi21.docs.apiary.io/#reference/payment-management/general-payment-management/get-available-payment-methods | ## Usage examples @@ -53,6 +55,8 @@ [Saving authorization](saving-authorization.md) +[Get pay URLs for existing payments](get-pay-urls-for-existing-payment.md) + ## Methods ### getProjects @@ -112,6 +116,18 @@ Returns HTML markup with list of payment buttons. Returns HTML markup with "Pay!" button. +### getPaymentButtonsForPayment + +Returns HTML markup with list of payment buttons for already existing payment. + +#### Parameters + +| name | type | | desc | +| --- | --- | --- | --- | +| $uid | string | required | Payment's UID | +| $languageCode | string | optional | Language code in ISO 6391 (2 chars) format | +| $useInlineAssets | bool | optional | will generate basic css & js | + #### Parameters | name | type | | desc | @@ -233,3 +249,14 @@ Create new payment using saved authorization. | --- | --- | --- | --- | | $uid | string | required | UID of parent payment | | $params | RealizePaymentBySavedAuthorizationParams | required | | + +### getPaymentUrlsForPayment + +Returns an array of available payment methods with pay URLs for certain payment. + +#### Parameters + +| name | type | | description | +| --- | --- | --- | --- | +| $uid | string | required | Payment's UID | +| $languageCode | string | optional | Language code in ISO 6391 (2 chars) format | diff --git a/src/Model/IPaymentMethod.php b/src/Model/IPaymentMethod.php new file mode 100644 index 0000000..9c77719 --- /dev/null +++ b/src/Model/IPaymentMethod.php @@ -0,0 +1,28 @@ + + */ + public function getTags(); +} diff --git a/src/Model/PaymentMethod.php b/src/Model/PaymentMethod.php index 36a6d06..6c25f5a 100644 --- a/src/Model/PaymentMethod.php +++ b/src/Model/PaymentMethod.php @@ -5,7 +5,7 @@ use ThePay\ApiClient\Utils\Json; use ThePay\ApiClient\ValueObject\Url; -class PaymentMethod +class PaymentMethod implements IPaymentMethod { /** * @deprecated use ThePay\ApiClient\ValueObject\PaymentMethodTag::CARD diff --git a/src/Model/PaymentMethodWithPayUrl.php b/src/Model/PaymentMethodWithPayUrl.php new file mode 100644 index 0000000..380e094 --- /dev/null +++ b/src/Model/PaymentMethodWithPayUrl.php @@ -0,0 +1,84 @@ + */ + private $tags; + + /** @var Url|null */ + private $imageUrl; + + /** @var Url */ + private $payUrl; + + /** + * @param string|array $values Json in string or associative array + */ + public function __construct($values) + { + $data = is_array($values) ? $values : Json::decode($values, true); + + $this->code = $data['code']; + $this->title = $data['title']; + $this->imageUrl = new Url($data['image']['src']); + $this->tags = $data['tags']; + $this->payUrl = new Url($data['url']); + } + + /** @return string */ + public function getCode() + { + return $this->code; + } + + /** @return string */ + public function getTitle() + { + return $this->title; + } + + /** @return Url */ + public function getImageUrl() + { + return $this->imageUrl; + } + + /** @return Url */ + public function getPayUrl() + { + return $this->payUrl; + } + + /** + * @return array + */ + public function getTags() + { + return $this->tags; + } + + /** + * @return array + */ + public function toArray() + { + return array( + 'code' => $this->code, + 'title' => $this->title, + 'tags' => $this->tags, + 'imageUrl' => (string) $this->imageUrl, + 'payUrl' => (string) $this->payUrl, + ); + } +} diff --git a/src/Service/ApiService.php b/src/Service/ApiService.php index 8f96fed..11d6fce 100644 --- a/src/Service/ApiService.php +++ b/src/Service/ApiService.php @@ -15,6 +15,7 @@ use ThePay\ApiClient\Model\CreatePaymentResponse; use ThePay\ApiClient\Model\PaginatedCollectionParams; use ThePay\ApiClient\Model\Payment; +use ThePay\ApiClient\Model\PaymentMethodWithPayUrl; use ThePay\ApiClient\Model\PaymentRefund; use ThePay\ApiClient\Model\PaymentRefundInfo; use ThePay\ApiClient\Model\Project; @@ -95,7 +96,7 @@ public function getActivePaymentMethods(LanguageCode $languageCode = null) $arguments['language'] = $languageCode->getValue(); } - $url = $this->url(array('methods')); + $url = $this->url(array('methods'), $arguments); $response = $this ->httpService ->get($url); @@ -420,6 +421,36 @@ public function createPaymentRefund(Identifier $uid, Amount $amount, StringValue } } + /** + * Returns an array of available payment methods with pay URLs for certain payment. + * + * @return array + */ + public function getPaymentUrlsForPayment(Identifier $uid, LanguageCode $languageCode = null) + { + $arguments = array(); + if ($languageCode) { + $arguments['language'] = $languageCode->getValue(); + } + + $url = $this->url(array('payments', $uid->getValue(), 'payment_urls'), $arguments); + $response = $this->httpService->get($url); + + if ($response->getCode() !== 200) { + throw $this->buildException($url, $response); + } + + $responseData = Json::decode($response->getBody(), true); + + $paymentMethods = array(); + foreach ($responseData as $paymentMethod) { + $paymentMethods[] = new PaymentMethodWithPayUrl($paymentMethod); + } + + return $paymentMethods; + } + + /** * Build URL for API requests diff --git a/src/Service/ApiServiceInterface.php b/src/Service/ApiServiceInterface.php index c11cfa7..e41a1f3 100644 --- a/src/Service/ApiServiceInterface.php +++ b/src/Service/ApiServiceInterface.php @@ -13,6 +13,7 @@ use ThePay\ApiClient\Model\CreatePaymentParams; use ThePay\ApiClient\Model\CreatePaymentResponse; use ThePay\ApiClient\Model\Payment; +use ThePay\ApiClient\Model\PaymentMethodWithPayUrl; use ThePay\ApiClient\Model\PaymentRefundInfo; use ThePay\ApiClient\Model\Project; use ThePay\ApiClient\Model\RealizeIrregularSubscriptionPaymentParams; @@ -153,4 +154,11 @@ public function getPaymentRefund(Identifier $uid); * @return void */ public function createPaymentRefund(Identifier $uid, Amount $amount, StringValue $reason); + + /** + * Returns an array of available payment methods with pay URLs for certain payment. + * + * @return array + */ + public function getPaymentUrlsForPayment(Identifier $uid, LanguageCode $languageCode = null); } diff --git a/src/Service/GateService.php b/src/Service/GateService.php index 4073cbb..bf764b8 100644 --- a/src/Service/GateService.php +++ b/src/Service/GateService.php @@ -4,8 +4,11 @@ use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; use ThePay\ApiClient\Model\CreatePaymentParams; +use ThePay\ApiClient\Model\IPaymentMethod; use ThePay\ApiClient\Model\PaymentMethod; use ThePay\ApiClient\TheConfig; +use ThePay\ApiClient\ValueObject\Identifier; +use ThePay\ApiClient\ValueObject\LanguageCode; /** * Class GateService is responsible for rendering payment forms. @@ -21,8 +24,6 @@ class GateService implements GateServiceInterface /** * @var ApiServiceInterface - * - * @phpstan-ignore-next-line never read (never mind, backward compatibility is more important) */ private $api; @@ -95,6 +96,7 @@ public function getPaymentButtons(CreatePaymentParams $params, PaymentMethodColl } $result .= '
'; + /** @var IPaymentMethod $method */ foreach ($methods as $method) { $btnAttrs['data-payment-method'] = $method->getCode(); $result .= $this->buildButton($this->getUrlForPayment($paymentData, $method->getCode()), $this->getButtonMethodContent($method), $btnAttrs); @@ -104,6 +106,27 @@ public function getPaymentButtons(CreatePaymentParams $params, PaymentMethodColl return $result; } + /** + * @param Identifier $uid UID of payment + * @return string HTML + */ + public function getPaymentButtonsForPayment(Identifier $uid, LanguageCode $languageCode = null) + { + $paymentMethods = $this->api->getPaymentUrlsForPayment($uid, $languageCode); + $result = ''; + + $btnAttrs = array(); + + $result .= '
'; + foreach ($paymentMethods as $method) { + $btnAttrs['data-payment-method'] = $method->getCode(); + $result .= $this->buildButton($method->getPayUrl(), $this->getButtonMethodContent($method), $btnAttrs); + } + $result .= '
'; + + return $result; + } + public function getInlineAssets() { @@ -202,10 +225,10 @@ private function buildPaymentDataForm(array $paymentData, array $attributes = ar /** * Returns content of method for button link - * @param PaymentMethod $method + * @param IPaymentMethod $method * @return string HTML */ - private function getButtonMethodContent(PaymentMethod $method) + private function getButtonMethodContent(IPaymentMethod $method) { return '' . 'htmlAttributes(array('src' => $method->getImageUrl(), 'alt' => $method->getTitle())) . ' />' diff --git a/src/Service/GateServiceInterface.php b/src/Service/GateServiceInterface.php index f747052..1811d19 100644 --- a/src/Service/GateServiceInterface.php +++ b/src/Service/GateServiceInterface.php @@ -5,6 +5,8 @@ use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; use ThePay\ApiClient\Model\CreatePaymentParams; use ThePay\ApiClient\TheConfig; +use ThePay\ApiClient\ValueObject\Identifier; +use ThePay\ApiClient\ValueObject\LanguageCode; interface GateServiceInterface { @@ -19,6 +21,12 @@ public function __construct(TheConfig $config, ApiServiceInterface $api); */ public function getPaymentButtons(CreatePaymentParams $params, PaymentMethodCollection $methods); + /** + * @param Identifier $uid UID of payment + * @return string HTML + */ + public function getPaymentButtonsForPayment(Identifier $uid, LanguageCode $languageCode = null); + /** * @param string $content HTML content of button * @param string|null $methodCode diff --git a/src/TheClient.php b/src/TheClient.php index dec2e5c..d5199f9 100644 --- a/src/TheClient.php +++ b/src/TheClient.php @@ -15,6 +15,7 @@ use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; use ThePay\ApiClient\Model\CreatePaymentParams; use ThePay\ApiClient\Model\CreatePaymentResponse; +use ThePay\ApiClient\Model\PaymentMethodWithPayUrl; use ThePay\ApiClient\Model\PaymentRefundInfo; use ThePay\ApiClient\Model\Project; use ThePay\ApiClient\Model\RealizeIrregularSubscriptionPaymentParams; @@ -207,6 +208,27 @@ public function getPayments(PaymentsFilter $filter = null, $page = 1, $limit = 2 ->getPayments($filter, $page, $limit); } + /** + * Returns an array of available payment methods with pay URLs for certain payment. + * + * @param string $uid UID of payment, + * @param string|null $languageCode language code in ISO 6391 format + * @return array + */ + public function getPaymentUrlsForPayment($uid, $languageCode = null) + { + $this->validateUid($uid); + + $language = null; + if ($languageCode !== null) { + $language = new LanguageCode($languageCode); + } + + return $this + ->api + ->getPaymentUrlsForPayment(new Identifier($uid), $language); + } + /** * Returns HTML code with payment buttons for each available payment method. * Every button is a link with click event handler to post the user to the payment process. @@ -241,6 +263,26 @@ public function getPaymentButtons(CreatePaymentParams $params, PaymentMethodFilt return $result; } + /** + * Returns HTML code with payment buttons for each available payment method. + * Every button contains direct link to pay with certain method. + * + * @param string $uid UID of payment + * @param string|null $languageCode + * @param bool $useInlineAssets false value disable generation default style & scripts + * + * @return string HTML + */ + public function getPaymentButtonsForPayment($uid, $languageCode = null, $useInlineAssets = true) + { + $result = ''; + if ($useInlineAssets) { + $result .= $this->getInlineAssets(); + } + $result .= $this->gate->getPaymentButtonsForPayment(new Identifier($uid), $languageCode ? new LanguageCode($languageCode) : $languageCode); + return $result; + } + /** * @param CreatePaymentParams $params * @param string $title diff --git a/tests/Mocks/Service/ApiMockService.php b/tests/Mocks/Service/ApiMockService.php index cc6e454..0cae1f0 100644 --- a/tests/Mocks/Service/ApiMockService.php +++ b/tests/Mocks/Service/ApiMockService.php @@ -12,6 +12,7 @@ use ThePay\ApiClient\Model\CreatePaymentParams; use ThePay\ApiClient\Model\CreatePaymentResponse; use ThePay\ApiClient\Model\Payment; +use ThePay\ApiClient\Model\PaymentMethodWithPayUrl; use ThePay\ApiClient\Model\PaymentRefund; use ThePay\ApiClient\Model\PaymentRefundInfo; use ThePay\ApiClient\Model\Project; @@ -344,6 +345,69 @@ public function getActivePaymentMethods(LanguageCode $languageCode = null) ); } + /** + * Fetch all active payment methods. + * + * @return array + */ + public function getPaymentUrlsForPayment(Identifier $uid, LanguageCode $languageCode = null) + { + return array( + 0 => new PaymentMethodWithPayUrl( + array( + 'code' => 'test_online', + 'title' => 'shared::payment_methods.test_online', + 'tags' => + array( + 0 => 'access_account_owner', + 1 => 'online', + 2 => 'returnable', + ), + 'image' => + array( + 'src' => 'http://localhost:8000/img/payment_methods/test_online.png', + ), + 'url' => 'http://localhost:8000/' . $uid->__toString() . '/update?payment_method_code=test_online', + ) + ), + 1 => new PaymentMethodWithPayUrl( + array( + 'code' => 'test_offline', + 'title' => 'shared::payment_methods.test_offline', + 'tags' => + array( + 0 => 'access_account_owner', + 1 => 'returnable', + ), + 'image' => + array( + 'src' => 'http://localhost:8000/img/payment_methods/test_offline.png', + ), + 'url' => 'http://localhost:8000/' . $uid->__toString() . '/update?payment_method_code=test_offline', + ) + ), + 2 => new PaymentMethodWithPayUrl( + array( + 'code' => 'card', + 'title' => 'Platba kartou', + 'tags' => + array( + 0 => 'card', + 1 => 'online', + 2 => 'pre_authorization', + 3 => 'recurring_payments', + 4 => 'returnable', + ), + 'image' => + array( + 'src' => 'http://localhost:8000/img/payment_methods/card.png', + ), + 'url' => 'http://localhost:8000/' . $uid->__toString() . '/update?payment_method_code=card', + ) + ), + ); + } + /** * @param Identifier $paymentUid * @return Payment