From bdc11d25f2d62e18ef75c4d97bf815360ac31031 Mon Sep 17 00:00:00 2001 From: Hendry Zheng Date: Tue, 26 Oct 2021 17:25:19 +0700 Subject: [PATCH] add eWallet missing API - [Void E-Wallet Charge] - [Refund E-Wallet Charge] - [Get Refund By ID] - [List Refunds] --- README.md | 68 ++++++++ examples/EWalletsExample.php | 34 ++++ src/EWallets.php | 71 +++++++- tests/Xendit/EWalletsTest.php | 310 ++++++++++++++++++++++++++++++++++ 4 files changed, 482 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 253d1d7..2eece64 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ This library is the abstraction of Xendit API for access from applications writt - [E-Wallets](#e-wallets) - [Create E-Wallet Charge](#create-e-wallet-charge) - [Get E-Wallet Charge Status](#get-e-wallet-charge-status) + - [Void E-Wallet Charge](#void-e-wallet-charge) + - [Refund E-Wallet Charge](#refund-e-wallet-charge) + - [Get Refund By ID](#get-refund-by-id) + - [List Refunds](#list-refunds) - [Invoice](#invoice) - [Create Invoice](#create-invoice) - [Get Invoice](#get-invoice) @@ -837,7 +841,71 @@ $eWalletStatusParam = [ $getEWalletChargeStatus = \Xendit\EWallets::getEWalletChargeStatus($charge_id, $eWalletStatusParam); var_dump($getEWalletChargeStatus); ``` +#### Void E-Wallet Charge +```php +\Xendit\EWallets::voidEwalletCharge(string $charge_id,array $params); +``` + +Usage example: + +```php +$charge_id = 'ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c'; +$voidEwalletChargeParam = [ + 'for-user-id' => 'test-reference-user-id' // OPTIONAL +] +$voidEwalletCharge = \Xendit\EWallets::voidEwalletCharge($charge_id, $voidEwalletChargeParam); +var_dump($voidEwalletCharge); +``` +#### Refund E-Wallet Charge + +```php +\Xendit\EWallets::refundEwalletCharge(string $charge_id,array $params); +``` + +Usage example: + +```php +$charge_id = 'ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c'; +$refundEwalletChargeParam = [ + 'for-user-id' => 'test-reference-user-id' // OPTIONAL +] +$refundEwalletCharge = \Xendit\EWallets::refundEwalletCharge($charge_id, $refundEwalletChargeParam); +var_dump($refundEwalletCharge); +``` +#### Get Refund By ID + +```php +\Xendit\EWallets::getRefund(string $charge_id,string $refund_id, array $params); +``` + +Usage example: + +```php +$charge_id = 'ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c'; +$refund_id = 'ewr_532as23lew2321id'; +$getRefundEwalletChargeParam = [ + 'for-user-id' => 'test-reference-user-id' // OPTIONAL +] +$getRefundEwalletCharge = \Xendit\EWallets::getRefund($charge_id, $refund_id, $getRefundEwalletChargeParam); +var_dump($getRefundEwalletCharge); +``` +#### List Refunds + +```php +\Xendit\EWallets::listRefund(string $charge_id, array $params); +``` + +Usage example: + +```php +$charge_id = 'ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c'; +$listRefundEwalletChargeParam = [ + 'for-user-id' => 'test-reference-user-id' // OPTIONAL +] +$listRefundEwalletCharge = \Xendit\EWallets::listRefund($charge_id, $getRefundEwalletChargeParam); +var_dump($listRefundEwalletCharge); +``` ### Invoice #### Create Invoice diff --git a/examples/EWalletsExample.php b/examples/EWalletsExample.php index c8fbefe..1be80e3 100644 --- a/examples/EWalletsExample.php +++ b/examples/EWalletsExample.php @@ -109,3 +109,37 @@ $walletChargeStatusParams ); var_dump($getEWalletChargeStatus); + +$eWalletChargeId = ''; +$voidEwalletChargeParam = []; +$voidEwalletCharge = \Xendit\EWallets::voidEwalletCharge( + $eWalletChargeId, + $voidEwalletChargeParam + ); +var_dump($voidEwalletCharge); + +$eWalletChargeId = ''; +$refundEwalletChargeParam = []; +$refundEwalletCharge = \Xendit\EWallets::refundEwalletCharge( + $eWalletChargeId, + $refundEwalletChargeParam + ); +var_dump($refundEwalletCharge); + +$eWalletChargeId = ''; +$refundEwalletChargeId = ''; +$getRefundEWalletParam = []; +$getRefundEWallet = \Xendit\EWallets::getRefund( + $eWalletChargeId, + $refundEwalletChargeId, + $getRefundEWalletParam + ); +var_dump($getRefundEWallet); + +$eWalletChargeId = ''; +$listRefundParam = []; +$listRefund = \Xendit\EWallets::listRefund( + $eWalletChargeId, + $listRefundParam + ); +var_dump($listRefund); \ No newline at end of file diff --git a/src/EWallets.php b/src/EWallets.php index 2911de0..f21c01a 100644 --- a/src/EWallets.php +++ b/src/EWallets.php @@ -115,7 +115,7 @@ public static function createEWalletCharge($params = []) /** * Get e-wallet charge status * - * @param string $charge_id chargee ID + * @param string $charge_id charge ID * * @return array please check for responses parameters here * https://developers.xendit.co/api-reference/?bash#get-ewallet-charge-status @@ -128,4 +128,73 @@ public static function getEWalletChargeStatus($charge_id, $params=[]) return static::_request('GET', $url, $params); } + + /** + * Void eWallet Charge + * + * @param string $charge_id charge ID + * + * @return array please check for responses parameters here + * https://developers.xendit.co/api-reference/#void-ewallet-charge + * @throws Exceptions\ApiException + */ + public static function voidEwalletCharge($charge_id, $params=[]) + { + $url = static::classUrl() + . '/charges/' . $charge_id.'/void'; + + return static::_request('POST', $url, $params); + } + + /** + * Refund eWallet Charge + * + * @param string $charge_id charge ID + * + * @return array please check for responses parameters here + * https://developers.xendit.co/api-reference/#refund-ewallet-charge + * @throws Exceptions\ApiException + */ + public static function refundEwalletCharge($charge_id, $params=[]) + { + $url = static::classUrl() + . '/charges/' . $charge_id.'/refunds'; + + return static::_request('POST', $url, $params); + } + + /** + * Get eWallet Refund by Refund ID + * + * @param string $charge_id charge ID + * @param string $refund_id refund ID + * + * @return array please check for responses parameters here + * https://developers.xendit.co/api-reference/#refund-ewallet-charge + * @throws Exceptions\ApiException + */ + public static function getRefund($charge_id, $refund_id, $params=[]) + { + $url = static::classUrl() + . '/charges/' . $charge_id.'/refunds/'.$refund_id; + + return static::_request('GET', $url, $params); + } + + /** + * Get eWallet Refund by Refund ID + * + * @param string $charge_id charge ID + * + * @return array please check for responses parameters here + * https://developers.xendit.co/api-reference/#refund-ewallet-charge + * @throws Exceptions\ApiException + */ + public static function listRefund($charge_id, $params=[]) + { + $url = static::classUrl() + . '/charges/' . $charge_id.'/refunds/'; + + return static::_request('GET', $url, $params); + } } diff --git a/tests/Xendit/EWalletsTest.php b/tests/Xendit/EWalletsTest.php index e71ddd5..85d78f7 100644 --- a/tests/Xendit/EWalletsTest.php +++ b/tests/Xendit/EWalletsTest.php @@ -30,6 +30,7 @@ class EWalletsTest extends TestCase const TEST_ID = "123"; const TEST_TYPE = "DANA"; const TEST_CHARGE_ID = "ewc_f3925450-5c54-4777-98c1-fcf22b0d1e1c"; + const TEST_REFUND_ID = "ewr_532as23lew2321id"; /** * Create EWallet test @@ -407,4 +408,313 @@ public function testIsEWalletChargeGettableThrowApiException() self::TEST_CHARGE_ID ); } + + /** + * Void eWallet Charge test + * Should pass + * + * @return void + * @throws Exceptions\ApiException + */ + public function testVoidEwalletChargeCreateable() + { + $response = [ + "id" => "ewc_532as23lew2321id", + "business_id" => "5easfnn23aadlmnaa42", + "reference_id" => "test_reference_id", + "status" => "SUCCEEDED", + "currency" => "IDR", + "charge_amount" => 123456, + "capture_amount" => 123456, + "refunded_amount" => null, + "checkout_method" => "ONE_TIME_PAYMENT", + "channel_code" => "ID_OVO", + "channel_properties" => + [ + "mobile_number" => "+6287777771111" + ], + "actions" => null, + "is_redirect_required" => false, + "callback_url" => "https=>//webhook.me/gethooked", + "created" => "2020-04-20T16:23:52Z", + "updated" => "2020-04-20T16:23:52Z", + "void_status" => "PENDING", + "voided_at" => null, + "capture_now" => true, + "customer_id" => null, + "payment_method_id" => null, + "failure_code" => null, + "basket" => null, + "metadata" => + [ + "branch_code" => "senayan_372" + ] + ]; + + $this->stubRequest( + 'POST', + '/ewallets/charges/'.self::TEST_CHARGE_ID.'/void', + [], + [], + $response + ); + + $result = EWallets::voidEwalletCharge( + self::TEST_CHARGE_ID + ); + + $this->assertEquals( + $result['id'], + 'ewc_532as23lew2321id' + ); + $this->assertEquals( + $result['currency'], + 'IDR' + ); + $this->assertEquals( + $result['status'], + 'SUCCEEDED' + ); + $this->assertEquals( + $result['charge_amount'], + '123456' + ); + $this->assertEquals( + $result['capture_amount'], + '123456' + ); + $this->assertEquals( + $result['refunded_amount'], + null + ); + $this->assertEquals( + $result['checkout_method'], + 'ONE_TIME_PAYMENT' + ); + $this->assertEquals( + $result['channel_code'], + 'ID_OVO' + ); + } + + /** + * Void EWallets Charge test + * Should throw ApiException + * + * @return void + * @throws Exceptions\ApiException + */ + public function testVoidEwalletChargeCreateableThrowApiException() + { + $this->expectException(\Xendit\Exceptions\ApiException::class); + + EWallets::voidEwalletCharge( + self::TEST_CHARGE_ID + ); + } + + /** + * Refund eWallet Charge test + * Should pass + * + * @return void + * @throws Exceptions\ApiException + */ + public function testRefundEwalletChargeCreateable() + { + $response = [ + "id" => "ewr_532as23lew2321id", + "charge_id" => self::TEST_CHARGE_ID, + "status" => "PENDING", + "currency" => "IDR", + "channel_code" => "ID_OVO", + "capture_amount" => 123456, + "refund_amount" => 123456, + "reason" => "REQUESTED_BY_CUSTOMER", + "failure_code" => null, + "created" => "2020-04-20T16:23:52Z", + "updated" => "2020-04-20T16:23:52Z" + ]; + + $this->stubRequest( + 'POST', + '/ewallets/charges/'.self::TEST_CHARGE_ID.'/refunds', + [], + [], + $response + ); + + $result = EWallets::refundEwalletCharge( + self::TEST_CHARGE_ID + ); + + $this->assertEquals( + $result['id'], + "ewr_532as23lew2321id" + ); + $this->assertEquals( + $result['currency'], + 'IDR' + ); + $this->assertEquals( + $result['status'], + 'PENDING' + ); + $this->assertEquals( + $result['capture_amount'], + '123456' + ); + $this->assertEquals( + $result['refund_amount'], + 123456 + ); + $this->assertEquals( + $result['reason'], + 'REQUESTED_BY_CUSTOMER' + ); + $this->assertEquals( + $result['channel_code'], + 'ID_OVO' + ); + } + + /** + * Refund EWallets Charge test + * Should throw ApiException + * + * @return void + * @throws Exceptions\ApiException + */ + public function testRefundEwalletChargeCreateableThrowApiException() + { + $this->expectException(\Xendit\Exceptions\ApiException::class); + + EWallets::refundEwalletCharge( + self::TEST_CHARGE_ID + ); + } + + /** + * Get Refund Ewallet test + * Should pass + * + * @return void + * @throws Exceptions\ApiException + */ + public function testGetRefundGettable() + { + $response = [ + "id" => "ewr_532as23lew2321id", + "charge_id" => self::TEST_CHARGE_ID, + "status" => "SUCCEEDED", + "currency" => "IDR", + "channel_code" => "ID_OVO", + "capture_amount" => 123456, + "refund_amount" => 100000, + "reason" => "REQUESTED_BY_CUSTOMER", + "failure_code" => null, + "created" => "2020-04-20T16:23:52Z", + "updated" => "2020-04-20T16:23:52Z" + ]; + + $this->stubRequest( + 'GET', + '/ewallets/charges/'.self::TEST_CHARGE_ID.'/refunds/'.self::TEST_REFUND_ID, + [], + [], + $response + ); + + $result = EWallets::getRefund( + self::TEST_CHARGE_ID, + self::TEST_REFUND_ID + ); + $this->assertEquals( + $result['status'], + "SUCCEEDED" + ); + $this->assertEquals( + $result['id'], + "ewr_532as23lew2321id" + ); + $this->assertEquals( + $result['currency'], + 'IDR' + ); + $this->assertEquals( + $result['status'], + 'SUCCEEDED' + ); + $this->assertEquals( + $result['capture_amount'], + '123456' + ); + $this->assertEquals( + $result['refund_amount'], + 100000 + ); + $this->assertEquals( + $result['reason'], + 'REQUESTED_BY_CUSTOMER' + ); + $this->assertEquals( + $result['channel_code'], + 'ID_OVO' + ); + } + + /** + * Get Refund EWallets Charge test + * Should throw ApiException + * + * @return void + * @throws Exceptions\ApiException + */ + public function testGetRefundGettableThrowApiException() + { + $this->expectException(\Xendit\Exceptions\ApiException::class); + + EWallets::getRefund( + self::TEST_CHARGE_ID, + self::TEST_REFUND_ID + ); + } + + /** + * Get list of ewallet refund test + * Should pass + * + * @return void + * @throws Exceptions\ApiException + */ + public function testListRefundIsGettable() + { + $expectedResponse = [ + 'has_more' => false + ]; + + $this->stubRequest( + 'GET', + '/ewallets/charges/'.self::TEST_CHARGE_ID.'/refunds/', + [], + [], + $expectedResponse + ); + + $result = EWallets::listRefund(self::TEST_CHARGE_ID); + $this->assertEquals($result['has_more'], $expectedResponse['has_more']); + } + + /** + * Get Refund list test + * Should throw ApiException + * + * @return void + */ + public function testListRefundIsGettableThrowsException() + { + $this->expectException(\Xendit\Exceptions\ApiException::class); + EWallets::listRefund(self::TEST_CHARGE_ID); + } + }