Skip to content

Commit

Permalink
Update API
Browse files Browse the repository at this point in the history
* append successUrl param
* deprecate extra param to replace as customFields param
* append extract pay url functional
* set SDK fingerprint
  • Loading branch information
lar-dragon committed Oct 23, 2018
1 parent 33f0468 commit caf2330
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 32 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ $publicKey = '2tbp1WQvsgQeziGY9vTLe9vDZNg7tmCymb4Lh6STQokqKrpCC6qrUUKEDZAJ7mvFnz
$params = [
'publicKey' => $publicKey,
'amount' => 200,
'billId' => '893794793973'
'billId' => '893794793973',
'successUrl' => 'http://test.ru/',
];

/** @var \Qiwi\Api\BillPayments $billPayments */
Expand All @@ -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
```

### Выставление счета
Expand All @@ -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 */
Expand Down Expand Up @@ -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
)
```

Expand Down Expand Up @@ -355,6 +357,19 @@ $billPayments->checkNotificationSignature(
?>
```

* Метод `getPayUrl` возвращает URL оплаты по счету:

```php
<?php

/** @var array $bill */
/** @var \Qiwi\Api\BillPayments $billPayments */
$payUrl = $billPayments->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
Expand Down
102 changes: 84 additions & 18 deletions src/BillPayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -180,40 +193,67 @@ 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);
}

/**
* Creating bill.
*
* @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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
66 changes: 56 additions & 10 deletions tests/BillPaymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/'
];


Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit caf2330

Please sign in to comment.