Skip to content

Commit

Permalink
Merge pull request #37 from ThePay/v1.x-autumn
Browse files Browse the repository at this point in the history
Release - 1.x autumn
  • Loading branch information
tymajiri authored Mar 30, 2023
2 parents ba7d483 + 0940aca commit e8e8280
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 6 deletions.
22 changes: 22 additions & 0 deletions doc/get-pay-urls-for-existing-payment.md
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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

Expand Down Expand Up @@ -53,6 +55,8 @@

[Saving authorization](saving-authorization.md)

[Get pay URLs for existing payments](get-pay-urls-for-existing-payment.md)

## Methods

### getProjects
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
28 changes: 28 additions & 0 deletions src/Model/IPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ThePay\ApiClient\Model;

use ThePay\ApiClient\ValueObject\Url;

interface IPaymentMethod
{
/**
* @return string
*/
public function getCode();

/**
* @return string
*/
public function getTitle();

/**
* @return Url
*/
public function getImageUrl();

/**
* @return array<string>
*/
public function getTags();
}
2 changes: 1 addition & 1 deletion src/Model/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions src/Model/PaymentMethodWithPayUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace ThePay\ApiClient\Model;

use ThePay\ApiClient\Utils\Json;
use ThePay\ApiClient\ValueObject\Url;

class PaymentMethodWithPayUrl implements IPaymentMethod
{
/** @var string */
private $code;

/** @var string */
private $title;

/** @var array<string> */
private $tags;

/** @var Url|null */
private $imageUrl;

/** @var Url */
private $payUrl;

/**
* @param string|array<string, mixed> $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<string>
*/
public function getTags()
{
return $this->tags;
}

/**
* @return array<string, mixed>
*/
public function toArray()
{
return array(
'code' => $this->code,
'title' => $this->title,
'tags' => $this->tags,
'imageUrl' => (string) $this->imageUrl,
'payUrl' => (string) $this->payUrl,
);
}
}
33 changes: 32 additions & 1 deletion src/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<PaymentMethodWithPayUrl>
*/
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
Expand Down
8 changes: 8 additions & 0 deletions src/Service/ApiServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PaymentMethodWithPayUrl>
*/
public function getPaymentUrlsForPayment(Identifier $uid, LanguageCode $languageCode = null);
}
31 changes: 27 additions & 4 deletions src/Service/GateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -95,6 +96,7 @@ public function getPaymentButtons(CreatePaymentParams $params, PaymentMethodColl
}

$result .= '<div class="tp-btn-grid" >';
/** @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);
Expand All @@ -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 .= '<div class="tp-btn-grid" >';
foreach ($paymentMethods as $method) {
$btnAttrs['data-payment-method'] = $method->getCode();
$result .= $this->buildButton($method->getPayUrl(), $this->getButtonMethodContent($method), $btnAttrs);
}
$result .= '</div>';

return $result;
}


public function getInlineAssets()
{
Expand Down Expand Up @@ -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 '<span class="tp-icon" >'
. '<img ' . $this->htmlAttributes(array('src' => $method->getImageUrl(), 'alt' => $method->getTitle())) . ' />'
Expand Down
8 changes: 8 additions & 0 deletions src/Service/GateServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
Loading

0 comments on commit e8e8280

Please sign in to comment.