Skip to content

Commit

Permalink
Merge pull request #39 from paypay/improvements/PP-45058
Browse files Browse the repository at this point in the history
Improvements/pp 45058
  • Loading branch information
Shreyansh Pandey authored Sep 17, 2020
2 parents 1268ee6 + f758870 commit 9e3a293
Show file tree
Hide file tree
Showing 22 changed files with 426 additions and 1,641 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ run_testlist:
vendor/bin/phpunit --testdox --debug tests/PaymentTest.php
vendor/bin/phpunit --testdox --debug tests/RefundTest.php
vendor/bin/phpunit --testdox --debug tests/UserTest.php
vendor/bin/phpunit --testdox --debug tests/WalletTest.php
run_staging:
clear
vendor/bin/phpunit --testdox --debug tests/QrTest.php
vendor/bin/phpunit --testdox --debug tests/PaymentTest.php
vendor/bin/phpunit --testdox --debug tests/UserTest.php
vendor/bin/phpunit --testdox --debug tests/WalletTest.php
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ parameters:
level: 8
paths:
- src
- tests
checkMissingIterableValueType: false
5 changes: 2 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Client
public function __construct($auth = null, $productionmode = false, $requestHandler = false)
{
if (!isset($auth['API_KEY']) || !isset($auth['API_SECRET'])) {
throw new Exception("Invalid auth credentials", 1);
throw new ClientException("Invalid auth credentials", 1);
}
$this->auth = $auth;
$toStg = !$productionmode ? '-stg' : '';
Expand All @@ -107,8 +107,7 @@ public function __construct($auth = null, $productionmode = false, $requestHandl
if (!$requestHandler) {
$this->requestHandler = new GuzzleHttpClient(['base_uri' => $this->config["API_URL"]]);
} else {
if (gettype($requestHandler) === 'GuzzleHttpClient' || gettype($requestHandler) === 'GuzzleHttp/Client') {
/** @phpstan-ignore-next-line */
if ($requestHandler instanceof GuzzleHttpClient) {
$this->requestHandler = $requestHandler;
} else {
throw new ClientException('Invalid request handler', 500);
Expand Down
29 changes: 4 additions & 25 deletions src/Controllers/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct($MainInstance, $auth)
public function createQRCode($payload)
{
if (!($payload instanceof CreateQrCodePayload)) {
throw new Exception("Payload not of type CreateQrCodePayload", 1);
throw new ClientControllerException("Payload not of type CreateQrCodePayload", 1);
}
$url = $this->api_url . $this->main()->GetEndpoint('CODE');
$data = $payload->serialize();
Expand All @@ -41,16 +41,7 @@ public function createQRCode($payload)
$options['TIMEOUT'] = 30;

if ($data) {
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);
}
}
/**
Expand All @@ -69,13 +60,7 @@ public function getPaymentDetails($merchantPaymentId)
if ($mid) {
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$response = $this->main()->http()->get(
$url,
[
'headers' => $options["HEADERS"]
]
);
return json_decode($response->getBody(), true);
return $this->doCall('get',$url,[],$options);
}


Expand All @@ -94,12 +79,6 @@ public function deleteQRCode($codeId)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$url = $this->api_url . $this->main()->GetEndpoint('CODE') . "/$codeId";
$response = $this->main()->http()->delete(
$url,
[
'headers' => $options["HEADERS"]
]
);
return json_decode($response->getBody(), true);
return $this->doCall('delete',$url,[],$options);
}
}
115 changes: 25 additions & 90 deletions src/Controllers/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function __construct($MainInstance, $auth)
*/
public function createPayment($payload, $agreeSimilarTransaction = false)
{
if (!($payload instanceof CreatePaymentPayload)) {
throw new ModelException("Payload not of type CreatePaymentPayload", 500, []);
}
$this->payloadTypeCheck($payload, new CreatePaymentPayload());
$data = $payload->serialize();

$url = $this->api_url . $this->main()->GetEndpoint('PAYMENT');
Expand All @@ -50,29 +48,9 @@ public function createPayment($payload, $agreeSimilarTransaction = false)
}
$options['TIMEOUT'] = 30;
if ($agreeSimilarTransaction) {

$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'query' => ['agreeSimilarTransaction' => true],
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doSimilarTransactionCall($url,$options,$data);
} else {
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);
}
}
/**
Expand All @@ -97,16 +75,8 @@ public function createContinuousPayment($payload)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$options['TIMEOUT'] = 30;
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);

}

/**
Expand All @@ -125,13 +95,7 @@ public function getPaymentDetails($merchantPaymentId)
if ($mid) {
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$response = $this->main()->http()->get(
$url,
[
'headers' => $options["HEADERS"]
]
);
return json_decode($response->getBody(), true);
return $this->doCall('get',$url,[],$options);
}

/**
Expand All @@ -154,13 +118,7 @@ public function cancelPayment($merchantPaymentId)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}

$response = $this->main()->http()->delete(
$url,
[
'headers' => $options["HEADERS"]
]
);
return json_decode($response->getBody(), true);
return $this->doCall('delete',$url,[],$options);
}

/**
Expand All @@ -172,9 +130,7 @@ public function cancelPayment($merchantPaymentId)
*/
public function createPaymentAuth($payload, $agreeSimilarTransaction = false)
{
if (!($payload instanceof CreatePaymentAuthPayload)) {
throw new ModelException("Payload not of type CreatePaymentAuthPayload", 1, []);
}
$this->payloadTypeCheck($payload, new CreatePaymentAuthPayload());
$data = $payload->serialize();

$url = $this->api_url . $this->main()->GetEndpoint('PAYMENT_PREAUTH');
Expand All @@ -186,28 +142,9 @@ public function createPaymentAuth($payload, $agreeSimilarTransaction = false)
}
$options['TIMEOUT'] = 30;
if ($agreeSimilarTransaction) {
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT'],
'query' => ['agreeSimilarTransaction' => true]
]
);

return json_decode($response->getBody(), true);
return $this->doSimilarTransactionCall($url,$options,$data);
} else {
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);
}
}

Expand All @@ -221,9 +158,7 @@ public function createPaymentAuth($payload, $agreeSimilarTransaction = false)
*/
public function capturePaymentAuth($payload)
{
if (!($payload instanceof CapturePaymentAuthPayload)) {
throw new Exception("Payload not of type CapturePaymentAuthPayload", 1);
}
$this->payloadTypeCheck($payload,new CapturePaymentAuthPayload());
$main = $this->MainInst;
$data = $payload->serialize();
$url = $main->GetConfig('API_URL') . $main->GetEndpoint('PAYMENT') . "/capture";
Expand All @@ -234,16 +169,7 @@ public function capturePaymentAuth($payload)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$options['TIMEOUT'] = 30;
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);
}

/**
Expand All @@ -257,9 +183,7 @@ public function capturePaymentAuth($payload)
*/
public function revertAuth($payload)
{
if (!($payload instanceof RevertAuthPayload)) {
throw new Exception("Payload not of type RevertAuthPayload", 1);
}
$this->payloadTypeCheck($payload,new RevertAuthPayload());
$main = $this->MainInst;
$data = $payload->serialize();
$url = $main->GetConfig('API_URL') . $main->GetEndpoint('PAYMENT') . "/preauthorize/revert";
Expand All @@ -270,15 +194,26 @@ public function revertAuth($payload)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$options['TIMEOUT'] = 30;
return $this->doCall('post',$url,$data,$options);
}
/**
* Generic HTTP call for similar transaction
*
* @param string $url
* @param array $options
* @param array $data
* @return array
*/
private function doSimilarTransactionCall($url,$options,$data){
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'query' => ['agreeSimilarTransaction' => true],
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
}
}
23 changes: 3 additions & 20 deletions src/Controllers/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ public function __construct($MainInstance, $auth)
public function refundPayment($payload)
{
if (!($payload instanceof RefundPaymentPayload)) {
throw new Exception("Payload not of type RefundPaymentPayload", 1);
throw new ClientControllerException("Payload not of type RefundPaymentPayload", 1);
}
$main = $this->MainInst;
$url = $main->GetConfig('API_URL') . $main->GetEndpoint('REFUND');
$options = $this->basePostOptions;
$options['TIMEOUT'] = 30;
$data = $payload->serialize();
$url = $main->GetConfig('API_URL') . $main->GetEndpoint('REFUND');
$endpoint = '/v2' . $main->GetEndpoint('REFUND');
Expand All @@ -42,16 +40,7 @@ public function refundPayment($payload)
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$options['TIMEOUT'] = 30;
$response = $this->main()->http()->post(
$url,
[
'headers' => $options["HEADERS"],
'json' => $data,
'timeout' => $options['TIMEOUT']
]
);

return json_decode($response->getBody(), true);
return $this->doCall('post',$url,$data,$options);
}

/**
Expand All @@ -69,12 +58,6 @@ public function getRefundDetails($merchantRefundId)
if ($mid) {
$options["HEADERS"]['X-ASSUME-MERCHANT'] = $mid;
}
$response = $this->main()->http()->get(
$url,
[
'headers' => $options["HEADERS"]
]
);
return json_decode($response->getBody(), true);
return $this->doCall('get',$url,[],$options);
}
}
Loading

0 comments on commit 9e3a293

Please sign in to comment.