-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbillogramAjax.php
More file actions
101 lines (86 loc) · 3.05 KB
/
billogramAjax.php
File metadata and controls
101 lines (86 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Class to handle all ajax calls to the plugin
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
require_once('billogramApi.php');
function billogramAjaxInit() {
new BillogramAjax;
}
add_action('admin_init', 'billogramAjaxInit');
class BillogramAjax {
private $bill,
$api;
function __construct() {
add_action('wp_ajax_send_invoice', array($this, 'sendInvoice'));
add_action('wp_ajax_create_renewal', array($this, 'createRenewal'));
}
public function setup()
{
$this->bill = new BillogramWC;
$this->api = new BillogramApiWrapper($this->bill->apiUser, $this->bill->apiPassword, $this->bill->apiUrl);
}
public function sendInvoice() {
$this->setup();
// Check nonce, if invalid die
check_ajax_referer( "innovBilloNonce", "BillSec", true );
$post = get_post(intval($_POST['orderId']));
$gateway = get_post_meta($post->ID, '_payment_method', true);
$recurringGateway = get_post_meta($post->ID, '_recurring_payment_method', true);
try {
if( $gateway === 'billogramwc' || $recurringGateway === 'billogramwc') {
$invoiceId = get_post_meta($post->ID, '_billogram_id', true);
if($invoiceId !== '') {
$this->api->getInvoice($invoiceId);
//echo $this->api->getInvoiceCustomerValue('email');
} else {
try {
$this->bill->createInvoiceOrder($post->ID);
$invoiceId = get_post_meta($post->ID, '_billogram_id', true);
$this->api->getInvoice($invoiceId);
} catch (Exception $e) {
$order = wc_get_order($post->ID);
$order->update_status( 'failed', __( 'Failed to create the invoice', 'billogram-wc' ) );
error_log(print_r($e, true));
echo __("Something went wrong, the invoice was not sent!", 'billogram-wc' );
wp_die();
}
}
$this->api->send();
echo __("Invoice sent", 'billogram-wc' );
wp_die();
}
} catch (Exception $e) {
echo __("Something went wrong, the invoice was not sent!", 'billogram-wc' );
}
wp_die();
}
public function createRenewal() {
$this->setup();
// Check nonce, if invalid die
check_ajax_referer( "innovBilloNonce", "BillSec", true );
$post = get_post(intval($_POST['orderId']));
$gateway = get_post_meta($post->ID, '_payment_method', true);
try {
if( $gateway === 'billogramwc') {
$invoiceId = get_post_meta($post->ID, '_billogram_id', true);
if(class_exists( 'WC_Subscriptions_Order' ) ) {
if (WC_Subscriptions_Order::order_contains_subscription( $post->ID ) ) {
//WC_Subscriptions_Manager::process_subscription_payment_failure_on_order( $post->ID, $product_id = '');
WC_Subscriptions_Manager::process_subscription_payments_on_order( $post->ID, $product_id = '');
echo __("Order created!", 'billogram-wc' );
} else {
echo __("This order does not contain any subscriptions!", 'billogram-wc' );
}
} else {
echo __("Woocommerce Subscriptions was not found!", 'billogram-wc' );
}
wp_die();
}
} catch (Exception $e) {
echo __("Something went wrong, the order was not created!", 'billogram-wc' );
}
wp_die();
}
}
?>