-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcp-stripe-elements.php
141 lines (122 loc) · 4.14 KB
/
rcp-stripe-elements.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
<?php
/*
Plugin Name: Restrict Content Pro Stripe Elements Gateway
Plugin URI: http://www.vanpattenmedia.com/
Description: Accept payments using Stripe Elements
Version: 1.3.0
Text Domain: rcp-stripe-elements
Domain Path: /languages
Author: Van Patten Media Inc.
Author URI: https://www.vanpattenmedia.com/
Contributors: chrisvanpatten, mcfarlan
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register Stripe Elements with RCP
*
* @param array $gateways
*
* @return array $gateways
*/
function rcp_elements_register_stripe_elements_gateway( $gateways ) {
// Exit early if the stripe_elements gateway or class already exist
if ( isset( $gateways['stripe_elements'] ) ) {
return $gateways;
}
// Define the stripe_elements gateway
$gateways['stripe_elements'] = array(
'label' => 'Stripe Elements',
'admin_label' => 'Stripe Elements',
'class' => 'RCP_Payment_Gateway_Stripe_Elements'
);
// Include RCP Stripe Elements Gateway
if ( ! class_exists( 'RCP_Payment_Gateway_Stripe_Elements' ) ) {
$path = trailingslashit( plugin_dir_path( __FILE__ ) );
require_once $path . 'class-rcp-payment-gateway-stripe-elements.php';
}
return $gateways;
}
add_filter( 'rcp_payment_gateways', 'rcp_elements_register_stripe_elements_gateway' );
/**
* Custom path for RCP template overrides
*
* @param array $template_stack
* @param string $template_names
*
* @return array $template_stack
*/
function rcp_elements_custom_template_path( $template_stack, $template_names ) {
$template_stack[] = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/';
return $template_stack;
}
add_filter( 'rcp_template_stack', 'rcp_elements_custom_template_path', 10, 2 );
/**
* Conditionally loads Stripe Elements JS for the `update card` page
*
* @return void
*/
function rcp_elements_load_scripts() {
// Bail early if scripts shouldn't be loaded
if ( rcp_elements_is_update_card_page() === false ) {
return;
}
$gateway = new RCP_Payment_Gateway_Stripe_Elements();
$gateway->scripts();
}
add_action( 'wp_enqueue_scripts', 'rcp_elements_load_scripts', 10, 0 );
/**
* Checks if the current page is the RCP update card page
*
* @return bool
*/
function rcp_elements_is_update_card_page() {
global $rcp_options, $post;
if ( isset( $rcp_options['update_card'] ) ) {
return is_page( $rcp_options['update_card'] );
}
if ( ! empty( $post ) && has_shortcode( $post->post_content, 'rcp_update_card' ) ) {
return true;
}
return false;
}
/**
* Checks if the membership can be cancelled
*
* @param bool $can_cancel Whether or not the membership can be cancelled.
* @param int $membership_id ID of the membership being checked.
* @param RCP_Membership $membership Membership object.
*
* @return bool Whether or not the membership can be cancelled.
*/
function rcp_elements_can_cancel( $can_cancel, $membership_id, $membership ) {
if ( $membership->get_gateway() !== 'stripe_elements' ) {
return $can_cancel;
}
if ( $membership->is_active() && $membership->is_recurring() && rcp_is_stripe_membership( $membership ) ) {
$can_cancel = true;
}
return $can_cancel;
}
add_filter( 'rcp_membership_can_cancel', 'rcp_elements_can_cancel', 10, 3 );
/**
* Cancel a Stripe Elements membership
*
* @param bool $success Whether or not the cancellation was successful.
* @param string $gateway Payment gateway used for the membership.
* @param string $gateway_subscription_id Gateway subscription ID.
* @param int $membership_id ID of the membership being cancelled.
* @param RCP_Membership $membership Membership object.
*
* @return true|WP_Error True if the cancellation was successful, WP_Error on failure.
*/
function rcp_elements_cancel( $success, $gateway, $gateway_subscription_id, $membership_id, $membership ) {
// If it was already cancelled or not Stripe Elements gateway, bail.
if ( $success || $gateway !== 'stripe_elements' ) {
return $success;
}
return rcp_stripe_cancel_membership( $gateway_subscription_id );
}
add_filter( 'rcp_membership_payment_profile_cancelled', 'rcp_elements_cancel', 10, 5 );