Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
fix direct calls to order object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sfwill-dev committed Sep 17, 2020
1 parent e3de2e2 commit 1b829a0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
15 changes: 12 additions & 3 deletions includes/admin/views/html-notice-missing-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@
}

$is_installed = false;
$is_active = false;
$has_required_version = false;

if ( function_exists( 'get_plugins' ) ) {
$all_plugins = get_plugins();
$is_installed = ! empty( $all_plugins['woocommerce/woocommerce.php'] );
$is_active = is_plugin_active( 'woocommerce/woocommerce.php' );

if ( $is_installed && defined( 'WC_VERSION' ) ) {
// Minimum Woocommerce version required.
$has_required_version = version_compare( WC_VERSION, '3.0', '>=' );
}
}

?>

<div class="error">
<p><strong><?php esc_html_e( 'WooCommerce Pagar.me', 'woocommerce-pagarme' ); ?></strong> <?php esc_html_e( 'depends on the last version of WooCommerce to work!', 'woocommerce-pagarme' ); ?></p>

<?php if ( $is_installed && current_user_can( 'install_plugins' ) ) : ?>
<p><strong><?php esc_html_e( 'WooCommerce Pagar.me', 'woocommerce-pagarme' ); ?></strong> <?php esc_html_e( 'depends on the last version of WooCommerce to work!', 'woocommerce-pagarme' ); ?></p>
<?php if ( $is_installed && ! $is_active && current_user_can( 'install_plugins' ) ) : ?>
<p><a href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=active' ), 'activate-plugin_woocommerce/woocommerce.php' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Active WooCommerce', 'woocommerce-pagarme' ); ?></a></p>
<?php elseif ( $is_installed && ! $has_required_version && current_user_can( 'install_plugins' ) ) : ?>
<p><a href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=woocommerce/woocommerce.php' ), 'upgrade-plugin_woocommerce/woocommerce.php' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade WooCommerce', 'woocommerce-pagarme' ); ?></a></p>
<?php else : ?>
<?php if ( current_user_can( 'install_plugins' ) ) : ?>
<p><a href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=woocommerce' ), 'install-plugin_woocommerce' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Install WooCommerce', 'woocommerce-pagarme' ); ?></a></p>
Expand Down
33 changes: 17 additions & 16 deletions includes/class-wc-pagarme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,17 @@ public function generate_transaction_data( $order, $posted ) {
'amount' => $order->get_total() * 100,
'postback_url' => WC()->api_request_url( get_class( $this->gateway ) ),
'customer' => array(
'name' => trim( $order->billing_first_name . ' ' . $order->billing_last_name ),
'email' => $order->billing_email,
'name' => trim( $order->get_formatted_billing_full_name() ),
'email' => $order->get_billing_email(),
),
'metadata' => array(
'order_number' => $order->get_order_number(),
),
);

// Phone.
if ( ! empty( $order->billing_phone ) ) {
$phone = $this->only_numbers( $order->billing_phone );
if ( $order->get_billing_phone() ) {
$phone = $this->only_numbers( $order->get_billing_phone() );

$data['customer']['phone'] = array(
'ddd' => substr( $phone, 0, 2 ),
Expand All @@ -265,11 +265,11 @@ public function generate_transaction_data( $order, $posted ) {
}

// Address.
if ( ! empty( $order->billing_address_1 ) ) {
if ( $order->get_billing_address_1() ) {
$data['customer']['address'] = array(
'street' => $order->billing_address_1,
'complementary' => $order->billing_address_2,
'zipcode' => $this->only_numbers( $order->billing_postcode ),
'street' => $order->get_billing_address_1(),
'complementary' => $order->get_billing_address_2(),
'zipcode' => $this->only_numbers( $order->get_billing_postcode() ),
);

// Non-WooCommerce default address fields.
Expand Down Expand Up @@ -549,7 +549,7 @@ public function do_transaction( $order, $args, $token = '' ) {
*/
public function do_refund( $order_id, $amount ) {
$order = wc_get_order( $order_id );
$transaction_id = get_post_meta( $order_id, '_wc_pagarme_transaction_id', true );
$transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' );
$endpoint = 'transactions/' . $transaction_id . '/refund';
$data = array (
'api_key' => $this->gateway->api_key,
Expand Down Expand Up @@ -862,15 +862,16 @@ public function process_successful_ipn( $posted ) {
$order = wc_get_order( $order_id );
$status = sanitize_text_field( $posted['current_status'] );

if ( $order && $order->id === $order_id ) {
if ( $order && $order->get_id() === $order_id ) {
$this->process_order_status( $order, $status );
}

// Async transactions will only send the boleto_url on IPN.
if ( ! empty( $posted['transaction']['boleto_url'] ) && 'pagarme-banking-ticket' === $order->payment_method ) {
$post_data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true );
if ( ! empty( $posted['transaction']['boleto_url'] ) && 'pagarme-banking-ticket' === $order->get_payment_method() ) {
$post_data = $order->get_meta( '_wc_pagarme_transaction_data' );
$post_data['boleto_url'] = sanitize_text_field( $posted['transaction']['boleto_url'] );
update_post_meta( $order->id, '_wc_pagarme_transaction_data', $post_data );
$order->update_meta_data( '_wc_pagarme_transaction_data', $post_data );
$order->save();
}
}

Expand All @@ -893,7 +894,7 @@ public function process_order_status( $order, $status ) {

break;
case 'pending_review':
$transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true );
$transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' );
$transaction_url = '<a href="https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '">https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '</a>';

/* translators: %s transaction details url */
Expand All @@ -920,7 +921,7 @@ public function process_order_status( $order, $status ) {
case 'refused' :
$order->update_status( 'failed', __( 'Pagar.me: The transaction was rejected by the card company or by fraud.', 'woocommerce-pagarme' ) );

$transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true );
$transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' );
$transaction_url = '<a href="https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '">https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '</a>';

$this->send_email(
Expand All @@ -933,7 +934,7 @@ public function process_order_status( $order, $status ) {
case 'refunded' :
$order->update_status( 'refunded', __( 'Pagar.me: The transaction was refunded/canceled.', 'woocommerce-pagarme' ) );

$transaction_id = get_post_meta( $order->id, '_wc_pagarme_transaction_id', true );
$transaction_id = $order->get_meta( '_wc_pagarme_transaction_id' );
$transaction_url = '<a href="https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '">https://dashboard.pagar.me/#/transactions/' . intval( $transaction_id ) . '</a>';

$this->send_email(
Expand Down
6 changes: 3 additions & 3 deletions includes/class-wc-pagarme-banking-ticket-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function process_payment( $order_id ) {
*/
public function thankyou_page( $order_id ) {
$order = wc_get_order( $order_id );
$data = get_post_meta( $order_id, '_wc_pagarme_transaction_data', true );
$data = $order->get_meta( '_wc_pagarme_transaction_data' );

if ( isset( $data['boleto_url'] ) && in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) ) {
$template = 'no' === $this->async ? 'payment' : 'async';
Expand All @@ -201,11 +201,11 @@ public function thankyou_page( $order_id ) {
* @return string Payment instructions.
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->payment_method ) {
if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->get_payment_method() ) {
return;
}

$data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true );
$data = $order->get_meta( '_wc_pagarme_transaction_data' );

if ( isset( $data['boleto_url'] ) ) {
$email_type = $plain_text ? 'plain' : 'html';
Expand Down
6 changes: 3 additions & 3 deletions includes/class-wc-pagarme-credit-card-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {
*/
public function thankyou_page( $order_id ) {
$order = wc_get_order( $order_id );
$data = get_post_meta( $order_id, '_wc_pagarme_transaction_data', true );
$data = $order->get_meta( '_wc_pagarme_transaction_data' );

if ( isset( $data['installments'] ) && in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) ) {
wc_get_template(
Expand All @@ -360,11 +360,11 @@ public function thankyou_page( $order_id ) {
* @return string Payment instructions.
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->payment_method ) {
if ( $sent_to_admin || ! in_array( $order->get_status(), array( 'processing', 'on-hold' ), true ) || $this->id !== $order->get_payment_method() ) {
return;
}

$data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true );
$data = $order->get_meta( '_wc_pagarme_transaction_data' );

if ( isset( $data['installments'] ) ) {
$email_type = $plain_text ? 'plain' : 'html';
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-pagarme-my-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public function __construct() {
* @return array
*/
public function my_orders_banking_ticket_link( $actions, $order ) {
if ( 'pagarme-banking-ticket' !== $order->payment_method ) {
if ( 'pagarme-banking-ticket' !== $order->get_payment_method() ) {
return $actions;
}

if ( ! in_array( $order->get_status(), array( 'pending', 'on-hold' ), true ) ) {
return $actions;
}

$data = get_post_meta( $order->id, '_wc_pagarme_transaction_data', true );
$data = $order->get_meta( '_wc_pagarme_transaction_data' );
if ( ! empty( $data['boleto_url'] ) ) {
$actions[] = array(
'url' => $data['boleto_url'],
Expand Down
25 changes: 19 additions & 6 deletions woocommerce-pagarme.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,28 @@ class WC_Pagarme {
private function __construct() {
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
$is_installed = false;
$has_required_version = false;

// Checks with WooCommerce is installed.
// Checks if WooCommerce is installed.
if ( class_exists( 'WC_Payment_Gateway' ) ) {
$this->upgrade();
$this->includes();
$is_installed = true;

add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
} else {
// Checks if minimum Woocommerce version is installed.
if ( defined( 'WC_VERSION' ) ) {
$has_required_version = version_compare( WC_VERSION, '3.0', '>=' );
}

if ( $has_required_version ) {
$this->upgrade();
$this->includes();

add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
}
}

if ( ! $is_installed || ! $has_required_version ) {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
}
}
Expand Down

0 comments on commit 1b829a0

Please sign in to comment.