-
Notifications
You must be signed in to change notification settings - Fork 2
/
ajax.php
87 lines (57 loc) · 2.32 KB
/
ajax.php
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
<?php
/*
========================
AJAX FUNCTIONS
========================
*/
add_action('wp_ajax_nopriv_ewvwp_form_ajax', 'ewvwp_ajax');
add_action('wp_ajax_ewvwp_form_ajax', 'ewvwp_ajax');
function ewvwp_ajax()
{
$quantity = intval($_POST['quantity']);
$userID = intval($_POST['userID']);
$reference = sanitize_text_field($_POST['reference']);
$email = sanitize_email($_POST['email']);
//The parameter after verify/ is the transaction reference to be verified
$url = 'https://api.paystack.co/transaction/verify/'.$reference;
$headers = array(
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache',
'Authorization' => 'Bearer ' . get_option( 'ewvwp_paystack_secret_key' ),
);
$args = array(
'headers' => $headers,
'timeout' => 60,
);
$request = wp_remote_get( $url, $args );
if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
$paystack_response = json_decode( wp_remote_retrieve_body( $request ), true );
if ( 'success' == $paystack_response['data']['status'] ) {
$post_status = "publish"; //publish, draft, etc
$post_type = "ewvwp"; // or whatever post type desired
/* Attempt to find post id by post name if it exists */
$found_post = get_post( $userID );
$found_post_id = $found_post_title->ID;
if ( FALSE === get_post_status( $found_post ) ) {
$result = array(
'success' => false,
'message' => "Candidate not found"
);
return wp_send_json( $result );
} else {
$vote = get_post_meta($userID, "_ewvwp_vote_value_key", true);
$total = $vote + $quantity;
update_post_meta( $userID, '_ewvwp_vote_value_key', $total );
$result = array(
'success' => true,
'message' => "Thanks for voting"
);
return wp_send_json( $result );
}
return wp_send_json($paystack_response);
} else {
return wp_send_json(array('success' => false, 'status' => 500, 'message' => 'Payment failed'));
}
}
die();
}