Skip to content

Commit

Permalink
Refactored and updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Oct 10, 2023
1 parent b71df3a commit 8b5c467
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 52 deletions.
6 changes: 1 addition & 5 deletions includes/gateway/abstract-omise-payment-base-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ private function prepareChargeData($order_id, $order, $omise_customer_id, $card_
$data = [
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters(
'omise_charge_params_description',
'WooCommerce Order id ' . $order_id,
$order
),
'description' => 'WooCommerce Order id ' . $order_id,
'return_uri' => $this->getRedirectUrl('omise_callback', $order_id, $order),
'metadata' => $this->getMetadata(
$order_id,
Expand Down
25 changes: 10 additions & 15 deletions includes/gateway/abstract-omise-payment-offline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@
/**
* @since 4.0
*/
abstract class Omise_Payment_Offline extends Omise_Payment {
abstract class Omise_Payment_Offline extends Omise_Payment
{
use Charge_Request_Builder;

protected $enabled_processing_notification = true;

/**
* @inheritdoc
*/
public function charge( $order_id, $order ) {
$total = $order->get_total();
$currency = $order->get_currency();
$metadata = array_merge(
apply_filters( 'omise_charge_params_metadata', array(), $order ),
array( 'order_id' => $order_id ) // override order_id as a reference for webhook handlers.
public function charge( $order_id, $order )
{
$requestData = $this->build_charge_request(
$order_id,
$order,
$this->source_type
);

return OmiseCharge::create( array(
'amount' => Omise_Money::to_subunit( $total, $currency ),
'currency' => $currency,
'description' => apply_filters( 'omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order ),
'source' => array( 'type' => $this->source_type ),
'metadata' => $metadata,
'webhook_endpoints' => [ Omise_Util::getWebhookURL() ]
) );
return OmiseCharge::create($requestData);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/gateway/class-omise-payment-ocbc-digital.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public function get_icon()
'alternate_text' => 'OCBC Digital',
'width' => 60,
]);
return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
return Omise_Util::apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,40 @@ public function build_charge_request(
$order_id,
$order,
$source_type,
$callback_endpoint
$callback_endpoint = null
)
{
$currency = $order->get_currency();
$return_uri = $this->getRedirectUrl($callback_endpoint, $order_id, $order);
$description = 'WooCommerce Order id ' . $order_id;

return [
$request = [
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => $description,
'return_uri' => $return_uri,
'metadata' => $this->getMetadata($order_id, $order),
'metadata' => $this->getMetadata($order_id),
'webhook_endpoints' => [ Omise_Util::getWebhookURL() ],
'source' => [ 'type' => $source_type ]
];

if (!$callback_endpoint) {
$return_uri = $this->getRedirectUrl($callback_endpoint, $order_id, $order);

return array_merge($request, [
'return_uri' => $return_uri,
]);
}

return $request;
}

/**
* @param string $order_id
* @param object $order
* @param array $additionalData
*/
public function getMetadata($order_id, $order, $additionalData = [])
public function getMetadata($order_id, $additionalData = [])
{
// override order_id as a reference for webhook handlers.
$orderId = [ 'order_id' => $order_id ];

return array_merge($orderId, $additionalData);
}

Expand All @@ -44,7 +50,6 @@ public function getMetadata($order_id, $order, $additionalData = [])
*/
public function getRedirectUrl($callback_url, $order_id, $order)
{
// return 'https://opn.ooo';
$redirectUrl = RedirectUrl::create($callback_url, $order_id);

// Call after RedirectUrl::create
Expand Down
1 change: 1 addition & 0 deletions omise-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private function include_classes()
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/events/class-omise-event-charge-complete.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/events/class-omise-event-charge-create.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/gateway/traits/sync-order-trait.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/gateway/traits/charge-request-builder-trait.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/gateway/abstract-omise-payment-offline.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/gateway/abstract-omise-payment-offsite.php';
require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/gateway/abstract-omise-payment-base-card.php';
Expand Down
70 changes: 48 additions & 22 deletions tests/unit/includes/gateway/traits/charge-request-builder-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,78 @@ class Charge_Request_Builder_Test extends TestCase
{
public static function setUpBeforeClass(): void
{
require_once __DIR__ . '/../../../../../includes/gateway/traits/charge-request-builder.php';
}
require_once __DIR__ . '/../../../../../includes/gateway/traits/charge-request-builder-trait.php';

/**
* @test
*/
public function buildChargeRequestReturnsValidResponse()
{
if (!function_exists('get_rest_url')) {
function get_rest_url() {
return "http://localhost/";
}
}
}

$mock = $this->getMockForTrait('Charge_Request_Builder');
public function getOrderMock($expectedAmount, $expectedCurrency)
{
// Create a mock of the $order object
$orderMock = Mockery::mock('WC_Order');

// Define expectations for the mock
$orderMock->shouldReceive('get_currency')
->andReturn($expectedCurrency);
$orderMock->shouldReceive('get_total')
->andReturn($expectedAmount); // in units
$orderMock->shouldReceive('add_meta_data');
return $orderMock;
}

public function testBuildChargeRequestForNonOfflinePayment()
{
$redirectUrlMock = Mockery::mock('alias:RedirectUrl');
$redirectUrlMock->shouldReceive('create')
->andReturn('https://abc.com/order/complete');
$redirectUrlMock->shouldReceive('getToken')
->andReturn('token123');

$order_id = 'order_123';
$order = new class {
public $property;

public function get_total() {
return 999999;
}
$expectedAmount = 999999;
$expectedCurrency = 'thb';

public function get_currency() {
return 'THB';
}
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

public function add_meta_data() {}
};
$source_type = 'alipay';
$callback_url = 'omise_alipay_callback';

$mock = $this->getMockForTrait('Charge_Request_Builder');
$result = $mock->build_charge_request(
$order_id,
$order,
$orderMock,
$source_type,
$callback_url
);

$this->assertEquals($source_type, $result['source']['type']);
$this->assertEquals(999999*100, $result['amount']);
$this->assertEquals($expectedAmount*100, $result['amount']);
$this->assertEquals($expectedCurrency, $result['currency']);
}

public function testBuildChargeRequestForOfflinePayment()
{
$order_id = 'order_123';
$expectedAmount = 999999;
$expectedCurrency = 'thb';

$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

$source_type = 'promptpay';
$callback_url = 'omise_promptpay_callback';
$mock = $this->getMockForTrait('Charge_Request_Builder');
$result = $mock->build_charge_request(
$order_id,
$orderMock,
$source_type,
null,// null means payment is offline
);

$this->assertEquals($source_type, $result['source']['type']);
$this->assertEquals($expectedAmount*100, $result['amount']);
$this->assertEquals($expectedCurrency, $result['currency']);
}
}

0 comments on commit 8b5c467

Please sign in to comment.