Created
March 15, 2024 04:39
-
-
Save luiseduardobraschi/f66145a03a5276fc4bdc823abe2e96e4 to your computer and use it in GitHub Desktop.
[Fórum WP] Validação fingerprint postback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( !class_exists( 'WCPagarmePixPayment\Pagarme\PagarmeApiV4' ) ){ | |
return; | |
} | |
use WCPagarmePixPayment\Pagarme\PagarmeApiV4; | |
class ArtiPagarmeApi extends PagarmeApiV4 { | |
public function ipn_handler() { | |
@ob_clean(); | |
$ipn_response = file_get_contents('php://input'); | |
if ($this->gateway->is_debug()) { | |
$this->gateway->log->add($this->gateway->id, 'Retornou um POSTBACK'); | |
$this->gateway->log->add($this->gateway->id, 'Response' . print_r($ipn_response, true)); | |
} | |
if ($ipn_response && $this->check_fingerprint($ipn_response)) { | |
header('HTTP/1.1 200 OK'); | |
parse_str( $ipn_response, $parsed_ipn_response ); | |
wc_get_logger()->add( 'arti', print_r( $parsed_ipn_response, true ) ); | |
$this->process_successful_ipn($parsed_ipn_response); | |
exit; | |
} else { | |
wp_die(esc_html__('Pagar.me PIX Request Failure', 'wc-pagarme-pix-payment'), '', array('response' => 401)); | |
} | |
} | |
public function check_fingerprint( $ipn_response ) { | |
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE']; | |
$parts = explode('=', $signature, 2); | |
if (count($parts) != 2) { | |
return false; | |
} | |
$api_key = $this->gateway->api_key; | |
$hash = hash_hmac($parts[0], $ipn_response, $api_key); | |
return $hash === $parts[1]; | |
} | |
} | |
add_filter( 'wc_payment_gateways_initialized', function( $wc_gateways ){ | |
foreach( $wc_gateways->payment_gateways as $gateway ){ | |
if( is_a( $gateway, 'WCPagarmePixPayment\Gateway\PagarmePixGateway' ) ){ | |
$gateway->api = new ArtiPagarmeApi( $gateway ); | |
break; | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment