Skip to content

Commit

Permalink
Return payments method data as part of settings endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
vedanshujain committed Jan 6, 2025
1 parent be767e2 commit 5f9b19d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
9 changes: 6 additions & 3 deletions app/src/Lib/PaymentGateway/CashOnDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use App\Models\Order;

final class CashOnDelivery implements PaymentGatewayInterface {
public string $id = 'cod';
public string $name = 'Cash on Delivery';
public string $id = 'cod';
public string $title = 'Cash on Delivery';
public string $description = 'Pay with cash upon delivery';

public static function from_webhook_data( array $data ): PaymentGatewayInterface {
return new CashOnDelivery();
}

public function to_public_array(): array {
return array(
'id' => $this->id,
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
);
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/Lib/PaymentGateway/WooPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
final class WooPay implements PaymentGatewayInterface, ExpressPaymentGatewayInterface {

public string $id = 'woopay';
public string $title = 'WooPay';
public string $description = 'Pay with WooPay';
public string $wcpay_version = '8.6.1-woo-serverless';
public string $store_name;
public string $store_logo;
Expand Down Expand Up @@ -83,9 +85,9 @@ public static function from_webhook_data( array $data ): PaymentGatewayInterface

public function to_public_array(): array {
return array(
'id' => $this->id,
'wcpay_version' => $this->wcpay_version,
'store_name' => $this->store_name,
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
);
}

Expand Down
12 changes: 9 additions & 3 deletions app/src/Store/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function authenticate(): Controller {
}

public function handle_request(): Controller {
$country_codes = array_keys( States::$state_codes );
$country_codes = array_keys( States::$state_codes );
$country_states_data = array();
foreach ( $country_codes as $country_code ) {
$country_states_data[ $country_code ] = array(
Expand All @@ -40,8 +40,14 @@ public function handle_request(): Controller {
print_r(
json_encode(
array(
'countries' => Countries::$country_codes,
'countryData' => $country_states_data,
'countries' => Countries::$country_codes,
'countryData' => $country_states_data,
'paymentMethodData' => array_map(
function ( $gateway ) {
return $gateway->to_public_array();
},
$this->auth->site_info->payment_gateways
),
)
)
);
Expand Down
23 changes: 23 additions & 0 deletions app/tests/unit/Store/SettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use App\Lib\Middlewares\Auth\StoreAuth;
use App\Lib\Imports\I18N\Countries;
use App\Lib\Imports\I18N\States;
use App\Models\SiteInfo;
use App\Lib\PaymentGateway\WooPay;
use App\Lib\PaymentGateway\CashOnDelivery;

class SettingsControllerTest extends TestCase {
public function test_country_settings() {
Expand All @@ -34,4 +37,24 @@ public function test_only_get_request_allowed() {
$this->expectException( \WBUnitTestException::class );
$controller->serve();
}

public function test_payment_method_data() {
$request = new Request();
$site_info = new SiteInfo();
$site_info->payment_gateways = array(
'woopay' => new WooPay(),
'cod' => new CashOnDelivery(),
);
$request->setMethod( 'GET' );
$auth = $this->getMockBuilder( StoreAuth::class )->disableOriginalConstructor()->getMock();
$auth->site_info = $site_info;
$controller = new SettingsController( $auth, $request );
ob_start();
$controller->handle_request();
$output = ob_get_clean();
$this->assertStringContainsString( 'paymentMethodData', $output );
$data = json_decode( $output, true );
$this->assertEquals( $site_info->payment_gateways['woopay']->to_public_array(), $data['paymentMethodData']['woopay'] );
$this->assertEquals( $site_info->payment_gateways['cod']->to_public_array(), $data['paymentMethodData']['cod'] );
}
}

0 comments on commit 5f9b19d

Please sign in to comment.