Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

payment urls #28

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/create-payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ 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)

### Redirect user to gate with payment method selected for already created payment
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved

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)

#### Buttons css customization

Example of rendered HTML for one button, values with **some** word can dynamically change in HTML rendering.
Expand Down
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 @@ -12,6 +12,7 @@
| getPayments | https://dataapi21.docs.apiary.io/#reference/0/project-level-resources/get-payments |
| getPaymentButtons | |
| getPaymentButton | |
| getPaymentButtons | |
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved
| 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 @@ -22,6 +23,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-creation/saved-card-authorization/change-method-for-payment/get-available-payment-methods |
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved

## Usage examples

Expand Down Expand Up @@ -54,6 +56,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 @@ -113,6 +117,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 | PaymentMethodFilter | optional | Language code in ISO 6391 (2 chars) format |
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved
| $useInlineAssets | bool | optional | will generate basic css & js |

#### Parameters

| name | type | | desc |
Expand Down Expand Up @@ -234,3 +250,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 | PaymentMethodFilter | optional | Language code in ISO 6391 (2 chars) format |
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions src/Model/IPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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();

/**
* @return array<string, mixed>
*/
public function toArray();
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved
}
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);
AlexKratky marked this conversation as resolved.
Show resolved Hide resolved
$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