-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqpay-payment.php
More file actions
94 lines (80 loc) · 3.09 KB
/
qpay-payment.php
File metadata and controls
94 lines (80 loc) · 3.09 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
<?php
/**
* Plugin Name: QPay Payment Forms
* Description: QPay V2 payment forms for WordPress (no WooCommerce required)
* Version: 1.0.1
* Author: QPay SDK
* License: MIT
*/
if (!defined('ABSPATH')) exit;
define('QPAY_WP_VERSION', '1.0.1');
define('QPAY_WP_PATH', plugin_dir_path(__FILE__));
define('QPAY_WP_URL', plugin_dir_url(__FILE__));
require_once QPAY_WP_PATH . 'includes/class-qpay-api.php';
require_once QPAY_WP_PATH . 'includes/class-qpay-admin.php';
require_once QPAY_WP_PATH . 'includes/class-qpay-shortcode.php';
require_once QPAY_WP_PATH . 'includes/class-qpay-ajax.php';
register_activation_hook(__FILE__, 'qpay_wp_activate');
function qpay_wp_activate()
{
global $wpdb;
$table = $wpdb->prefix . 'qpay_payments';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
invoice_id varchar(255) NOT NULL,
amount decimal(12,2) NOT NULL DEFAULT 0,
description varchar(500) DEFAULT '',
status varchar(32) NOT NULL DEFAULT 'pending',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY invoice_id (invoice_id),
KEY status (status)
) $charset;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
}
// Init
add_action('init', function () {
QPay_Shortcode::register();
});
add_action('admin_menu', function () {
QPay_Admin::register_menu();
});
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('qpay-form', QPAY_WP_URL . 'public/css/qpay-form.css', [], QPAY_WP_VERSION);
wp_enqueue_script('qpay-form', QPAY_WP_URL . 'public/js/qpay-form.js', [], QPAY_WP_VERSION, true);
wp_localize_script('qpay-form', 'qpayAjax', [
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('qpay_nonce'),
]);
});
// AJAX
QPay_Ajax::register();
// REST API webhook — QPay calls GET with ?qpay_payment_id=...
add_action('rest_api_init', function () {
register_rest_route('qpay/v1', '/webhook', [
'methods' => 'GET',
'callback' => function ($request) {
$payment_id = $request->get_param('qpay_payment_id');
if (!$payment_id) {
return new WP_REST_Response('FAILED', 400);
}
$api = new QPay_API();
// Get payment details to find the invoice_id
$payment = $api->get_payment($payment_id);
if ($payment && !empty($payment['object_id'])) {
$invoice_id = $payment['object_id'];
// Double-check payment via check_payment endpoint
$result = $api->check_payment($invoice_id);
if ($result && !empty($result['rows'])) {
global $wpdb;
$wpdb->update($wpdb->prefix . 'qpay_payments', ['status' => 'paid'], ['invoice_id' => $invoice_id]);
}
}
return new WP_REST_Response('SUCCESS', 200);
},
'permission_callback' => '__return_true',
]);
});