← All changes
|
src/Controllers/Rest/class-payment-rest-controller.php
+34
-66
3.2.0
→
4.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,26 +23,22 @@ | ||
| 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. |
| @@ -69,20 +67,18 @@ | ||
| 69 | 67 | * |
| 70 | 68 | * @return WP_REST_Response|WP_Error |
| 71 | 69 | */ |
| 72 | 70 | public function get_methods( WP_REST_Request $request ) { |
| 73 | - $response = null; | |
| 74 | - try { | |
| 75 | - $response = $this->payment_method_service->get_all_payment_methods( | |
| 76 | - strval( $request->get_param( self::PARAM_STORE_ID ) ), | |
| 77 | - strval( $request->get_param( self::PARAM_MERCHANT_ID ) ), | |
| 78 | - false | |
| 79 | - ); | |
| 80 | - } catch ( \Throwable $e ) { | |
| 81 | - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ ); | |
| 82 | - $response = new WP_Error( 'error', $e->getMessage() ); | |
| 83 | - } | |
| 84 | - 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 ); | |
| 85 | 81 | } |
| 86 | 82 | |
| 87 | 83 | /** |
| 88 | 84 | * Get all payment methods. |
| @@ -91,44 +87,16 @@ | ||
| 91 | 87 | * |
| 92 | 88 | * @return WP_REST_Response|WP_Error |
| 93 | 89 | */ |
| 94 | 90 | public function get_all_methods( WP_REST_Request $request ) { |
| 95 | - $response = array(); | |
| 96 | - try { | |
| 97 | - $store_id = strval( $request->get_param( self::PARAM_STORE_ID ) ); | |
| 98 | - | |
| 99 | - $countries = AdminAPI::get() | |
| 100 | - ->countryConfiguration( $store_id ) | |
| 101 | - ->getCountryConfigurations() | |
| 102 | - ->toArray(); | |
| 103 | - | |
| 104 | - foreach ( $countries as $country ) { | |
| 105 | - if ( ! isset( $country['merchantId'] ) ) { | |
| 106 | - $this->logger->log_debug( 'Merchant ID not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ) ) ); | |
| 107 | - continue; | |
| 108 | - } | |
| 109 | - | |
| 110 | - $payment_methods = $this->payment_method_service->get_all_payment_methods( $store_id, strval( $country['merchantId'] ) ); | |
| 111 | - | |
| 112 | - foreach ( $payment_methods as $payment_method ) { | |
| 113 | - if ( ! isset( $country['countryCode'], $payment_method['product'], $payment_method['title'] ) ) { | |
| 114 | - $this->logger->log_debug( 'Required properties not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ), new LogContextData( 'paymentMethod', $payment_method ) ) ); | |
| 115 | - continue; | |
| 116 | - } | |
| 117 | - | |
| 118 | - $response[] = array( | |
| 119 | - 'countryCode' => $country['countryCode'], | |
| 120 | - 'product' => $payment_method['product'], | |
| 121 | - 'title' => $payment_method['title'], | |
| 122 | - 'campaign' => $payment_method['campaign'] ?? null, | |
| 123 | - 'supportsWidgets' => $payment_method['supportsWidgets'], | |
| 124 | - 'supportsInstallmentPayments' => $payment_method['supportsInstallmentPayments'], | |
| 125 | - ); | |
| 126 | - } | |
| 127 | - } | |
| 128 | - } catch ( \Throwable $e ) { | |
| 129 | - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ ); | |
| 130 | - $response = new WP_Error( 'error', $e->getMessage() ); | |
| 131 | - } | |
| 132 | - 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 ); | |
| 133 | 101 | } |
| 134 | 102 | } |