PluginProbe
seQura / 3.0.2
seQura v3.0.2
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
sequra / src / Controllers / Rest / class-payment-rest-controller.php

class-payment-rest-controller.php in seQura 3.0.2, at src/Controllers/Rest/class-payment-rest-controller.php

132 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST Payment Controller
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Controllers/Rest
7 */
8
9 namespace SeQura\WC\Controllers\Rest;
10
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;
15 use WP_Error;
16 use WP_REST_Request;
17 use WP_REST_Response;
18
19 /**
20 * REST Payment Controller
21 */
22 class Payment_REST_Controller extends REST_Controller {
23
24 /**
25 * Payment method service
26 *
27 * @var Interface_Payment_Method_Service
28 */
29 private $payment_method_service;
30
31 /**
32 * Constructor.
33 *
34 * @param string $rest_namespace The namespace.
35 * @param Interface_Logger_Service $logger The logger service.
36 * @param Interface_Payment_Method_Service $payment_method_service The payment method service.
37 */
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;
43 }
44
45 /**
46 * Register the API endpoints.
47 */
48 public function register_routes(): void {
49 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
50 $data_store_id = array( self::PARAM_STORE_ID => $this->get_arg_string() );
51 $data_methods = array_merge(
52 $data_store_id,
53 array( self::PARAM_MERCHANT_ID => $this->get_arg_string() )
54 );
55
56 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
57 $merchant_id = $this->url_param_pattern( self::PARAM_MERCHANT_ID );
58
59 $this->register_get( "methods/{$store_id}/{$merchant_id}", 'get_methods', $data_methods );
60 $this->register_get( "methods/{$store_id}", 'get_all_methods', $data_store_id );
61 }
62
63 /**
64 * Get payment methods.
65 *
66 * @param WP_REST_Request $request The request.
67 *
68 * @return WP_REST_Response|WP_Error
69 */
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 );
82 }
83
84 /**
85 * Get all payment methods.
86 *
87 * @param WP_REST_Request $request The request.
88 *
89 * @return WP_REST_Response|WP_Error
90 */
91 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 );
130 }
131 }
132