PluginProbe
seQura / 4.1.0
seQura v4.1.0
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
← All changes | src/Controllers/Rest/class-payment-rest-controller.php +37 -66 3.0.24.1.0 View file →
@@ -8,11 +8,13 @@
8 8
9 9 namespace SeQura\WC\Controllers\Rest;
10 10
11 11 use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI;
12 -use SeQura\Core\Infrastructure\Logger\LogContextData;
13 -use SeQura\WC\Services\Interface_Logger_Service;
14 -use SeQura\WC\Services\Payment\Interface_Payment_Method_Service;
12 +use SeQura\WC\Services\Log\Interface_Logger_Service;
13 +use SeQura\Core\BusinessLogic\AdminAPI\PaymentMethods\Requests\GetFormattedPaymentMethodsRequest;
14 +use SeQura\Core\BusinessLogic\AdminAPI\PaymentMethods\Requests\GetPaymentMethodsRequest;
15 +use SeQura\Core\BusinessLogic\AdminAPI\Response\Response;
16 +use SeQura\Core\Infrastructure\Utility\RegexProvider;
15 17 use WP_Error;
16 18 use WP_REST_Request;
17 19 use WP_REST_Response;
18 20
@@ -21,32 +23,30 @@
21 23 */
22 24 class Payment_REST_Controller extends REST_Controller {
23 25
24 26 /**
25 - * Payment method service
26 - *
27 - * @var Interface_Payment_Method_Service
28 - */
29 - private $payment_method_service;
30 -
31 - /**
32 27 * Constructor.
33 28 *
34 29 * @param string $rest_namespace The namespace.
35 30 * @param Interface_Logger_Service $logger The logger service.
36 - * @param Interface_Payment_Method_Service $payment_method_service The payment method service.
31 + * @param RegexProvider $regex The regex provider.
37 32 */
38 - public function __construct( $rest_namespace, Interface_Logger_Service $logger, Interface_Payment_Method_Service $payment_method_service ) {
39 - parent::__construct( $logger );
40 - $this->namespace = $rest_namespace;
41 - $this->rest_base = '/payment';
42 - $this->payment_method_service = $payment_method_service;
33 + public function __construct(
34 + $rest_namespace,
35 + Interface_Logger_Service $logger,
36 + RegexProvider $regex
37 + ) {
38 + parent::__construct( $logger, $regex );
39 + $this->namespace = $rest_namespace;
40 + $this->rest_base = '/payment';
43 41 }
44 42
45 43 /**
46 44 * Register the API endpoints.
45 + *
46 + * @return void
47 47 */
48 - public function register_routes(): void {
48 + public function register_routes() {
49 49 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
50 50 $data_store_id = array( self::PARAM_STORE_ID => $this->get_arg_string() );
51 51 $data_methods = array_merge(
52 52 $data_store_id,
@@ -67,19 +67,18 @@
67 67 *
68 68 * @return WP_REST_Response|WP_Error
69 69 */
70 70 public function get_methods( WP_REST_Request $request ) {
71 - $response = null;
72 - try {
73 - $response = $this->payment_method_service->get_all_payment_methods(
74 - strval( $request->get_param( self::PARAM_STORE_ID ) ),
75 - strval( $request->get_param( self::PARAM_MERCHANT_ID ) )
76 - );
77 - } catch ( \Throwable $e ) {
78 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
79 - $response = new WP_Error( 'error', $e->getMessage() );
80 - }
81 - return rest_ensure_response( $response );
71 + /**
72 + * Response
73 + *
74 + * @var Response $response */
75 + $response = AdminAPI::get()
76 + ->paymentMethods( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
77 + ->getPaymentMethods(
78 + new GetPaymentMethodsRequest( strval( $request->get_param( self::PARAM_MERCHANT_ID ) ), true )
79 + );
80 + return $this->build_response( $response );
82 81 }
83 82
84 83 /**
85 84 * Get all payment methods.
@@ -88,44 +87,16 @@
88 87 *
89 88 * @return WP_REST_Response|WP_Error
90 89 */
91 90 public function get_all_methods( WP_REST_Request $request ) {
92 - $response = array();
93 - try {
94 - $store_id = strval( $request->get_param( self::PARAM_STORE_ID ) );
95 -
96 - $countries = AdminAPI::get()
97 - ->countryConfiguration( $store_id )
98 - ->getCountryConfigurations()
99 - ->toArray();
100 -
101 - foreach ( $countries as $country ) {
102 - if ( ! isset( $country['merchantId'] ) ) {
103 - $this->logger->log_debug( 'Merchant ID not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ) ) );
104 - continue;
105 - }
106 -
107 - $payment_methods = $this->payment_method_service->get_all_payment_methods( $store_id, strval( $country['merchantId'] ) );
108 -
109 - foreach ( $payment_methods as $payment_method ) {
110 - if ( ! isset( $country['countryCode'], $payment_method['product'], $payment_method['title'] ) ) {
111 - $this->logger->log_debug( 'Required properties not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ), new LogContextData( 'paymentMethod', $payment_method ) ) );
112 - continue;
113 - }
114 -
115 - $response[] = array(
116 - 'countryCode' => $country['countryCode'],
117 - 'product' => $payment_method['product'],
118 - 'title' => $payment_method['title'],
119 - 'campaign' => $payment_method['campaign'] ?? null,
120 - 'supportsWidgets' => $payment_method['supportsWidgets'],
121 - 'supportsInstallmentPayments' => $payment_method['supportsInstallmentPayments'],
122 - );
123 - }
124 - }
125 - } catch ( \Throwable $e ) {
126 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
127 - $response = new WP_Error( 'error', $e->getMessage() );
128 - }
129 - return rest_ensure_response( $response );
91 + /**
92 + * Response
93 + *
94 + * @var Response $response */
95 + $response = AdminAPI::get()
96 + ->paymentMethods( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
97 + ->getAllAvailablePaymentMethods(
98 + new GetFormattedPaymentMethodsRequest( true )
99 + );
100 + return $this->build_response( $response );
130 101 }
131 102 }