route( '/admin/payments/stripe', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'stripe' ), 'permission' => $settings, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_stripe' ), 'permission' => $settings, ), ) ); $this->route( '/admin/payments/paypal', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'paypal' ), 'permission' => $settings, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_paypal' ), 'permission' => $settings, ), ) ); register_rest_route( self::NAMESPACE, '/stripe/webhook', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'stripe_webhook' ), // Stripe is not a WordPress user; the signature is checked in the callback. 'permission_callback' => '__return_true', ) ); } /** * Stripe status. * * @return array */ public function stripe(): array { $stripe = Settings::stripe(); $key = $stripe['secret_key']; return array( 'connected' => StripeMethod::valid_secret_key( $key ), 'mode' => str_contains( $key, '_live_' ) ? 'live' : ( '' === $key ? '' : 'test' ), 'key_hint' => '' === $key ? '' : substr( $key, -4 ), 'publishable_key' => $stripe['publishable_key'], 'webhook_secret_set' => '' !== $stripe['webhook_secret'], 'webhook_url' => rest_url( self::NAMESPACE . '/stripe/webhook' ), ); } /** * Save Stripe keys that were sent; an empty string removes one. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When a key is malformed or refused. */ public function save_stripe( WP_REST_Request $request ): array { $stripe = Settings::stripe(); $body = self::body( $request ); if ( isset( $body['secret_key'] ) ) { $key = trim( (string) $body['secret_key'] ); if ( '' !== $key ) { if ( ! StripeMethod::valid_secret_key( $key ) ) { throw new InvalidArgumentException( __( 'That is not a Stripe secret key. It starts with sk_live_ or sk_test_.', 'schedulereed' ) ); } try { ( new StripeClient( $key ) )->get( 'balance' ); } catch ( PaymentException $e ) { /* translators: %s: Stripe's error message. */ throw new InvalidArgumentException( sprintf( __( 'Stripe did not accept that key: %s', 'schedulereed' ), $e->getMessage() ) ); } } $stripe['secret_key'] = $key; } if ( isset( $body['publishable_key'] ) ) { $key = trim( (string) $body['publishable_key'] ); if ( '' !== $key && 1 !== preg_match( '/^pk_(test|live)_[A-Za-z0-9]+$/', $key ) ) { throw new InvalidArgumentException( __( 'That is not a Stripe publishable key. It starts with pk_live_ or pk_test_.', 'schedulereed' ) ); } $stripe['publishable_key'] = $key; } if ( isset( $body['webhook_secret'] ) ) { $secret = trim( (string) $body['webhook_secret'] ); if ( '' !== $secret && 1 !== preg_match( '/^whsec_[A-Za-z0-9]+$/', $secret ) ) { throw new InvalidArgumentException( __( 'That is not a webhook signing secret. It starts with whsec_.', 'schedulereed' ) ); } $stripe['webhook_secret'] = $secret; } update_option( Settings::STRIPE, $stripe, false ); $this->enable( 'stripe', '' !== $stripe['secret_key'] ); return $this->stripe(); } /** * PayPal status. * * @return array */ public function paypal(): array { $paypal = Settings::paypal(); return array( 'connected' => '' !== $paypal['client_id'] && '' !== $paypal['secret'], 'mode' => $paypal['mode'], 'client_id' => $paypal['client_id'], ); } /** * Save PayPal credentials after PayPal accepts them; empty values disconnect. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When PayPal refuses them. */ public function save_paypal( WP_REST_Request $request ): array { $body = self::body( $request ); $paypal = array( 'client_id' => trim( (string) ( $body['client_id'] ?? '' ) ), 'secret' => trim( (string) ( $body['secret'] ?? '' ) ), 'mode' => 'sandbox' === ( $body['mode'] ?? '' ) ? 'sandbox' : 'live', ); if ( '' !== $paypal['client_id'] || '' !== $paypal['secret'] ) { try { ( new PayPalClient( $paypal['client_id'], $paypal['secret'], $paypal['mode'] ) )->access_token(); } catch ( PaymentException $e ) { /* translators: %s: PayPal's error message. */ throw new InvalidArgumentException( sprintf( __( 'PayPal did not accept those credentials: %s', 'schedulereed' ), $e->getMessage() ) ); } } update_option( Settings::PAYPAL, $paypal, false ); $this->enable( 'paypal', '' !== $paypal['client_id'] ); return $this->paypal(); } /** * Handle an event from Stripe. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function stripe_webhook( WP_REST_Request $request ) { $payload = $request->get_body(); if ( ! WebhookSignature::verify( $payload, (string) $request->get_header( 'stripe_signature' ), Settings::stripe()['webhook_secret'], Clock::now() ) ) { return new WP_Error( 'schedulereed_invalid_signature', 'Invalid signature.', array( 'status' => 400 ) ); } $event = json_decode( $payload, true ); if ( ! is_array( $event ) ) { return rest_ensure_response( array( 'received' => true ) ); } /** * Fires for every Stripe event whose signature checked out. * * @param array $event The event. */ do_action( 'schedulereed_stripe_event', $event ); $session = $event['data']['object'] ?? null; if ( ! in_array( $event['type'] ?? '', self::PAID_EVENTS, true ) || ! is_array( $session ) ) { return rest_ensure_response( array( 'received' => true ) ); } $uid = (string) ( $session['client_reference_id'] ?? '' ); $payment = StripeMethod::payment_from_session( $session, $uid ); if ( null !== $payment ) { try { Services::payment_flow()->paid( $uid, $payment['amount'], StripeMethod::ID, $payment['transaction_id'] ); } catch ( BookingException ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Not one of ours, e.g. another site on the same Stripe account. } } return rest_ensure_response( array( 'received' => true ) ); } /** * Switch a method on when it is connected, off when disconnected. * * @param string $id Method. * @param bool $on On. */ private function enable( string $id, bool $on ): void { $methods = Settings::payment_methods(); $methods = $on ? array_values( array_unique( array_merge( $methods, array( $id ) ) ) ) : array_values( array_diff( $methods, array( $id ) ) ); update_option( Settings::PAYMENT_METHODS, $methods ); } }