From caf23308c5c6b52705c3295deb97745b95db2898 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 23 Oct 2018 15:00:56 +0500 Subject: [PATCH] Update API * append successUrl param * deprecate extra param to replace as customFields param * append extract pay url functional * set SDK fingerprint --- README.md | 23 +++++++-- src/BillPayments.php | 102 ++++++++++++++++++++++++++++++------- tests/BillPaymentsTest.php | 66 ++++++++++++++++++++---- 3 files changed, 159 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 02bbb57..11689a1 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,8 @@ $publicKey = '2tbp1WQvsgQeziGY9vTLe9vDZNg7tmCymb4Lh6STQokqKrpCC6qrUUKEDZAJ7mvFnz $params = [ 'publicKey' => $publicKey, 'amount' => 200, - 'billId' => '893794793973' + 'billId' => '893794793973', + 'successUrl' => 'http://test.ru/', ]; /** @var \Qiwi\Api\BillPayments $billPayments */ @@ -79,7 +80,7 @@ echo $link; Вывод: ``` -https://oplata.qiwi.com/create?publicKey=2tbp1WQvsgQeziGY9vTLe9vDZNg7tmCymb4Lh6STQokqKrpCC6qrUUKEDZAJ7mvFnzr1yTebUiQaBLDnebLMMxL8nc6FF5zfmGQnypdXCbQJqHEJW5RJmKfj8nvgc&amount=200&billId=893794793973 +https://oplata.qiwi.com/create?publicKey=2tbp1WQvsgQeziGY9vTLe9vDZNg7tmCymb4Lh6STQokqKrpCC6qrUUKEDZAJ7mvFnzr1yTebUiQaBLDnebLMMxL8nc6FF5zfmGQnypdXCbQJqHEJW5RJmKfj8nvgc&amount=200&billId=893794793973&successUrl=http%3A%2F%2Ftest.ru%2F ``` ### Выставление счета @@ -98,7 +99,8 @@ $fields = [ 'comment' => 'test', 'expirationDateTime' => '2018-03-02T08:44:07', 'email' => 'example@mail.org', - 'account' => 'client4563' + 'account' => 'client4563', + 'sucessUrl' => 'http://test.ru/', ]; /** @var \Qiwi\Api\BillPayments $billPayments */ @@ -131,7 +133,7 @@ Array [comment] => test [creationDateTime] => 2018-07-12T10:28:38.855+03:00 [expirationDateTime] => 2018-08-26T10:28:38.855+03:00 - [payUrl] => https://oplata.qiwi.com/form/?invoice_uid=bb773791-9bd9-42c1-b8fc-3358cd108422 + [payUrl] => https://oplata.qiwi.com/form/?invoice_uid=bb773791-9bd9-42c1-b8fc-3358cd108422&successUrl=http%3A%2F%2Ftest.ru%2F ) ``` @@ -355,6 +357,19 @@ $billPayments->checkNotificationSignature( ?> ``` +* Метод `getPayUrl` возвращает URL оплаты по счету: + +```php +getPayUrl($bill, 'http://test.ru/'); +// https://oplata.qiwi.com/form/?invoice_uid=d875277b-6f0f-445d-8a83-f62c7c07be77&successUrl=http%3A%2F%2Ftest.ru%2F + +?> +``` + ## Тестирование ```bash diff --git a/src/BillPayments.php b/src/BillPayments.php index 728ffb9..e790a0e 100644 --- a/src/BillPayments.php +++ b/src/BillPayments.php @@ -12,6 +12,19 @@ use Exception; use DateTime; +/** @var string CLIENT_NAME The client name */ +if (!defined('CLIENT_NAME')) { + define('CLIENT_NAME', 'php_sdk'); +} + +/** @var string CLIENT_VERSION The client version */ +if (!defined('CLIENT_VERSION')) { + define('CLIENT_VERSION', @json_decode( + file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.json'), + true + )['version']); +} + /** * Class for rest v 3. * @see https://developer.qiwi.com/en/bill-payments @@ -180,19 +193,44 @@ public function generateId() return Uuid::uuid4()->toString(); } + /** + * Get pay URL witch success URL param. + * + * @param array $bill The bill + * @param string $successUrl The success URL + * @return string + */ + public function getPayUrl($bill, $successUrl) { + $payUrl = parse_url($bill['payUrl']); + if (array_key_exists('query', $payUrl)) { + parse_str($payUrl['query'],$query); + $query['successUrl'] = $successUrl; + } else { + $query = [ + 'successUrl' => $successUrl + ]; + } + $payUrl['query'] = http_build_query($query, '', '&', PHP_QUERY_RFC3986); + return $this->buildUrl($payUrl); + } + /** * Creating checkout link. * * @param array $params The parameters - * ['billId'] string|number The bill identifier - * ['publicKey'] string The publicKey - * ['amount'] string|number The amount + * ['billId'] string|number The bill identifier + * ['publicKey'] string The publicKey + * ['amount'] string|number The amount + * ['successUrl'] string The success url * @return string Return result */ public function createPaymentForm($params) { $params['amount'] = isset($params['amount']) ? $this->normalizeAmount($params['amount']) : null; - return self::CREATE_URI . '?' . http_build_query($params); + $params['customFields'] = isset($params['customFields']) ? $params['customFields'] : []; + $params['customFields']['apiClient'] = CLIENT_NAME; + $params['customFields']['apiClientVersion'] = CLIENT_VERSION; + return self::CREATE_URI . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986); } /** @@ -200,20 +238,22 @@ public function createPaymentForm($params) * * @param string|number $billId The bill identifier * @param array $params The parameters - * ['amount'] string|number The amount - * ['currency'] string The currency - * ['comment'] string The bill comment - * ['expirationDateTime'] string The bill expiration datetime (ISOstring) - * ['extra'] array The bill extra data - * ['phone'] array The phone - * ['email'] string The email - * ['account'] string The account + * ['amount'] string|number The amount + * ['currency'] string The currency + * ['comment'] string The bill comment + * ['expirationDateTime'] string The bill expiration datetime (ISOstring) + * ['extra'] array The bill custom fields + * ['extra'] array The bill custom fields (deprecated, will be removed soon) + * ['phone'] array The phone + * ['email'] string The email + * ['account'] string The account + * ['successUrl'] string The success url * @return array Return result * @throws BillPaymentsException Throw on API return invalid response */ public function createBill($billId, $params) { - return $this->requestBuilder($billId, self::PUT, array_filter([ + $bill = $this->requestBuilder($billId, self::PUT, array_filter([ 'amount' => array_filter([ 'currency' => isset($params['currency']) ? $params['currency'] : null, 'value' => isset($params['amount']) ? $this->normalizeAmount($params['amount']) : null @@ -225,12 +265,19 @@ public function createBill($billId, $params) 'email' => isset($params['email']) ? $params['email'] : null, 'account' => isset($params['account']) ? $params['account'] : null, ]), - 'extra' => isset($params['extra']) ? $params['extra'] : null, - 'customFields' => [ - 'apiClient' => 'apiClient', - 'apiClientVersion' => 'php_sdk', - ], + 'customFields' => array_merge_recursive( + isset($params['extra']) ? $params['extra'] : [], // extra is deprecated, will be removed in next minor update + isset($params['customFields']) ? $params['customFields'] : [], + [ + 'apiClient' => CLIENT_NAME, + 'apiClientVersion' => CLIENT_VERSION, + ] + ), ])); + if (!empty($bill['payUrl']) && !empty($params['successUrl'])) { + $bill['payUrl'] = $this->getPayUrl($bill, $params['successUrl']); + } + return $bill; } /** @@ -330,4 +377,23 @@ protected function requestBuilder($uri, $method = self::GET, array $body = array } return empty($this->internalCurl->response) ? true : json_decode($this->internalCurl->response, true); } + + /** + * Build URL. + * + * @param array $parsedUrl The parsed URL + * @return string + */ + protected function buildUrl($parsedUrl) { + $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : ''; + $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : ''; + $port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : ''; + $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : ''; + $pass = isset($parsedUrl['pass']) ? ':' . $parsedUrl['pass'] : ''; + $pass = ($user || $pass) ? "$pass@" : ''; + $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; + $query = isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : ''; + $fragment = isset($parsedUrl['fragment']) ? '#' . $parsedUrl['fragment'] : ''; + return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; + } } diff --git a/tests/BillPaymentsTest.php b/tests/BillPaymentsTest.php index 2fe82cb..eb76984 100644 --- a/tests/BillPaymentsTest.php +++ b/tests/BillPaymentsTest.php @@ -9,6 +9,15 @@ use PHPUnit\Framework\TestCase; +/** @var string CLIENT_NAME The client name */ +if (!defined('CLIENT_NAME')) define('CLIENT_NAME', 'php_sdk'); + +/** @var string CLIENT_VERSION The client version */ +if (!defined('CLIENT_VERSION')) define('CLIENT_VERSION', @json_decode( + file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.json'), + true +)['version']); + /** * Class BillPaymentsTest * @package Qiwi\Api @@ -36,7 +45,15 @@ class BillPaymentsTest extends TestCase 'currency' => 'RUB', 'expirationDateTime' => '', // will be generate 'providerName' => 'Test', - 'comment' => 'test' + 'comment' => 'test', + 'phone' => '79999999999', + 'email' => 'test@test.ru', + 'account' => 'user uid on your side', + 'customFields' => [ + 'city' => 'Москва', + 'street' => 'Арбат' + ], + 'successUrl' => 'http://test.ru/' ]; @@ -76,11 +93,22 @@ public function testCreatePaymentForm() $uri = BillPayments::CREATE_URI; $amount_string = '200.34'; $amount_number = 200.345; - $testLink = "{$uri}?publicKey={$this->config['merchantPublicKey']}&amount={$amount_string}&billId={$this->billId}"; + $query = http_build_query([ + 'publicKey' => $this->config['merchantPublicKey'], + 'amount' => $amount_string, + 'billId' => $this->billId, + 'successUrl' => $this->fields['successUrl'], + 'customFields' => [ + 'apiClient' => CLIENT_NAME, + 'apiClientVersion' => CLIENT_VERSION + ] + ], '', '&', PHP_QUERY_RFC3986); + $testLink = "{$uri}?{$query}"; $result = $this->billPayments->createPaymentForm([ 'publicKey' => $this->config['merchantPublicKey'], 'amount' => $amount_number, - 'billId' => $this->billId + 'billId' => $this->billId, + 'successUrl' => $this->fields['successUrl'] ]); $this->assertEquals($testLink, $result, 'creates payment form'); } @@ -120,37 +148,55 @@ public function testCheckNotificationSignature() * request - bill create * @throws BillPaymentsException */ - public function testCreateBill() + public function subTestCreateBill() { $bill = $this->billPayments->createBill( $this->billId, $this->fields ); - $this->assertTrue(is_array($bill),'create bill'); + $testPayUrlQuery = http_build_query(['successUrl' => $this->fields['successUrl']], '', '&', PHP_QUERY_RFC3986); + $this->assertTrue(is_array($bill) && strpos($bill['payUrl'], $testPayUrlQuery) !== false,'create bill'); } /** * request - bill info * @throws BillPaymentsException */ - public function testGetBillInfo() + public function subTestGetBillInfo() { - $this->testCreateBill(); + $this->subTestCreateBill(); $bill = $this->billPayments->getBillInfo($this->billId); - $this->assertTrue(is_array($bill),'returns valid bill info'); + $testFields = [ + 'customFields' => [ + 'apiClient' => CLIENT_NAME, + 'apiClientVersion' => CLIENT_VERSION + ] + ]; + $this->assertArraySubset($testFields, $bill, 'returns valid bill info'); + } /** * request - bill cancel * @throws BillPaymentsException */ - public function testCancelBill() + public function subTestCancelBill() { - $this->testCreateBill(); $bill = $this->billPayments->cancelBill($this->billId); $this->assertTrue(is_array($bill),'cancel unpaid bill'); } + /** + * requests life cycle + * @throws BillPaymentsException + */ + public function testRequests() + { + $this->subTestCreateBill(); + $this->subTestGetBillInfo(); + $this->subTestCancelBill(); + } + /** * request - refund create * @throws BillPaymentsException