Skip to content

Commit cc65d3e

Browse files
committed
Prevent Error When Getting Payment Method Before Setting
Calling [`Magento\Quote\Model\Quote\Payment::getMethodInstance`](https://github.com/magento/graphql-ce/blob/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1/app/code/Magento/Payment/Model/Info.php#L105) throws an exception when the method is not set. This would trigger and error when requsting the `selected_payment_method` cart property. This commit returns an empty string instead.
1 parent 28757e4 commit cc65d3e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3434
return [];
3535
}
3636

37+
try {
38+
$methodTitle = $payment->getMethodInstance()->getTitle();
39+
} catch (LocalizedException $e) {
40+
$methodTitle = '';
41+
}
42+
3743
return [
38-
'code' => $payment->getMethod(),
39-
'title' => $payment->getMethodInstance()->getTitle(),
44+
'code' => $payment->getMethod() ?? '',
45+
'title' => $methodTitle,
4046
'purchase_order_number' => $payment->getPoNumber(),
4147
];
4248
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSelectedPaymentMethodTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ public function testGetSelectedPaymentMethod()
4949
$this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']);
5050
}
5151

52+
/**
53+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
54+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
55+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
56+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
57+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
58+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
59+
*/
60+
public function testGetSelectedPaymentMethodBeforeSet()
61+
{
62+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
63+
64+
$query = $this->getQuery($maskedQuoteId);
65+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
66+
67+
$this->assertArrayHasKey('cart', $response);
68+
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
69+
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
70+
$this->assertEquals('', $response['cart']['selected_payment_method']['code']);
71+
}
72+
5273
/**
5374
* @magentoApiDataFixture Magento/Customer/_files/customer.php
5475
* @expectedException \Exception

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSelectedPaymentMethodTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ public function testGetSelectedPaymentMethod()
4949
$this->assertEquals('checkmo', $response['cart']['selected_payment_method']['code']);
5050
}
5151

52+
/**
53+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
54+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
55+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
56+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
57+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
58+
*/
59+
public function testGetSelectedPaymentMethodBeforeSet()
60+
{
61+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
62+
63+
$query = $this->getQuery($maskedQuoteId);
64+
$response = $this->graphQlQuery($query);
65+
66+
$this->assertArrayHasKey('cart', $response);
67+
$this->assertArrayHasKey('selected_payment_method', $response['cart']);
68+
$this->assertArrayHasKey('code', $response['cart']['selected_payment_method']);
69+
$this->assertEquals('', $response['cart']['selected_payment_method']['code']);
70+
}
71+
5272
/**
5373
* @expectedException \Exception
5474
*/

0 commit comments

Comments
 (0)