From ee684599fe54d2df6d99f7a9df27a29114b73cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pu=C5=9Blecki?= Date: Tue, 20 Sep 2016 11:30:09 +0200 Subject: [PATCH] Cast to proper class for Resource in Response --- MangoPay/ApiResponses.php | 9 ++++-- MangoPay/Libraries/ApiBase.php | 53 +++++++++++++++++++++++++++++++++- MangoPay/Response.php | 7 +++++ demos/api/form.php | 4 +-- demos/api/menu.php | 3 ++ tests/cases/base.php | 6 ++-- tests/cases/idempotency.php | 11 +++++++ 7 files changed, 85 insertions(+), 8 deletions(-) diff --git a/MangoPay/ApiResponses.php b/MangoPay/ApiResponses.php index 4917570a..c41e6f73 100644 --- a/MangoPay/ApiResponses.php +++ b/MangoPay/ApiResponses.php @@ -12,7 +12,6 @@ */ class ApiResponses extends Libraries\ApiBase { - /** * Get response from previous call by idempotency key * @param string $idempotencyKey Idempotency key @@ -20,6 +19,12 @@ class ApiResponses extends Libraries\ApiBase */ public function Get($idempotencyKey) { - return $this->GetObject('responses_get', $idempotencyKey, 'MangoPay\Response'); + $response = $this->GetObject('responses_get', $idempotencyKey, 'MangoPay\Response'); + $className = $this->GetObjectForIdempotencyUrl($response->RequestUrl); + if (is_null($className) || empty($className)) + return $response; + + $response->Resource = $this->CastResponseToEntity($response->Resource, $className); + return $response; } } diff --git a/MangoPay/Libraries/ApiBase.php b/MangoPay/Libraries/ApiBase.php index b27ada03..328a231f 100644 --- a/MangoPay/Libraries/ApiBase.php +++ b/MangoPay/Libraries/ApiBase.php @@ -134,7 +134,7 @@ protected function getLogger() 'temp_paymentcards_get' => array( '/temp/paymentcards/%s', RequestType::GET ), 'temp_immediatepayins_create' => array( '/temp/immediate-payins', RequestType::POST ), - 'mandates_create' => array( '/mandates/directdebit/web', RequestType::POST ), + 'mandates_create' => array( '/mandates/directdebit/web', RequestType::POST ), 'mandates_save' => array( '/mandates/%s/cancel', RequestType::PUT ), 'mandates_get' => array( '/mandates/%s', RequestType::GET ), 'mandates_all' => array( '/mandates', RequestType::GET ), @@ -438,4 +438,55 @@ private function CanReadSubRequestData($entity, $propertyName) return false; } + + protected function GetObjectForIdempotencyUrl($url){ + if (is_null($url) || empty($url)) + return null; + + $map = array( + 'preauthorization_create' => '\MangoPay\CardPreAuthorization', + 'cardregistration_create' => '\MangoPay\CardRegistration', + 'temp_paymentcards_create' => '\MangoPay\TemporaryPaymentCard', + 'client_upload_logo' => '', + 'disputes_repudiation_create_settlement' => '\MangoPay\Transfer', + 'disputes_document_create' => '\MangoPay\DisputeDocument', + 'disputes_document_page_create' => '', + 'hooks_create' => '\MangoPay\Hook', + 'mandates_create' => '\MangoPay\Mandate', + 'payins_card-web_create' => '\MangoPay\PayIn', + 'payins_card-direct_create' => '\MangoPay\PayIn', + 'payins_preauthorized-direct_create' => '\MangoPay\PayIn', + 'payins_bankwire-direct_create' => '\MangoPay\PayIn', + 'payins_directdebit-web_create' => '\MangoPay\PayIn', + 'payins_directdebit-direct_create' => '\MangoPay\PayIn', + 'payins_createrefunds' => '\MangoPay\Refund', + 'temp_immediatepayins_create' => '\MangoPay\TemporaryImmediatePayIn', + 'payouts_bankwire_create' => '\MangoPay\PayOut', + 'reports_create' => '\MangoPay\ReportRequest', + 'transfers_create' => '\MangoPay\Transfer', + 'transfers_createrefunds' => '\MangoPay\Refund', + 'users_createnaturals' => '\MangoPay\UserNatural', + 'users_createlegals' => '\MangoPay\UserLegal', + 'users_createbankaccounts_iban' => '\MangoPay\BankAccount', + 'users_createbankaccounts_gb' => '\MangoPay\BankAccount', + 'users_createbankaccounts_us' => '\MangoPay\BankAccount', + 'users_createbankaccounts_ca' => '\MangoPay\BankAccount', + 'users_createbankaccounts_other' => '\MangoPay\BankAccount', + 'kyc_documents_create' => '\MangoPay\KycDocument', + 'kyc_page_create' => '', + 'wallets_create' => '\MangoPay\Wallet' + ); + + foreach ($map as $key => $className) { + $sourceUrl = $this->GetRequestUrl($key); + $sourceUrl = str_replace("%s", "[0-9a-zA-Z]*", $sourceUrl); + $sourceUrl = str_replace("/", "\/", $sourceUrl); + $pattern = '/' . $sourceUrl . '/'; + if (preg_match($pattern, $url) > 0) { + return $className; + } + } + + return null; + } } \ No newline at end of file diff --git a/MangoPay/Response.php b/MangoPay/Response.php index 94d94e36..1ca99872 100644 --- a/MangoPay/Response.php +++ b/MangoPay/Response.php @@ -31,6 +31,13 @@ class Response extends Libraries\Dto */ public $Date; + + /** + * Request URL where the idempotency key was used + * @var string + */ + public $RequestUrl; + /** * Entity or response data (JSON-serialized) * @var string diff --git a/demos/api/form.php b/demos/api/form.php index 8a980285..a57d1f28 100644 --- a/demos/api/form.php +++ b/demos/api/form.php @@ -17,8 +17,8 @@ $subEntityName = @$details[3]; $filterName = @$details[4]; $subSubEntityName = @$details[5]; -$entityId = (int)@$_POST['Id']; -$subEntityId = (int)@$_POST['IdSubEntity']; +$entityId = @$_POST['Id']; +$subEntityId = @$_POST['IdSubEntity']; if (isset($_POST['_postback']) && $_POST['_postback'] == '1') { diff --git a/demos/api/menu.php b/demos/api/menu.php index 3924711a..5a92d6cf 100644 --- a/demos/api/menu.php +++ b/demos/api/menu.php @@ -129,6 +129,9 @@ 'List all report request' => 'ReportRequest_Reports_All__FilterReports__$Sort', //'List transactions for wallet' => 'Wallet_Wallets_ListSubEntity_GetTransactions_FilterTransactions__$Sort', ), + 'Responses' => array( + 'Get responses' => 'Response_Responses_Get' + ), ); ?> diff --git a/tests/cases/base.php b/tests/cases/base.php index d7650420..f2519e66 100644 --- a/tests/cases/base.php +++ b/tests/cases/base.php @@ -91,9 +91,9 @@ protected function buildNewMangoPayApi() { // use test client credentails $api->Config->ClientId = 'sdk-unit-tests'; - // sandbox environment: - $api->Config->BaseUrl = 'https://api.sandbox.mangopay.com'; - $api->Config->ClientPassword = 'cqFfFrWfCcb7UadHNxx2C9Lo6Djw8ZduLi7J9USTmu8bhxxpju'; + // sandbox environment: + $api->Config->BaseUrl = 'https://api.sandbox.mangopay.com'; + $api->Config->ClientPassword = 'cqFfFrWfCcb7UadHNxx2C9Lo6Djw8ZduLi7J9USTmu8bhxxpju'; $api->OAuthTokenManager->RegisterCustomStorageStrategy(new \MangoPay\Tests\MockStorageStrategy()); diff --git a/tests/cases/idempotency.php b/tests/cases/idempotency.php index 1d26c2fb..2f7daa36 100644 --- a/tests/cases/idempotency.php +++ b/tests/cases/idempotency.php @@ -42,4 +42,15 @@ function test_DifferentIdempotencyKey_ActIndependentlyAndRetreivable() { $this->assertTrue($resp1->Resource->Id == $user1->Id); $this->assertTrue($resp2->Resource->Id == $user2->Id); } + + function test_IdempotencyKey_CheckResponseObject() { + $idempotencyKey = md5(uniqid()); + $user = $this->buildJohn(); + $user1 = $this->_api->Users->Create($user, $idempotencyKey); + + // responses may be retreived later + $resp1 = $this->_api->Responses->Get($idempotencyKey); + + $this->assertIsA($resp1->Resource, '\MangoPay\UserNatural'); + } } \ No newline at end of file