swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Stripe_Payment_Gateway_Controller |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * Dynamic REST controller for payment gateway settings. |
| 10 | */ |
| 11 | class WC_REST_Stripe_Payment_Gateway_Controller extends WC_Stripe_REST_Base_Controller { |
| 12 | |
| 13 | /** |
| 14 | * Endpoint path. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $rest_base = 'wc_stripe/payment-gateway'; |
| 19 | |
| 20 | /** |
| 21 | * Stripe payment gateway. |
| 22 | * |
| 23 | * @var WC_Gateway_Stripe |
| 24 | */ |
| 25 | private $gateway; |
| 26 | |
| 27 | /** |
| 28 | * Gateway match array. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | private $gateways = [ |
| 33 | 'stripe_sepa' => WC_Gateway_Stripe_Sepa::class, |
| 34 | 'stripe_giropay' => WC_Gateway_Stripe_Giropay::class, |
| 35 | 'stripe_ideal' => WC_Gateway_Stripe_Ideal::class, |
| 36 | 'stripe_bancontact' => WC_Gateway_Stripe_Bancontact::class, |
| 37 | 'stripe_eps' => WC_Gateway_Stripe_Eps::class, |
| 38 | 'stripe_sofort' => WC_Gateway_Stripe_Sofort::class, |
| 39 | 'stripe_p24' => WC_Gateway_Stripe_P24::class, |
| 40 | 'stripe_alipay' => WC_Gateway_Stripe_Alipay::class, |
| 41 | 'stripe_multibanco' => WC_Gateway_Stripe_Multibanco::class, |
| 42 | 'stripe_oxxo' => WC_Gateway_Stripe_Oxxo::class, |
| 43 | 'stripe_boleto' => WC_Gateway_Stripe_Boleto::class, |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * Returns an instance of some WC_Gateway_Stripe. |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | private function instantiate_gateway( $gateway_id ) { |
| 52 | $this->gateway = new $this->gateways[ $gateway_id ](); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Configure REST API routes. |
| 57 | */ |
| 58 | public function register_routes() { |
| 59 | register_rest_route( |
| 60 | $this->namespace, |
| 61 | '/' . $this->rest_base . '/(?P<payment_gateway_id>[a-z0-9_]+)', |
| 62 | [ |
| 63 | 'methods' => WP_REST_Server::READABLE, |
| 64 | 'callback' => [ $this, 'get_payment_gateway_settings' ], |
| 65 | 'permission_callback' => [ $this, 'check_permission' ], |
| 66 | ] |
| 67 | ); |
| 68 | register_rest_route( |
| 69 | $this->namespace, |
| 70 | '/' . $this->rest_base . '/(?P<payment_gateway_id>[a-z0-9_]+)', |
| 71 | [ |
| 72 | 'methods' => WP_REST_Server::EDITABLE, |
| 73 | 'callback' => [ $this, 'update_payment_gateway_settings' ], |
| 74 | 'permission_callback' => [ $this, 'check_permission' ], |
| 75 | ] |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Retrieve payment gateway settings. |
| 81 | * |
| 82 | * @param WP_REST_Request $request |
| 83 | * @return WP_REST_Response |
| 84 | */ |
| 85 | public function get_payment_gateway_settings( $request = null ) { |
| 86 | try { |
| 87 | $id = $request->get_param( 'payment_gateway_id' ); |
| 88 | $this->instantiate_gateway( $id ); |
| 89 | $settings = [ |
| 90 | 'is_' . $id . '_enabled' => $this->gateway->is_enabled(), |
| 91 | $id . '_name' => $this->gateway->get_option( 'title' ), |
| 92 | $id . '_description' => $this->gateway->get_option( 'description' ), |
| 93 | ]; |
| 94 | if ( method_exists( $this->gateway, 'get_unique_settings' ) ) { |
| 95 | $settings = $this->gateway->get_unique_settings( $settings ); |
| 96 | } |
| 97 | return new WP_REST_Response( $settings ); |
| 98 | } catch ( Exception $exception ) { |
| 99 | return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Update payment gateway settings. |
| 105 | * |
| 106 | * @param WP_REST_Request $request Request object. |
| 107 | */ |
| 108 | public function update_payment_gateway_settings( WP_REST_Request $request ) { |
| 109 | try { |
| 110 | $id = $request->get_param( 'payment_gateway_id' ); |
| 111 | $this->instantiate_gateway( $id ); |
| 112 | $this->update_is_gateway_enabled( $request ); |
| 113 | $this->update_gateway_name( $request ); |
| 114 | $this->update_gateway_description( $request ); |
| 115 | if ( method_exists( $this->gateway, 'update_unique_settings' ) ) { |
| 116 | $this->gateway->update_unique_settings( $request ); |
| 117 | } |
| 118 | return new WP_REST_Response( [], 200 ); |
| 119 | } catch ( Exception $exception ) { |
| 120 | return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Updates payment gateway enabled status. |
| 126 | * |
| 127 | * @param WP_REST_Request $request Request object. |
| 128 | */ |
| 129 | private function update_is_gateway_enabled( WP_REST_Request $request ) { |
| 130 | $field_name = 'is_' . $this->gateway->id . '_enabled'; |
| 131 | $is_enabled = $request->get_param( $field_name ); |
| 132 | |
| 133 | if ( null === $is_enabled || ! is_bool( $is_enabled ) ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if ( $is_enabled ) { |
| 138 | $this->gateway->enable(); |
| 139 | } else { |
| 140 | $this->gateway->disable(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Updates payment gateway title. |
| 146 | * |
| 147 | * @param WP_REST_Request $request Request object. |
| 148 | */ |
| 149 | private function update_gateway_name( WP_REST_Request $request ) { |
| 150 | $field_name = $this->gateway->id . '_name'; |
| 151 | $name = $request->get_param( $field_name ); |
| 152 | |
| 153 | if ( null === $name ) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $value = sanitize_text_field( $name ); |
| 158 | $this->gateway->update_option( 'title', $value ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Updates payment gateway description. |
| 163 | * |
| 164 | * @param WP_REST_Request $request Request object. |
| 165 | */ |
| 166 | private function update_gateway_description( WP_REST_Request $request ) { |
| 167 | $field_name = $this->gateway->id . '_description'; |
| 168 | $description = $request->get_param( $field_name ); |
| 169 | |
| 170 | if ( null === $description ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | $value = sanitize_text_field( $description ); |
| 175 | $this->gateway->update_option( 'description', $value ); |
| 176 | } |
| 177 | } |