Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion includes/class-wc-braspag-order-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function wc_gateway_braspag_pagador_status_update_void($order_id) {
$order = wc_get_order( $order_id );

if (!preg_match("#braspag#is", $order->get_payment_method())
|| $order->get_meta( '_braspag_charge_cancelled') === 'yes'
|| $order->get_meta( '_braspag_charge_refunded') === 'yes')
{
return $this;
Expand Down Expand Up @@ -137,7 +138,9 @@ public function wc_gateway_braspag_pagador_status_update_refund($order_id) {

$order = wc_get_order( $order_id );

if (!preg_match("#braspag#is", $order->get_payment_method())) {
if (!preg_match("#braspag#is", $order->get_payment_method())
|| $order->get_meta( '_braspag_charge_cancelled') === 'yes'
|| $order->get_meta( '_braspag_charge_refunded') === 'yes') {
return $this;
}

Expand Down
19 changes: 15 additions & 4 deletions includes/class-wc-braspag-webhook-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ public function process_change_type_status_update_response($response, $order) {
switch ( $response->body->Payment->Status) {

case '2': #PaymentConfirmed
$order->payment_complete($response->body->Payment->PaymentId);

/* translators: transaction id */
$message = sprintf( __( 'Post Notification Message: Braspag charge complete (Charge ID: %s)', 'woocommerce-braspag' ), $response->body->Payment->PaymentId);
$order->add_order_note( $message );
$order->add_meta_data( '_braspag_charge_captured', 'yes', true);
$order->update_status( 'processing', sprintf( __( 'Post Notification Message: Braspag charge Payment Captured (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId));
break;

case '1': #Authorized

$order->add_meta_data( '_braspag_charge_authorized', 'yes', true);

if ( $order->has_status( array( 'pending' ) ) ) {
WC_Braspag_Helper::is_wc_lt( '3.0' ) ? $order->reduce_order_stock() : wc_reduce_stock_levels( $order->get_id() );
}
Expand All @@ -162,24 +163,34 @@ public function process_change_type_status_update_response($response, $order) {
break;

case '0': #NotFinished

$order->add_meta_data( '_braspag_charge_failed', 'yes', true);
$order->update_status( 'failed', sprintf( __( 'Post Notification Message: Braspag charge Payment Not Finished (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId));
break;
case '12': #Pending

$order->add_meta_data( '_braspag_charge_pending', 'yes', true);
$order->update_status( 'pending', sprintf( __( 'Post Notification Message: Braspag charge Payment Pending (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId));
break;
case '20': #Scheduled

$order->add_meta_data( '_braspag_charge_pending', 'yes', true);
$order->update_status( 'pending', sprintf( __( 'Post Notification Message: Braspag charge Payment Scheduled (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId));
break;

case '3': #Denied
case '10': #Voided
case '13': #Aborted
#cancel

$order->add_meta_data( '_braspag_charge_cancelled', 'yes', true);
$order->update_status( 'cancelled', sprintf( __( 'Braspag charge payment Cancelled (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId));
break;

case '11': #Refunded
#refund

$order->add_meta_data( '_braspag_charge_refunded', 'yes', true);
$order->update_status( 'refunded', sprintf( __( 'Braspag charge refunded (Charge ID: %s).', 'woocommerce-braspag' ), $response->body->Payment->PaymentId) );
break;

Expand Down
44 changes: 38 additions & 6 deletions includes/class-wc-gateway-braspag.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,31 @@ public function get_braspag_payment_provider($card_type, $testMode) {
*/
public function get_customer_identity_data($order) {

if ( '' === $order->get_meta( '_billing_persontype' ) ) {
if ( '' === $order->get_meta( '_billing_persontype' )
&& '' === $order->get_meta( '_billing_cpf' )
&& '' === $order->get_meta( '_billing_cnpj' )) {
return '';
}

$customer_identity_type = $order->get_meta('_billing_persontype') == '1' ? 'CPF' : 'CNPJ';
$customer_identity_type = '';
$identityValue = '';

if ( '' !== $order->get_meta( '_billing_persontype' )) {
$customer_identity_type = $order->get_meta('_billing_persontype') == '1' ? 'CPF' : 'CNPJ';
$identityValue = preg_replace('/\D+/', '', $customer_identity_type == 'CPF' ? $order->get_meta('_billing_cpf') : $order->get_meta('_billing_cnpj'));

} elseif ('' !== $order->get_meta( '_billing_cpf' )) {
$customer_identity_type = 'CPF';
$identityValue = preg_replace('/\D+/', '', $order->get_meta('_billing_cpf'));

} elseif ('' !== $order->get_meta( '_billing_cnpj' )) {
$customer_identity_type = 'CNPJ';
$identityValue = preg_replace('/\D+/', '', $order->get_meta('_billing_cnpj'));
}

return [
'type' => $customer_identity_type,
'value' => preg_replace('/\D+/', '', $customer_identity_type == 'CPF' ? $order->get_meta('_billing_cpf') : $order->get_meta('_billing_cnpj'))
'value' => $identityValue
];
}

Expand Down Expand Up @@ -410,7 +427,7 @@ public function get_braspag_pagador_request_customer_data($order){
return [
"Name" => $order->get_formatted_billing_full_name(),
"Email" => $order->get_billing_email(),
"Phone" => preg_replace('/\D+/', '', $order->get_billing_phone()),
"Phone" => $this->get_customer_phone_data($order),
"Identity" => preg_replace('/\D+/', '', $customer_identity_data['value']),
"IdentityType" => $customer_identity_data['type'],
"Address" => [
Expand All @@ -420,7 +437,7 @@ public function get_braspag_pagador_request_customer_data($order){
"ZipCode" => $billing_address['postcode'],
"City" => $billing_address['city'],
"State" => $billing_address['state'],
"Country" => $billing_address['country'] == 'BR' ? 'BRA' : '',
"Country" => $billing_address['country'] == 'BR' ? 'BRA' : $billing_address['country'],
"District" => $billing_address['neighborhood']
],
"DeliveryAddress" => [
Expand All @@ -430,12 +447,27 @@ public function get_braspag_pagador_request_customer_data($order){
"ZipCode" => $shipping_address['postcode'],
"City" => $shipping_address['city'],
"State" => $shipping_address['state'],
"Country" => $shipping_address['country'] == 'BR' ? 'BRA' : '',
"Country" => $shipping_address['country'] == 'BR' ? 'BRA' : $shipping_address['country'],
"District" => $shipping_address['neighborhood']
]
];
}

public function get_customer_phone_data($order) {

$phone_prefix = '';

$billing_address = $order->get_address('billing');
switch ($billing_address['country']) {
case 'BR':
case 'BRA':
$phone_prefix = '55';
break;
}

return $phone_prefix.preg_replace('/\D+/', '', $order->get_billing_phone());
}

/**
* @param $payment_method
* @param $order
Expand Down
Loading