-
Notifications
You must be signed in to change notification settings - Fork 7
/
my-changa.php
208 lines (176 loc) · 6.24 KB
/
my-changa.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* @link https://mauko.co.ke
* @since 1.0.0
* @package my_changa
*
* @wordpress-plugin
* Plugin Name: My Changa Donations
* Plugin URI: https://osen.co/products/categories/wp/my-changa
* Description: Receive contributions/donations via Safaricom MyMPesa
* Version: 1.0.0
* Author: Mauko Maunde
* Author URI: https://mauko.co.ke
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: my-changa
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Start at version 1.0.0 and use SemVer - https://semver.org
*/
define( 'MY_CHANGA_VERSION', '1.0.0' );
/**
* The code that runs during plugin activation.
* @todo Create default configuration - mc-options
*/
register_activation_hook( __FILE__, 'activate_my_changa' );
function activate_my_changa() {
}
/**
* Delete mc-options setting during plugin deactivation.
*/
register_deactivation_hook( __FILE__, 'deactivate_my_changa' );
function deactivate_my_changa() {
}
/**
* Include external files
*/
require plugin_dir_path( __FILE__ ) . 'includes/mpesa.php';
require plugin_dir_path( __FILE__ ) . 'includes/settings.php';
add_action('admin_menu', 'mc_options_page');
function mc_options_page()
{
add_menu_page(
'My Changa Settings',
'My Changa',
'manage_options',
'mc',
'mc_options_page_html',
'dashicons-smiley',
20
);
}
add_filter( 'plugin_row_meta', 'mc_row_meta', 10, 2 );
function mc_row_meta( $links, $file )
{
$plugin = plugin_basename( __FILE__ );
if ( $plugin == $file ) {
$row_meta = array(
'github' => '<a href="' . esc_url( 'https://github.com/wcmpesa/my-changa' ) . '" target="_blank" aria-label="' . esc_attr__( 'Contribute on Github', 'woocommerce' ) . '">' . esc_html__( 'Github', 'woocommerce' ) . '</a>',
'apidocs' => '<a href="' . esc_url( 'https://developer.safaricom.co.ke/docs/' ) . '" target="_blank" aria-label="' . esc_attr__( 'MPesa API Docs ( Daraja )', 'woocommerce' ) . '">' . esc_html__( 'API docs', 'woocommerce' ) . '</a>'
);
return array_merge( $links, $row_meta );
}
return ( array ) $links;
}
add_filter( 'plugin_action_links_'.plugin_basename( __FILE__ ), 'mc_action_links' );
function mc_action_links( $links )
{
return array_merge( $links, [ '<a href="'.admin_url( 'admin.php?page=mc' ).'"> Preferences</a>' ] );
}
$mconfig = get_option( 'mc_options' );
$mconfig['mc_conf_callback_url'] = rtrim( home_url(), '/').':443/?mpesa_ipn_listener=reconcile';
$mconfig['mc_conf_timeout_url'] = rtrim( home_url(), '/').':443/?mpesa_ipn_listener=timeout';
$mconfig['mc_conf_result_url'] = rtrim( home_url(), '/').':443/?mpesa_ipn_listener=reconcile';
$mconfig['mc_conf_confirmation_url'] = rtrim( home_url(), '/').':443/?mpesa_ipn_listener=confirm';
$mconfig['mc_conf_validation_url'] = rtrim( home_url(), '/').':443/?mpesa_ipn_listener=validate';
mc_mpesa_setup( $mconfig );
add_shortcode('MCFORM', 'mc_form_callback');
function mc_form_callback( $atts = array(), $content = null ) {
$mconfig = get_option( 'mc_options' );
$status = isset( $_SESSION['mc_trx_status'] ) ? $mconfig['mc_mpesa_conf_msg'].'<br>'.$_SESSION['mc_trx_status'] : '';
return '<form id="mc-contribution-form" method="POST" action="" class="mc_contribution_form">
<p>'.$status.'</p>
<input type="hidden" name="action" value="process_mc_form">
<label for="mc-phone">Phone Number</label>
<input id="mc-phone" type="text" name="mc-phone" placeholder="Phone Number" class="mc_phone"><br>
<label for="mc-amount">Amount to contribute</label>
<input id="mc-amount" type="text" name="mc-amount" value="200" class="mc_amount"><br>
<button type="submit" name="mc-contribute" class="mc_contribute">CONTRIBUTE</button>
</form>';
}
add_action( 'init', 'mc_process_form_data' );
function mc_process_form_data() {
if ( isset( $_POST['mc-contribute'] ) ) {
$amount = trim( $_POST['mc-amount'] );
$phone = trim( $_POST['mc-phone'] );
$response = mc_mpesa_checkout( $amount, $phone, 'Contributions' );
$status = json_decode( $response );
$s = '';
if( !$response ){
$s .= "<b>Failed!</b> Could not process contribution. Please try again";
} elseif ( isset( $status->errorCode ) ) {
$s .= "<b>Request ID:</b> {$status->requestId}<br>";
$s .= "<b>Error Code:</b> {$status->errorCode}<br>";
$s .= "<b>Error Message:</b> {$status->errorMessage}<br>";
} else {
$ss = $status->Body->stkCallback;
$s .= "<b>Request ID:</b> {$ss->MerchantRequestID}<br>";
$s .= "<b>Checkout ID:</b> {$ss->CheckoutRequestID}<br>";
$s .= "<b>Code:</b> {$ss->ResultCode}<br>";
$s .= "<b>Description:</b> {$ss->ResultDesc}";
}
$_SESSION['mc_trx_status'] = $s;
}
}
/**
* Register Validation and Confirmation URLs
* Outputs registration status
*/
add_action( 'init', 'mc_mpesa_do_register' );
function mc_mpesa_do_register()
{
header( "Access-Control-Allow-Origin: *" );
header( 'Content-Type:Application/json' );
if ( ! isset( $_GET['mpesa_ipn_register'] ) ){ return; }
wp_send_json( mc_mpesa_register_urls() );
}
/**
*
*/
add_action( 'init', 'mc_mpesa_confirm' );
function mc_mpesa_confirm()
{
if ( ! isset( $_GET['mpesa_ipn_listener'] ) ) return;
if ( $_GET['mpesa_ipn_listener'] !== 'confirm' ) return;
$response = json_decode( file_get_contents( 'php://input' ), true );
if( ! isset( $response['Body'] ) ){
return;
}
header( "Access-Control-Allow-Origin: *" );
header( 'Content-Type:Application/json' );
wp_send_json(
array(
'ResponseCode' => 0,
'ResponseDesc' => 'Success',
'ThirdPartyTransID' => 0
)
);
}
/**
*
*/
add_action( 'init', 'mc_mpesa_validate' );
function mc_mpesa_validate()
{
if ( ! isset( $_GET['mpesa_ipn_listener'] ) ){ return; }
if ( $_GET['mpesa_ipn_listener'] !== 'validate' ){ return; }
$response = json_decode( file_get_contents( 'php://input' ), true );
if( ! isset( $response['Body'] ) ){
return;
}
header( "Access-Control-Allow-Origin: *" );
header( 'Content-Type:Application/json' );
wp_send_json(
array(
'ResponseCode' => 0,
'ResponseDesc' => 'Success',
'ThirdPartyTransID' => 0
)
);
}