-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpmpro-paypal.php
More file actions
156 lines (142 loc) · 5.21 KB
/
Copy pathpmpro-paypal.php
File metadata and controls
156 lines (142 loc) · 5.21 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
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
<?php
/**
* Plugin Name: Paid Memberships Pro - PayPal Gateway
* Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-paypal/
* Description: Modern PayPal integration using Orders V2 and Subscriptions API v1 with offsite redirect checkout.
* Version: 1.1.1
* Author: Paid Memberships Pro
* Author URI: https://www.paidmembershipspro.com
* Text Domain: pmpro-paypal
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) || exit;
define( 'PMPRO_PAYPAL_VERSION', '1.1.1' );
define( 'PMPRO_PAYPAL_DIR', plugin_dir_path( __FILE__ ) );
define( 'PMPRO_PAYPAL_URL', plugin_dir_url( __FILE__ ) );
define( 'PMPRO_PAYPAL_BASENAME', plugin_basename( __FILE__ ) );
/**
* Bootstrap the plugin after PMPro is loaded.
*/
function pmpro_paypal_init() {
// Gate on PMPro being active.
if ( ! defined( 'PMPRO_DIR' ) ) {
return;
}
// Require PMPro 3.7.1+ which renames the old 'paypal' (Website Payments Pro) slug
// to 'paypalwpp', freeing the 'paypal' slug for this add-on.
if ( defined( 'PMPRO_VERSION' ) && version_compare( PMPRO_VERSION, '3.7.1', '<' ) ) {
add_action( 'admin_notices', 'pmpro_paypal_needs_core_upgrade_notice' );
return;
}
// Load classes.
require_once PMPRO_PAYPAL_DIR . 'classes/class-paypal-api.php';
require_once PMPRO_PAYPAL_DIR . 'classes/class-pmprogateway-paypal.php';
// Secondary-gateway checkout UI.
require_once PMPRO_PAYPAL_DIR . 'includes/checkout.php';
// Register webhook REST route.
add_action( 'rest_api_init', 'pmpro_paypal_register_webhook_route' );
// Set gateway_ready based on credentials.
add_filter( 'pmpro_is_ready', 'pmpro_paypal_gateway_ready' );
}
add_action( 'plugins_loaded', 'pmpro_paypal_init', 20 );
/**
* Register webhook REST route.
*
* GET is accepted alongside POST so the URL can be opened in a browser
* to confirm the endpoint is reachable, matching the convention used by
* the other PMPro gateway webhook handlers. The callback no-ops on GET.
*/
function pmpro_paypal_register_webhook_route() {
register_rest_route( 'pmpro-paypal/v1', '/webhook', array(
'methods' => array( 'GET', 'POST' ),
'callback' => 'pmpro_paypal_webhook_callback',
'permission_callback' => '__return_true',
) );
}
/**
* Webhook callback — delegates POST requests to the handler. GET requests
* return a static "ok" response so the URL is browser-pingable; routing
* them through the handler would just log a signature-verification failure
* on every bot crawl.
*/
function pmpro_paypal_webhook_callback( $request ) {
if ( 'POST' !== $request->get_method() ) {
return new WP_REST_Response(
array(
'status' => 'ok',
'message' => 'PMPro PayPal webhook endpoint is reachable.',
),
200
);
}
require_once PMPRO_PAYPAL_DIR . 'includes/webhook-handler.php';
return pmpro_paypal_handle_webhook( $request );
}
/**
* Set gateway_ready based on credentials.
*
* PMPro core doesn't know about add-on gateways, so the gateway_ready
* global defaults to false. We hook pmpro_is_ready to set it.
*/
function pmpro_paypal_gateway_ready( $r ) {
global $pmpro_gateway_ready;
$gateway = pmpro_getGateway();
if ( 'paypal' === $gateway ) {
$pmpro_gateway_ready = pmpro_paypal_has_credentials();
if ( $pmpro_gateway_ready ) {
$r = true;
}
}
return $r;
}
/**
* Whether PayPal credentials are configured for the active environment.
*
* @return bool
*/
function pmpro_paypal_has_credentials() {
$environment = get_option( 'pmpro_gateway_environment', 'sandbox' );
$suffix = 'sandbox' === $environment ? '_sandbox' : '_live';
$client_id = get_option( 'pmpro_paypal_client_id' . $suffix );
$secret = get_option( 'pmpro_paypal_client_secret' . $suffix );
return ! empty( $client_id ) && ! empty( $secret );
}
/**
* Show admin notice when PMPro core is too old for this add-on.
*
* @since 1.0
*/
function pmpro_paypal_needs_core_upgrade_notice() {
// Only show on PMPro admin pages.
if ( ! isset( $_REQUEST['page'] ) || strpos( sanitize_text_field( $_REQUEST['page'] ), 'pmpro-' ) !== 0 ) {
return;
}
?>
<div class="notice notice-error">
<p>
<strong><?php esc_html_e( 'Paid Memberships Pro - PayPal Gateway', 'pmpro-paypal' ); ?>:</strong>
<?php esc_html_e( 'This add-on requires Paid Memberships Pro version 3.7.1 or later. Please update Paid Memberships Pro to use the PayPal Gateway.', 'pmpro-paypal' ); ?>
</p>
</div>
<?php
}
/**
* Add links to the plugin row meta.
*
* @param array $links the links array
* @param string $file the file name
* @return array $links the links array
* @since 1.0
*/
function pmpro_paypal_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-paypal.php' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/pmpro-paypal/' ) . '" title="' . esc_attr__( 'View Documentation', 'pmpro-paypal' ) . '">' . esc_html__( 'Docs', 'pmpro-paypal' ) . '</a>',
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/support/' ) . '" title="' . esc_attr__( 'Visit Customer Support Forum', 'pmpro-paypal' ) . '">' . esc_html__( 'Support', 'pmpro-paypal' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'pmpro_paypal_plugin_row_meta', 10, 2 );