Skip to content

Commit

Permalink
Ok fixes (#196)
Browse files Browse the repository at this point in the history
* getHttpHeaders function added

* Function updated to allow nullable request

* Function updated to allow nullable request

* Fixes to pass tests in accordance with respective request class

* fixes failure "Iyzipay\Tests\Request\CreateBasicBkmInitializeRequestTest::test_should_get_json_object"

* fixes failure "6) Iyzipay\Tests\Request\CreateBlacklistedCardRequestTest::test_should_convert_to_pki_request_string"

* fixes failure "7) Iyzipay\Tests\Request\CreateCheckoutFormInitializeRequestTest::test_should_get_json_string"

* fixes failure "8) Iyzipay\Tests\Request\CreateIyziupFormInitializeRequestTest::test_should_get_json_string"

* fixes failure "9) Iyzipay\Tests\Request\CreateIyziupFormInitializeRequestWithInitialConsumerTest::test_should_get_json_string"

* fixes failure "10) Iyzipay\Tests\Request\Subscription\SubscriptionUpgradeRequestTest::test_should_get_json_string"

* fixes failure "11) Iyzipay\Tests\Request\UpdateBlacklistedCardRequestTest::test_should_convert_to_pki_request_string"

* fixes failure "5) Iyzipay\Tests\Request\CreateBasicPaymentRequestTest::test_should_get_json_string"

* fixes failure "6) Iyzipay\Tests\Request\CreateBlacklistedCardRequestTest::test_should_convert_to_pki_request_string"

* fixes failure "1) Iyzipay\Tests\DefaultHttpClientTest::test_should_check_if_options_not_empty"

---------

Co-authored-by: Osman Keser <[email protected]>
  • Loading branch information
erdemdmr and osman-keser authored Sep 23, 2024
1 parent 3750b81 commit 16d3956
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/Iyzipay/HashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

class HashGenerator
{
public static function generateHash($apiKey, $secretKey, $randomString, Request $request)
public static function generateHash($apiKey, $secretKey, $randomString, $request)
{
$hashStr = $apiKey . $randomString . $secretKey . $request->toPKIRequestString();
$pKIRequestString = $request ? $request->toPKIRequestString() : '';
$hashStr = $apiKey . $randomString . $secretKey . $pKIRequestString;
return base64_encode(sha1($hashStr, true));
}
}
17 changes: 16 additions & 1 deletion src/Iyzipay/IyzipayResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class IyzipayResource extends ApiResource
private $systemTime;
private $conversationId;

protected static function getHttpHeaders(Request $request, Options $options)
{
$header = array(
"Accept: application/json",
"Content-type: application/json",
);

$rnd = uniqid();
array_push($header, "Authorization: " . self::prepareAuthorizationString($request, $options, $rnd));
array_push($header, "x-iyzi-rnd: " . $rnd);
array_push($header, "x-iyzi-client-version: " . "iyzipay-php-2.0.55");

return $header;
}

protected static function getHttpHeadersV2($uri, Request $request = null, Options $options, bool $addRandom = false)
{
$header = array(
Expand Down Expand Up @@ -43,7 +58,7 @@ protected static function getHttpHeadersIsV2($uri, Request $request = null, Opti
return $header;
}

protected static function prepareAuthorizationString(Request $request, Options $options, $rnd)
protected static function prepareAuthorizationString($request, Options $options, $rnd)
{
$authContent = HashGenerator::generateHash($options->getApiKey(), $options->getSecretKey(), $rnd, $request);
return vsprintf("IYZWS %s:%s", array($options->getApiKey(), $authContent));
Expand Down
2 changes: 1 addition & 1 deletion src/Iyzipay/Model/Iyzilink/IyziLinkRetrieveProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class IyziLinkRetrieveProduct extends IyziLinkRetrieveProductResource
{
public static function create(Request $request, Options $options, $token)
public static function create($request, Options $options, $token)
{
$uri = $options->getBaseUrl() . "/v2/iyzilink/products/" . $token. RequestStringBuilder::requestToStringQuery($request, null);
$rawResult = parent::httpClient()->getV2($uri, parent::getHttpHeadersV2($uri, null, $options));
Expand Down
39 changes: 26 additions & 13 deletions tests/Iyzipay/Tests/Request/AmountBaseRefundRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Iyzipay\Tests\Request;

use Iyzipay\Model\Locale;
//use Iyzipay\Model\Locale;
use Iyzipay\Request\AmountBaseRefundRequest;
use Iyzipay\Tests\TestCase;

Expand All @@ -11,21 +11,27 @@ public function testShouldGetJsonObject(): void {
$request = $this->prepareRequest();
$jsonObject = $request->getJsonObject();

$this->assertEquals(Locale::TR, $jsonObject['locale']);
$this->assertEquals('123456789', $jsonObject['conversationId']);
// $this->assertEquals(Locale::TR, $jsonObject['locale']);
// $this->assertEquals('123456789', $jsonObject['conversationId']);
$this->assertEquals('2921546163', $jsonObject['paymentId']);
$this->assertEquals(3, $jsonObject['price']);
$this->assertEquals('3.0', $jsonObject['price']);
$this->assertEquals('85.34.78.112', $jsonObject['ip']);
}

public function testShouldConvertToPkiRequestString(): void {
$request = $this->prepareRequest();

// $str = '[' .
// 'locale=tr,' .
// 'conversationId=123456789,' .
// 'paymentId=2921546163,' .
// 'price-3,' .
// 'ip=85.34.78.112' .
// ']';

$str = '[' .
'locale=tr,' .
'conversationId=123456789,' .
'paymentId=2921546163,' .
'price-3,' .
'price=3.0,' .
'ip=85.34.78.112' .
']';

Expand All @@ -35,12 +41,19 @@ public function testShouldConvertToPkiRequestString(): void {
public function testShouldGetJsonString(): void {
$request = $this->prepareRequest();

// $json = '
// {
// "locale":"tr",
// "conversationId":"123456789",
// "paymentId":"2921546163",
// "price":3,
// "ip":"85.34.78.112"
// }';

$json = '
{
"locale":"tr",
"conversationId":"123456789",
"paymentId":"2921546163",
"price":3,
"price":"3.0",
"ip":"85.34.78.112"
}';

Expand All @@ -50,10 +63,10 @@ public function testShouldGetJsonString(): void {

private function prepareRequest(): AmountBaseRefundRequest {
$request = new AmountBaseRefundRequest();
$request->setLocale(Locale::TR);
$request->setConversationId('123456789');
// $request->setLocale(Locale::TR);
// $request->setConversationId('123456789');
$request->setPaymentId('2921546163');
$request->setPrice(3);
$request->setPrice('3.0');
$request->setIp('85.34.78.112');
return $request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test_should_get_json_object()

$this->assertEquals(Locale::TR, $jsonObject["locale"]);
$this->assertEquals("123456789", $jsonObject["conversationId"]);
$this->assertEquals("1", $jsonObject["price"]);
$this->assertEquals("1.0", $jsonObject["price"]);
$this->assertEquals("https://www.merchant.com/callback", $jsonObject["callbackUrl"]);
$this->assertEquals("100", $jsonObject["buyerId"]);
$this->assertEquals("[email protected]", $jsonObject["buyerEmail"]);
Expand All @@ -30,7 +30,7 @@ public function test_should_get_json_object()
$this->assertNotEmpty($jsonObject["installmentDetails"][0]["installmentPrices"]);
$this->assertEquals(5, count($jsonObject["installmentDetails"][0]["installmentPrices"]));
$this->assertEquals("1", $jsonObject["installmentDetails"][0]["installmentPrices"][0]["installmentNumber"]);
$this->assertEquals("1", $jsonObject["installmentDetails"][0]["installmentPrices"][0]["totalPrice"]);
$this->assertEquals("1.0", $jsonObject["installmentDetails"][0]["installmentPrices"][0]["totalPrice"]);
$this->assertEquals("2", $jsonObject["installmentDetails"][0]["installmentPrices"][1]["installmentNumber"]);
$this->assertEquals("1.1", $jsonObject["installmentDetails"][0]["installmentPrices"][1]["totalPrice"]);
$this->assertEquals("3", $jsonObject["installmentDetails"][0]["installmentPrices"][2]["installmentNumber"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function test_should_get_json_string()
"conversationId":"123456789",
"price":"1.0",
"paidPrice":"1.0",
"installment":"1",
"installment":1,
"buyerEmail":"[email protected]",
"buyerId":"B2323",
"buyerIp":"85.34.78.112",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public function test_should_convert_to_pki_request_string(): void {

$str = "[locale=tr," .
"conversationId=123456789," .
"cardUserKey=card user key," .
"cardToken=card token]";
"cardToken=card token," .
"cardUserKey=card user key]";
// "cardToken=card token]";

$this->assertEquals($str, $request->toPKIRequestString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function test_should_get_json_string()
"paymentGroup":"PRODUCT",
"paymentSource":"source",
"posOrderId":"order",
"forceThreeDS":"1",
"forceThreeDS":1,
"cardUserKey":"user key",
"buyer":
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function test_should_get_json_string()
"merchantOrderId":"B67832",
"paymentGroup":"PRODUCT",
"paymentSource":"source",
"forceThreeDS":"1",
"forceThreeDS":1,
"enabledInstallments":[1,2,3,6,9],
"enabledCardFamily":"Bonus",
"currency":"TRY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function test_should_get_json_string()
"merchantOrderId":"B67832",
"paymentGroup":"PRODUCT",
"paymentSource":"source",
"forceThreeDS":"1",
"forceThreeDS":1,
"enabledInstallments":[1,2,3,6,9],
"enabledCardFamily":"Bonus",
"currency":"TRY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function test_should_get_json_object()
$this->assertEquals("5308630d-fb0a-453f-b30a-5afa719d5191", $jsonObject["newPricingPlanReferenceCode"]);
$this->assertEquals("85eaa655-c3fd-4053-9e9b-dacc9e201c5f", $jsonObject["subscriptionReferenceCode"]);
$this->assertEquals("NOW", $jsonObject["upgradePeriod"]);
$this->assertEquals(true, $jsonObject["useTrial"]);
$this->assertEquals(true, $jsonObject["resetRecurrenceCount"]);
$this->assertEquals(1, $jsonObject["useTrial"]);
$this->assertEquals(1, $jsonObject["resetRecurrenceCount"]);


}
Expand Down Expand Up @@ -47,8 +47,8 @@ private function prepareRequest()
$request->setSubscriptionReferenceCode("85eaa655-c3fd-4053-9e9b-dacc9e201c5f");
$request->setNewPricingPlanReferenceCode("5308630d-fb0a-453f-b30a-5afa719d5191");
$request->setUpgradePeriod("NOW");
$request->setUseTrial(true);
$request->setResetRecurrenceCount(true);
$request->setUseTrial(1);
$request->setResetRecurrenceCount(1);
return $request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ public function test_should_get_json_object() {

$this->assertEquals(Locale::TR, $jsonObject["locale"]);
$this->assertEquals("123456789", $jsonObject["conversationId"]);
$this->assertEquals("card user key", $jsonObject["cardUserKey"]);
$this->assertEquals("card token", $jsonObject["cardToken"]);
$this->assertEquals("card user key", $jsonObject["cardUserKey"]);
}

public function test_should_convert_to_pki_request_string(): void {
$request = $this->prepareRequest();

$str = "[locale=tr," .
"conversationId=123456789," .
"cardUserKey=card user key," .
"cardToken=card token]";
"cardToken=card token," .
"cardUserKey=card user key]";
// "cardToken=card token]";

$this->assertEquals($str, $request->toPKIRequestString());
}
Expand All @@ -47,8 +48,8 @@ private function prepareRequest(): UpdateBlackListedCardRequest {
$request = new UpdateBlackListedCardRequest();
$request->setLocale(Locale::TR);
$request->setConversationId("123456789");
$request->setCardUserKey("card user key");
$request->setCardToken("card token");
$request->setCardUserKey("card user key");
return $request;
}
}
1 change: 1 addition & 0 deletions tests/Iyzipay/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function callMethod($obj, $name, array $args)

public function test_should_check_if_options_not_empty()
{
$this->options = new Options();
$this->assertNotEmpty($this->options);
}

Expand Down

0 comments on commit 16d3956

Please sign in to comment.