Skip to content

Commit

Permalink
webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseHCalderon authored Sep 2, 2023
1 parent 2396d5d commit 68fc193
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public function update_order( $culqi_order ) {
* [old_update_order description]
* @return [type] [description]
*/
/*
public function old_update_order() {
$inputJSON = file_get_contents('php://input');
Expand All @@ -212,7 +213,7 @@ public function old_update_order() {
echo wp_send_json( ['result' => 'success' ] );
die();
}

*/
/**
* Notice Currency
* @return html
Expand Down
55 changes: 41 additions & 14 deletions includes/class-fullculqi-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ public function to_receive() {
$headers = getallheaders();
$headers = $headers['Authorization'];
if(!isset($headers)){
exit("Error: Cabecera Authorization no presente");
$this->set_json_response("Error", "Cabecera Authorization no presente", 400);
//exit("Error: Cabecera Authorization no presente");
}
$authorization = substr($headers,6);
$credenciales = base64_decode($authorization);
$credenciales = explode( ':', $credenciales );
$username = $credenciales[0];
$password = $credenciales[1];
if(!isset($username) or !isset($password)){
exit("Error: No Autorizado");
$this->set_json_response("error", "No Autorizado", 401);
//exit("Error: No Autorizado");
}
if( empty( $inputJSON ) )
return;
Expand All @@ -45,7 +47,8 @@ public function to_receive() {
$password_bd = $settings['password'];

if( $username != $username_bd || $password != $password_bd ){
exit("Error: Crendenciales Incorrectas");
$this->set_json_response("error", "Crendenciales Incorrectas", 400);
//exit("Error: Crendenciales Incorrectas");
}

if( $input->object != 'event' )
Expand All @@ -54,11 +57,13 @@ public function to_receive() {
$data = json_decode( $input->data );

if (empty($data->metadata)) {
exit("Error: Metadata vacia");
$this->set_json_response("Error", "Metadata vacia", 400);
//exit("Error: Metadata vacia");
}

if (empty($data->amount) && empty($data->actualAmount)) {
exit("Error: No envió el amount");
$this->set_json_response("error", "No envió el amount", 400);
//exit("Error: No envió el amount");
}

// Webhook History
Expand All @@ -67,14 +72,16 @@ public function to_receive() {
switch( $input->type ) {
case 'order.status.changed' :
if (empty($data->id) || empty($data->order_number) || empty($data->currency_code) || empty($data->state)) {
exit("Error: order_id, order_number, currency_code o state vacios");
$this->set_json_response("error", "order_id, order_number, currency_code o state vacios", 400);
//exit("Error: order_id, order_number, currency_code o state vacios");
}
FullCulqi_Orders::update($data, 1);
break;

case 'refund.creation.succeeded' :
if (empty($data->chargeId)) {
exit("Error: No envió el chargeId");
$this->set_json_response("error", "No envió el chargeId", 400);
//exit("Error: No envió el chargeId");
}
$order_id = fullculqi_post_from_meta('_culqi_charge_id', $data->chargeId);
$charge_id = fullculqi_post_from_meta('culqi_id', $data->chargeId);
Expand Down Expand Up @@ -121,28 +128,34 @@ public function to_receive() {
$amount = $order->get_total() * 100;
if($currency == $data->currency && $amount == $data->actualAmount) {
FullCulqi_Charges::create( $data , true);
die("Cargo actualizado con éxito");
$this->set_json_response("success", "Cargo actualizado con éxito", 200);
//die("Cargo actualizado con éxito");
break;
}
die("La moneda o monto no coinciden con la orden.");
$this->set_json_response("error", "La moneda o monto no coinciden con la orden", 400);
//die("La moneda o monto no coinciden con la orden.");
break;
}
die($verifyCharge);
$this->set_json_response("error", $verifyCharge, 400);
//die($verifyCharge);
break;
}
die("No se puede actualizar, la orden no esta pendiente pago.");
$this->set_json_response("error", "No se puede actualizar, la orden no esta pendiente pago", 400);
//die("No se puede actualizar, la orden no esta pendiente pago.");
break;
}
die("El método de pago usado en la orden no es Culqi.");
$this->set_json_response("error", "El método de pago usado en la orden no es Culqi", 400);
//die("El método de pago usado en la orden no es Culqi.");
break;
}

die("No existe la orden");
$this->set_json_response("error", "No existe la orden", 400);
//die("No existe la orden");
break;

}

do_action( 'fullculqi/webhooks/to_receive', $input, $data );
$this->set_json_response("success", "Operación satisfactoria", 200);
}


Expand Down Expand Up @@ -205,6 +218,20 @@ private function verifyChargeInOrders($charge_id, $current_order_id)

return false;
}

public function set_json_response($result, $msg, $statusCode) {
if($statusCode===200){
return wp_send_json_success(
['result' => $result,
'message' => $msg ],
$statusCode );
}else{
return wp_send_json_error(
['result' => $result,
'message' => $msg ],
$statusCode );
}
}

}

Expand Down

0 comments on commit 68fc193

Please sign in to comment.