PluginProbe
seQura / 3.0.5
seQura v3.0.5
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.5, at src/Controllers/Rest/class-payment-rest-controller.php

134 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 * @return void
49 */
50 public function register_routes() {
51 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
52 $data_store_id = array( self::PARAM_STORE_ID => $this->get_arg_string() );
53 $data_methods = array_merge(
54 $data_store_id,
55 array( self::PARAM_MERCHANT_ID => $this->get_arg_string() )
56 );
57
58 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
59 $merchant_id = $this->url_param_pattern( self::PARAM_MERCHANT_ID );
60
61 $this->register_get( "methods/{$store_id}/{$merchant_id}", 'get_methods', $data_methods );
62 $this->register_get( "methods/{$store_id}", 'get_all_methods', $data_store_id );
63 }
64
65 /**
66 * Get payment methods.
67 *
68 * @param WP_REST_Request $request The request.
69 *
70 * @return WP_REST_Response|WP_Error
71 */
72 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 );
79 } catch ( \Throwable $e ) {
80 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
81 $response = new WP_Error( 'error', $e->getMessage() );
82 }
83 return rest_ensure_response( $response );
84 }
85
86 /**
87 * Get all payment methods.
88 *
89 * @param WP_REST_Request $request The request.
90 *
91 * @return WP_REST_Response|WP_Error
92 */
93 public function get_all_methods( WP_REST_Request $request ) {
94 $response = array();
95 try {
96 $store_id = strval( $request->get_param( self::PARAM_STORE_ID ) );
97
98 $countries = AdminAPI::get()
99 ->countryConfiguration( $store_id )
100 ->getCountryConfigurations()
101 ->toArray();
102
103 foreach ( $countries as $country ) {
104 if ( ! isset( $country['merchantId'] ) ) {
105 $this->logger->log_debug( 'Merchant ID not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ) ) );
106 continue;
107 }
108
109 $payment_methods = $this->payment_method_service->get_all_payment_methods( $store_id, strval( $country['merchantId'] ) );
110
111 foreach ( $payment_methods as $payment_method ) {
112 if ( ! isset( $country['countryCode'], $payment_method['product'], $payment_method['title'] ) ) {
113 $this->logger->log_debug( 'Required properties not found', __FUNCTION__, __CLASS__, array( new LogContextData( 'countryConfiguration', $country ), new LogContextData( 'paymentMethod', $payment_method ) ) );
114 continue;
115 }
116
117 $response[] = array(
118 'countryCode' => $country['countryCode'],
119 'product' => $payment_method['product'],
120 'title' => $payment_method['title'],
121 'campaign' => $payment_method['campaign'] ?? null,
122 'supportsWidgets' => $payment_method['supportsWidgets'],
123 'supportsInstallmentPayments' => $payment_method['supportsInstallmentPayments'],
124 );
125 }
126 }
127 } catch ( \Throwable $e ) {
128 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
129 $response = new WP_Error( 'error', $e->getMessage() );
130 }
131 return rest_ensure_response( $response );
132 }
133 }
134