Skip to content

Commit

Permalink
Cast to proper class for Resource in Response
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-puslecki committed Sep 20, 2016
1 parent 5b33c99 commit ee68459
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
9 changes: 7 additions & 2 deletions MangoPay/ApiResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
*/
class ApiResponses extends Libraries\ApiBase
{

/**
* Get response from previous call by idempotency key
* @param string $idempotencyKey Idempotency key
* @return \MangoPay\Response Entity of Response object
*/
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;
}
}
53 changes: 52 additions & 1 deletion MangoPay/Libraries/ApiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions MangoPay/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions demos/api/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Expand Down
3 changes: 3 additions & 0 deletions demos/api/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
);

?>
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
11 changes: 11 additions & 0 deletions tests/cases/idempotency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit ee68459

Please sign in to comment.