Skip to content

Commit

Permalink
Envío de PDF por correo y mapa de tiendas
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Balderas committed Feb 15, 2019
1 parent c6bece6 commit 2f63509
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 48 deletions.
Binary file modified openpay-stores.zip
Binary file not shown.
37 changes: 30 additions & 7 deletions openpay-stores/openpay_stores.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

/*
Plugin Name: Openpay Stores Plugin
Plugin URI: http://www.openpay.mx/docs/plugins/woocommerce.html
Description: Provides a cash payment method with Openpay for WooCommerce. Compatible with WooCommerce 3.3.5 and Wordpress 4.9.5.
Version: 1.3.0
Description: Provides a cash payment method with Openpay for WooCommerce. Compatible with WooCommerce 3.5.3 and Wordpress 5.0.3.
Version: 1.4.0
Author: Openpay
Author URI: http://www.openpay.mx
Expand All @@ -13,13 +14,35 @@
Openpay Docs: http://www.openpay.mx/docs/
*/


function openpay_stores_init_your_gateway()
{
if (class_exists('WC_Payment_Gateway'))
{
function openpay_stores_init_your_gateway() {
if (class_exists('WC_Payment_Gateway')) {
include_once('openpay_stores_gateway.php');
}
}

add_action('plugins_loaded', 'openpay_stores_init_your_gateway', 0);
add_filter('woocommerce_email_attachments', 'attach_store_payment_receipt', 10, 3);

function attach_store_payment_receipt($attachments, $email_id, $order) {
$logger = wc_get_logger();
$upload_dir = wp_upload_dir();
$order_id = $order->get_id();

// Only for "customer_on_hold_order" email notification (for customers)
if ($email_id == 'customer_on_hold_order' && $order->get_payment_method() == 'openpay_stores') {
$pdf_url = get_post_meta($order_id, '_pdf_url', true);

$logger->info('get_shipping_postcode: ' . $order->get_shipping_postcode());
$logger->info('_openpay_customer_id: ' . get_post_meta($order->get_id(), '_openpay_customer_id', true));
$logger->info('_pdf_url: ' . $pdf_url);
$logger->info('email_id: ' . $email_id);
$logger->info('order_id: ' . $order_id);
$logger->info('basedir: ' . $upload_dir['basedir']);

$pdf_path = $upload_dir['basedir'] . '/recibo_pago_' . $order_id . '.pdf';
file_put_contents($pdf_path, file_get_contents($pdf_url));
$attachments[] = $pdf_path;
}

return $attachments;
}
86 changes: 50 additions & 36 deletions openpay-stores/openpay_stores_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Openpay_Stores extends WC_Payment_Gateway
protected $order = null;
protected $transaction_id = null;
protected $transactionErrorMessage = null;
protected $currencies = array('MXN');
protected $currencies = array('MXN');

public function __construct() {
$this->id = 'openpay_stores';
Expand All @@ -28,6 +28,7 @@ public function __construct() {

$this->init_form_fields();
$this->init_settings();
$this->logger = wc_get_logger();

$this->title = 'Pago en efectivo en tiendas de conveniencia';
$this->description = '';
Expand All @@ -38,7 +39,7 @@ public function __construct() {
$this->live_merchant_id = $this->settings['live_merchant_id'];
$this->live_private_key = $this->settings['live_private_key'];
$this->live_publishable_key = $this->settings['live_publishable_key'];
$this->deadline = $this->settings['deadline'];
$this->deadline = $this->settings['deadline'];

$this->merchant_id = $this->is_sandbox ? $this->test_merchant_id : $this->live_merchant_id;
$this->publishable_key = $this->is_sandbox ? $this->test_publishable_key : $this->live_publishable_key;
Expand Down Expand Up @@ -83,74 +84,86 @@ public function webhook_handler() {
$json = json_decode($obj);

if ($json->type == 'charge.succeeded') {
$order_id = $json->transaction->order_id;
$payment_date = date("Y-m-d", $json->event_date);
$order = new WC_Order($order_id);
update_post_meta($order->get_id(), 'openpay_payment_date', $payment_date);
$order->payment_complete();
$order->add_order_note(sprintf("Payment completed."));
$openpay = Openpay::getInstance($this->merchant_id, $this->private_key);
Openpay::setProductionMode($this->is_sandbox ? false : true);

$charge = $openpay->charges->get($json->transaction->id);

if ($charge->status == 'completed') {
$order_id = $json->transaction->order_id;
$payment_date = date("Y-m-d", $json->event_date);
$order = new WC_Order($order_id);
update_post_meta($order->get_id(), 'openpay_payment_date', $payment_date);
$order->payment_complete();
$order->add_order_note(sprintf("Payment completed."));
}
}
}

public function init_form_fields() {

public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'type' => 'checkbox',
'title' => __('Enable/Disable', 'woothemes'),
'label' => __('Enable Openpay Stores', 'woothemes'),
'title' => __('Habilitar módulo', 'woothemes'),
'label' => __('Habilitar', 'woothemes'),
'default' => 'yes'
),
'sandbox' => array(
'type' => 'checkbox',
'title' => __('Sandbox mode', 'woothemes'),
'label' => __('Enable sandbox', 'woothemes'),
'description' => __('Place the payment gateway in test mode using Sandbox API keys.', 'woothemes'),
'title' => __('Modo de pruebas', 'woothemes'),
'label' => __('Habilitar', 'woothemes'),
'default' => 'no'
),
'test_merchant_id' => array(
'type' => 'text',
'title' => __('Sandbox merchant ID', 'woothemes'),
'description' => __('Get your Sandbox API keys from your Openpay account.', 'woothemes'),
'title' => __('ID de comercio de pruebas', 'woothemes'),
'description' => __('Obten tus llaves de prueba de tu cuenta de Openpay.', 'woothemes'),
'default' => __('', 'woothemes')
),
'test_private_key' => array(
'type' => 'text',
'title' => __('Sandbox secret key', 'woothemes'),
'description' => __('Get your Sandbox API keys from your Openpay account ("sk_").', 'woothemes'),
'title' => __('Llave secreta de pruebas', 'woothemes'),
'description' => __('Obten tus llaves de prueba de tu cuenta de Openpay ("sk_").', 'woothemes'),
'default' => __('', 'woothemes')
),
'test_publishable_key' => array(
'type' => 'text',
'title' => __('Sandbox public key', 'woothemes'),
'description' => __('Get your Sandbox API keys from your Openpay account ("pk_").', 'woothemes'),
'title' => __('Llave pública de pruebas', 'woothemes'),
'description' => __('Obten tus llaves de prueba de tu cuenta de Openpay ("pk_").', 'woothemes'),
'default' => __('', 'woothemes')
),
'live_merchant_id' => array(
'type' => 'text',
'title' => __('Production merchant ID', 'woothemes'),
'description' => __('Get your Production API keys from your Openpay account.', 'woothemes'),
'title' => __('ID de comercio de producción', 'woothemes'),
'description' => __('Obten tus llaves de producción de tu cuenta de Openpay.', 'woothemes'),
'default' => __('', 'woothemes')
),
'live_private_key' => array(
'type' => 'text',
'title' => __('Production secret key', 'woothemes'),
'description' => __('Get your Production API keys from your Openpay account ("sk_").', 'woothemes'),
'title' => __('Llave secreta de producción', 'woothemes'),
'description' => __('Obten tus llaves de producción de tu cuenta de Openpay ("sk_").', 'woothemes'),
'default' => __('', 'woothemes')
),
'live_publishable_key' => array(
'type' => 'text',
'title' => __('Production public key', 'woothemes'),
'description' => __('Get your Production API keys from your Openpay account ("pk_").', 'woothemes'),
'title' => __('Llave pública de producción', 'woothemes'),
'description' => __('Obten tus llaves de producción de tu cuenta de Openpay ("pk_").', 'woothemes'),
'default' => __('', 'woothemes')
),
),
'deadline' => array(
'type' => 'number',
'required' => true,
'title' => __('Payment deadline', 'woothemes'),
'description' => __('Define how many hours have the customer to make the payment.', 'woothemes'),
'default' => '48'
),
'show_map' => array(
'type' => 'checkbox',
'title' => __('Mostrar mapa', 'woothemes'),
'label' => __('Habilitar', 'woothemes'),
'description' => __('Al selccionar esta opción, un mapa se desplegará mostrando las tiendas más cercanas al momento mostrar el recipo de pago (https://www.openpay.mx/docs/stores-map.html).', 'woothemes'),
'default' => 'no'
)
);
}

Expand All @@ -159,7 +172,7 @@ public function admin_options() {
}

public function payment_fields() {
$this->images_dir = plugin_dir_url( __FILE__ ).'/assets/images/';
$this->images_dir = plugin_dir_url( __FILE__ ).'/assets/images/';
include_once('templates/payment.php');
}

Expand All @@ -173,7 +186,7 @@ protected function processOpenpayCharge() {
"amount" => (float) $this->order->get_total(),
"currency" => strtolower(get_woocommerce_currency()),
"description" => sprintf("Cargo para %s", $this->order->get_billing_email()),
"order_id" => $this->order->get_id(),
//"order_id" => $this->order->get_id(),
'due_date' => $due_date
);

Expand All @@ -182,14 +195,15 @@ protected function processOpenpayCharge() {
$result_json = $this->createOpenpayCharge($openpay_customer, $charge_request);

if ($result_json != false) {

$this->transaction_id = $result_json->id;
WC()->session->set('pdf_url', $this->pdf_url_base.'/'.$this->merchant_id.'/'.$result_json->payment_method->reference);
$pdf_url = $this->pdf_url_base.'/'.$this->merchant_id.'/'.$result_json->payment_method->reference;
//WC()->session->set('pdf_url', $pdf_url);
//Save data for the ORDER
update_post_meta($this->order->get_id(), '_openpay_customer_id', $openpay_customer->id);
update_post_meta($this->order->get_id(), '_transaction_id', $result_json->id);
update_post_meta($this->order->get_id(), '_key', $this->private_key);

update_post_meta($this->order->get_id(), '_show_map', $this->settings['show_map']);
update_post_meta($this->order->get_id(), '_pdf_url', $pdf_url);

return true;
} else {
return false;
Expand All @@ -204,7 +218,8 @@ public function process_payment($order_id) {
$this->order->update_status('on-hold', 'En espera de pago');
$this->order->reduce_order_stock();
$woocommerce->cart->empty_cart();
$this->order->add_order_note(sprintf("%s payment completed with Transaction Id of '%s'", $this->GATEWAY_NAME, $this->transaction_id));
$this->order->add_order_note(sprintf("%s payment completed with Transaction Id of '%s'", $this->GATEWAY_NAME, $this->transaction_id));

return array(
'result' => 'success',
'redirect' => $this->get_return_url($this->order)
Expand Down Expand Up @@ -253,7 +268,6 @@ public function getOpenpayCustomer() {
}

public function createOpenpayCustomer() {

$customerData = array(
'name' => $this->order->get_billing_first_name(),
'last_name' => $this->order->get_billing_last_name(),
Expand Down
18 changes: 13 additions & 5 deletions openpay-stores/templates/woocommerce/checkout/thankyou.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,21 @@
<?php endif; ?>

</ul>

<?php if (WC()->session->__isset('pdf_url') && ($order->get_payment_method() == 'openpay_stores' || $order->get_payment_method() == 'openpay_spei')): ?>
<div class="clear"></div>
<br>
<iframe id="pdf" src="<?php echo WC()->session->get('pdf_url') ?>" style="width:100%; height:950px; visibility: visible !important; opacity: 1 !important;" frameborder="0"></iframe>
<?php //if (WC()->session->__isset('pdf_url') && ($order->get_payment_method() == 'openpay_stores' || $order->get_payment_method() == 'openpay_spei')): ?>
<?php if(strlen(get_post_meta($order->get_id(), '_pdf_url', true)) && ($order->get_payment_method() == 'openpay_stores' || $order->get_payment_method() == 'openpay_spei')): ?>
<h2 class="woocommerce-order-details__title">Recibo de pago</h2>
<iframe id="pdf" src="<?php echo get_post_meta($order->get_id(), '_pdf_url', true); //echo WC()->session->get('pdf_url') ?>" style="width:100%; height:950px; visibility: visible !important; opacity: 1 !important;" frameborder="0"></iframe>
<?php endif; ?>

<div class="clear"></div>

<?php if(get_post_meta($order->get_id(), '_show_map', true) == 'yes'): ?>
<div style="margin: 20px 0px;">
<h2 class="woocommerce-order-details__title">Mapa de tiendas</h2>
<iframe src="https://www.paynet.com.mx/mapa-tiendas/index.html?locationNotAllowed=true&postalCode=<?php echo $order->get_shipping_postcode() ?>" style="border: 1px solid #000; width:100%; height:300px; visibility: visible !important; opacity: 1 !important;" frameborder="0"></iframe>
</div>
<?php endif; ?>

<?php endif; ?>

Expand Down

0 comments on commit 2f63509

Please sign in to comment.