| 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 |
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 ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get all payment methods. |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request The request. |
| 91 |
* |
| 92 |
* @return WP_REST_Response|WP_Error |
| 93 |
*/ |
| 94 |
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 ); |
| 133 |
} |
| 134 |
} |
| 135 |
|