Last active
December 3, 2018 13:52
-
-
Save froger-me/2c66a842ef8900b017809d7c738130c9 to your computer and use it in GitHub Desktop.
WordPress WeChat Pay integration plugin skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: WeChat Pay integration plugin skeleton | |
Plugin URI: https://gist.github.com/froger-me/2c66a842ef8900b017809d7c738130c9 | |
Description: Requires WP Weixin version min 1.3. A skeleton plugin. NEEDS TO BE EDITED BY A DEVELOPER BEFORE USE! | |
Version: 1.3 | |
Author: Alexandre Froger | |
Author URI: https://froger.me | |
WC tested up to: 3.5.2 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
/* | |
* - The skeleton's structure is not optimized, better to use a class than global variables. | |
* - The plugin does not do anything on its own, but provides placeholders for custom logic. | |
* - The skeleton includes only the placeholders for handling transaction validation after a payment has already been made. | |
*/ | |
add_action( 'wp_weixin_endpoints', 'wechat_pay_plugin_add_endpoints' ); | |
add_filter( 'query_vars', 'wechat_pay_plugin_add_query_vars', 10, 1 ); | |
add_action( 'parse_request', 'wechat_pay_plugin_parse_request' ); | |
add_action( 'wp_weixin_handle_pay_notification', 'wechat_pay_plugin_handle_pay_notification' ); | |
add_action( 'wp_weixin_handle_auto_refund', 'wechat_pay_plugin_handle_auto_refund', 10, 2 ); | |
function wechat_pay_plugin_add_endpoints() { | |
add_rewrite_rule( | |
'^wechat-pay-plugin/my-callback/$', | |
'index.php?my_callback_var=1', | |
'top' | |
); | |
} | |
function wechat_pay_plugin_add_query_vars( $vars ) { | |
$vars[] = 'my_callback_var'; | |
return $vars; | |
} | |
function wechat_pay_plugin_parse_request() { | |
global $wp, $wechat_pay_plugin_is_handler; | |
if ( isset( $wp->query_vars['my_callback_var'] ) ) { | |
$wechat_pay_plugin_is_handler = true; | |
do_action( 'wp_weixin_handle_pay_notification' ); | |
} | |
} | |
function wechat_pay_plugin_handle_pay_notification() { | |
global $wechat_pay_plugin_payment_result, $wechat_pay_plugin_is_handler; | |
$wechat = wp_weixin_get_wechat(); | |
$notification = $wechat->getNotify(); | |
$wechat_error = $wechat->getError(); | |
$success = false; | |
$refund = false; | |
$more_data = wechat_pay_plugin_get_more_data( $notification ); | |
$is_wechat_pay_plugin_transaction = wechat_pay_plugin_identify_transaction( $notification ); | |
if ( $is_wechat_pay_plugin_transaction = wechat_pay_plugin_identify_transaction( $notification ) ) { | |
$success = true; | |
} | |
if ( $wechat_error ) { | |
$wechat_error = wechat_pay_plugin_get_error_message( $wechat_error ); | |
$refund = wechat_pay_plugin_get_wechat_error_refund_message( $notification, $wechat_error ); | |
} else { | |
if ( $is_wechat_pay_plugin_transaction ) { | |
$internal_error = wechat_pay_plugin_finalise_transaction( $notification ); | |
if ( $internal_error ) { | |
$refund = wechat_pay_plugin_get_internal_error_refund_message( $notification, $internal_error ); | |
} | |
} | |
} | |
$wechat_pay_plugin_payment_result = array( | |
'success' => $success, | |
'data' => $notification, | |
'refund' => $refund, | |
'notify_error' => ( ! $wechat_error ) ? false : $wechat_error, | |
'blog_id' => get_current_blog_id(), | |
'main_handler' => $wechat_pay_plugin_is_handler, | |
'wechat_pay_plugin_data' => $more_data, | |
); | |
add_filter( 'wp_weixin_pay_notify_results', 'wechat_pay_plugin_add_pay_result', 10, 1 ); | |
} | |
function wechat_pay_plugin_add_pay_result( $results ) { | |
global $wechat_pay_plugin_payment_result; | |
if ( is_array( $wechat_pay_plugin_payment_result ) && ! empty( $wechat_pay_plugin_payment_result ) ) { | |
$results[] = $wechat_pay_plugin_payment_result; | |
} | |
return $results; | |
} | |
function wechat_pay_plugin_handle_auto_refund( $refund_result, $result_data ) { | |
if ( $refund_result && isset( $result_data['wechat_pay_plugin_data'] ) ) { | |
wechat_pay_plugin_process_auto_refund_success( $result_data['wechat_pay_plugin_data'] ); | |
} elseif ( isset( $result_data['wechat_pay_plugin_data'] ) ) { | |
wechat_pay_plugin_process_auto_refund_failure( $result_data['wechat_pay_plugin_data'] ); | |
} | |
} | |
function wechat_pay_plugin_get_more_data( $wechat_notification ) { | |
return false; | |
} | |
function wechat_pay_plugin_get_error_message( $wechat_error ) { | |
if ( isset( $wechat_error['code'] ) ) { | |
$error_description = $wechat_error['code'] . ': ' . $wechat_error['message']; | |
} else { | |
$error_description = $wechat_error['message']; | |
} | |
return $error_description; | |
} | |
function wechat_pay_plugin_get_wechat_error_refund_message( $wechat_error ) { | |
return true; | |
} | |
function wechat_pay_plugin_identify_transaction( $wechat_notification ) { | |
return false; | |
} | |
function wechat_pay_plugin_finalise_transaction( $wechat_notification ) { | |
return false; | |
} | |
function wechat_pay_plugin_get_internal_error_refund_message( $wechat_notification, $error ) { | |
return true; | |
} | |
function wechat_pay_plugin_process_auto_refund_success( $wechat_pay_plugin_data ) { | |
} | |
function wechat_pay_plugin_process_auto_refund_failure( $wechat_pay_plugin_data ) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment