Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.70% |
119 / 127 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Payments | |
95.20% |
119 / 125 |
|
60.00% |
3 / 5 |
7 | |
0.00% |
0 / 1 |
| get_key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_state | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| register_routes | |
100.00% |
39 / 39 |
|
100.00% |
1 / 1 |
1 | |||
| get_product_args | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
1 | |||
| get_coupon_args | |
100.00% |
50 / 50 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The Payments section: Stripe, plans, coupons and the commission WordPress.com takes. |
| 4 | * |
| 5 | * @package automattic/jetpack-monetize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Monetize\Sections; |
| 9 | |
| 10 | use Automattic\Jetpack\Current_Plan; |
| 11 | use Automattic\Jetpack\Monetize\Dashboard_Section; |
| 12 | use Automattic\Jetpack\Monetize\WPCOM_Proxy; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Payments state and routes. |
| 20 | * |
| 21 | * @since 0.2.0 |
| 22 | */ |
| 23 | class Payments implements Dashboard_Section { |
| 24 | |
| 25 | /** |
| 26 | * Section key. |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public function get_key() { |
| 31 | return 'payments'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Whether the plan lets the site manage payment plans, as Calypso decides it. |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public function get_state() { |
| 40 | return array( |
| 41 | 'canManagePlans' => Current_Plan::supports( 'recurring-payments' ) |
| 42 | || Current_Plan::supports( 'donations' ) |
| 43 | || Current_Plan::supports( 'premium-content/container' ), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Register the section's routes. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function register_routes() { |
| 53 | // Stripe connection, commission and currency. |
| 54 | WPCOM_Proxy::register_route( |
| 55 | 'wpcom/v2', |
| 56 | 'memberships/status', |
| 57 | 'GET', |
| 58 | array( |
| 59 | 'source' => array( |
| 60 | 'type' => 'string', |
| 61 | 'sanitize_callback' => 'sanitize_key', |
| 62 | ), |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/stripe/disconnect', 'POST' ); |
| 67 | |
| 68 | // Totals and next month's forecast, plus the commission. |
| 69 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/earnings', 'GET' ); |
| 70 | |
| 71 | WPCOM_Proxy::register_route( |
| 72 | 'wpcom/v2', |
| 73 | 'memberships/products', |
| 74 | 'GET', |
| 75 | array( |
| 76 | 'type' => array( |
| 77 | 'type' => 'string', |
| 78 | 'enum' => array( 'all' ), |
| 79 | ), |
| 80 | 'is_editable' => array( 'type' => 'boolean' ), |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/product', 'POST', self::get_product_args() ); |
| 85 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/product/(?P<id>\d+)', 'POST', self::get_product_args() ); |
| 86 | |
| 87 | WPCOM_Proxy::register_route( |
| 88 | 'wpcom/v2', |
| 89 | 'memberships/product/(?P<id>\d+)', |
| 90 | 'DELETE', |
| 91 | array( |
| 92 | 'cancel_subscriptions' => array( 'type' => 'boolean' ), |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/coupons', 'GET' ); |
| 97 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/coupons', 'POST', self::get_coupon_args() ); |
| 98 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/coupon/(?P<id>\d+)', 'PUT', self::get_coupon_args() ); |
| 99 | WPCOM_Proxy::register_route( 'wpcom/v2', 'memberships/coupon/(?P<id>\d+)', 'DELETE' ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * The fields a plan is created or updated with, as WordPress.com reads them. |
| 104 | * |
| 105 | * @return array |
| 106 | */ |
| 107 | private static function get_product_args() { |
| 108 | return array( |
| 109 | 'title' => array( |
| 110 | 'type' => 'string', |
| 111 | 'required' => true, |
| 112 | ), |
| 113 | 'price' => array( |
| 114 | 'type' => 'number', |
| 115 | 'required' => true, |
| 116 | ), |
| 117 | 'currency' => array( |
| 118 | 'type' => 'string', |
| 119 | 'required' => true, |
| 120 | ), |
| 121 | 'interval' => array( |
| 122 | 'type' => 'string', |
| 123 | 'enum' => array( '1 month', '1 year', 'one-time' ), |
| 124 | 'required' => true, |
| 125 | ), |
| 126 | 'type' => array( |
| 127 | 'type' => 'string', |
| 128 | 'enum' => array( 'tier', 'donation' ), |
| 129 | ), |
| 130 | // A tier's annual product names its monthly one. |
| 131 | 'tier' => array( 'type' => 'integer' ), |
| 132 | 'description' => array( 'type' => 'string' ), |
| 133 | 'welcome_email_content' => array( 'type' => 'string' ), |
| 134 | 'buyer_can_change_amount' => array( 'type' => 'boolean' ), |
| 135 | 'multiple_per_user' => array( 'type' => 'boolean' ), |
| 136 | 'subscribe_as_site_subscriber' => array( 'type' => 'boolean' ), |
| 137 | 'is_editable' => array( 'type' => 'boolean' ), |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * The fields a coupon is created or updated with, as WordPress.com reads them. |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | private static function get_coupon_args() { |
| 147 | return array( |
| 148 | 'coupon_code' => array( |
| 149 | 'type' => 'string', |
| 150 | 'pattern' => '^[a-zA-Z0-9-]{3,20}$', |
| 151 | 'required' => true, |
| 152 | ), |
| 153 | 'discount_type' => array( |
| 154 | 'type' => 'string', |
| 155 | 'enum' => array( 'percentage', 'amount' ), |
| 156 | 'required' => true, |
| 157 | ), |
| 158 | 'discount_percentage' => array( 'type' => 'number' ), |
| 159 | 'discount_value' => array( 'type' => 'number' ), |
| 160 | 'discount_currency' => array( 'type' => 'string' ), |
| 161 | 'start_date' => array( |
| 162 | 'type' => 'string', |
| 163 | 'pattern' => '^\d{4}-\d{2}-\d{2}$', |
| 164 | 'required' => true, |
| 165 | ), |
| 166 | // Empty when the coupon never expires. |
| 167 | 'end_date' => array( |
| 168 | 'type' => 'string', |
| 169 | 'pattern' => '^(\d{4}-\d{2}-\d{2})?$', |
| 170 | ), |
| 171 | // A number of months, such as "3 months", or empty for forever. |
| 172 | 'duration' => array( |
| 173 | 'type' => 'string', |
| 174 | 'pattern' => '^(\d+ months)?$', |
| 175 | ), |
| 176 | 'limit_per_user' => array( |
| 177 | 'type' => 'integer', |
| 178 | 'minimum' => 1, |
| 179 | ), |
| 180 | 'can_be_combined' => array( |
| 181 | 'type' => 'boolean', |
| 182 | 'required' => true, |
| 183 | ), |
| 184 | 'first_time_purchase_only' => array( |
| 185 | 'type' => 'boolean', |
| 186 | 'required' => true, |
| 187 | ), |
| 188 | 'plan_ids_allow_list' => array( |
| 189 | 'type' => 'array', |
| 190 | 'items' => array( 'type' => 'integer' ), |
| 191 | 'required' => true, |
| 192 | ), |
| 193 | 'email_allow_list' => array( |
| 194 | 'type' => 'array', |
| 195 | 'items' => array( 'type' => 'string' ), |
| 196 | 'required' => true, |
| 197 | ), |
| 198 | ); |
| 199 | } |
| 200 | } |