Skip to content

Commit

Permalink
Update rest response callback method names
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdiyazdani committed Aug 3, 2023
1 parent bc544fe commit c471130
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/Paddle/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait Events {
*
* @return void
*/
private function subscription_created( $webhook_data ) {
private function event_subscription_created( $webhook_data ) {

// Retrieve the order object.
$order = $this->get_order( $webhook_data );
Expand All @@ -54,7 +54,7 @@ private function subscription_created( $webhook_data ) {
*
* @return void
*/
private function subscription_cancelled( $webhook_data ) {
private function event_subscription_cancelled( $webhook_data ) {

// Retrieve the order object.
$order = $this->get_order( $webhook_data );
Expand All @@ -80,7 +80,7 @@ private function subscription_cancelled( $webhook_data ) {
*
* @return void
*/
private function subscription_payment_refunded( $webhook_data ) {
private function event_subscription_payment_refunded( $webhook_data ) {

// Retrieve the order object.
$order = $this->get_order( $webhook_data );
Expand All @@ -106,7 +106,7 @@ private function subscription_payment_refunded( $webhook_data ) {
*
* @return void
*/
private function subscription_payment_succeeded( $webhook_data ) {
private function event_subscription_payment_succeeded( $webhook_data ) {

// Retrieve the order object.
$order = $this->get_order( $webhook_data );
Expand Down Expand Up @@ -147,7 +147,7 @@ private function subscription_payment_succeeded( $webhook_data ) {
*
* @return void
*/
private function subscription_payment_failed( $webhook_data ) {
private function event_subscription_payment_failed( $webhook_data ) {

// Retrieve the order object.
$order = $this->get_order( $webhook_data );
Expand Down
71 changes: 26 additions & 45 deletions src/Paddle/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Woo_Paddle_Gateway\Paddle;

use WP_Error;
use WP_REST_Server;
use WP_REST_Response;

Expand Down Expand Up @@ -66,7 +65,7 @@ public function register_listener() {

register_rest_route(
self::NAMESPACE,
'/' . self::ROUTE,
self::ROUTE,
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'dispatch_webhook_payload' ),
Expand All @@ -81,7 +80,7 @@ public function register_listener() {
*
* @since 1.0.0
*
* @return WP_REST_Response|WP_Error
* @return WP_REST_Response
*/
public function dispatch_webhook_payload() {

Expand All @@ -90,13 +89,10 @@ public function dispatch_webhook_payload() {

// Check if the public key is set.
if ( empty( $saved_keys->public_key ) ) {
return new WP_Error(
'woo_paddle_gateway_rest_cannot_view',
__( 'Paddle public key is not set.', 'woo-paddle-gateway' ),
array(
'status' => rest_authorization_required_code(),
)
);
$response = new WP_REST_Response( array( 'error' => 'Paddle public key is not set.' ) );
$response->set_status( 400 );

return $response;
}

// Retrieve the webhook data.
Expand All @@ -111,13 +107,10 @@ public function dispatch_webhook_payload() {

// Check if the webhook signature is set.
if ( empty( $webhook_data['p_signature'] ) ) {
return new WP_Error(
'woo_paddle_gateway_rest_cannot_view',
__( 'Paddle webhook signature is not set.', 'woo-paddle-gateway' ),
array(
'status' => rest_authorization_required_code(),
)
);
$response = new WP_REST_Response( array( 'error' => 'Paddle webhook signature is not set.' ) );
$response->set_status( 400 );

return $response;
}

// Pop the signature from the webhook data.
Expand All @@ -133,50 +126,38 @@ public function dispatch_webhook_payload() {
$verification = openssl_verify( $data, $signature, $saved_keys->public_key, OPENSSL_ALGO_SHA1 );

if ( 1 !== $verification ) {
return new WP_Error(
'woo_paddle_gateway_rest_cannot_view',
__( 'Paddle webhook signature verification failed.', 'woo-paddle-gateway' ),
array(
'status' => rest_authorization_required_code(),
)
);
$response = new WP_REST_Response( array( 'error' => 'Paddle webhook signature verification failed.' ) );
$response->set_status( 400 );

return $response;
}

// Check if the alert name is set.
if ( empty( $webhook_data['alert_name'] ) ) {
return new WP_Error(
'woo_paddle_gateway_rest_cannot_view',
__( 'Alert name is not set.', 'woo-paddle-gateway' ),
array(
'status' => rest_authorization_required_code(),
)
);
$response = new WP_REST_Response( array( 'error' => 'Alert name is not set.' ) );
$response->set_status( 400 );

return $response;
}

// Retrieve the alert name.
$alert_name = $webhook_data['alert_name'];

// Check if the alert name is valid and if the corresponding method exists.
if ( ! method_exists( $this, $alert_name ) ) {
return new WP_Error(
'woo_paddle_gateway_rest_cannot_view',
__( 'Alert name is not valid.', 'woo-paddle-gateway' ),
array(
'status' => rest_authorization_required_code(),
)
);
if ( ! method_exists( $this, "event_{$alert_name}" ) ) {
$response = new WP_REST_Response( array( 'error' => 'Alert name is not valid.' ) );
$response->set_status( 400 );

return $response;
}

// Call the appropriate method based on the alert name.
call_user_func( array( $this, $alert_name ), $webhook_data );
call_user_func( array( $this, "event_{$alert_name}" ), $webhook_data );

// Return a success response.
$response = rest_ensure_response(
array(
'success' => true,
)
);
$response = new WP_REST_Response( array( 'success' => true ) );

// Set the success status code.
$response->set_status( 200 );

return $response;
Expand Down

0 comments on commit c471130

Please sign in to comment.