PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.4.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.4.0
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / API / UserAPI.php

UserAPI.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.4.0, at includes/API/UserAPI.php

158 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\API;
4
5 use Better_Payment\Lite\Admin\DB;
6 use Better_Payment\Lite\Classes\Helper;
7 use Better_Payment\Lite\Classes\ProSubscriptionTransactionData;
8 use WP_REST_Controller;
9 use WP_REST_Server;
10
11 /**
12 * Exit if accessed directly
13 */
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Public User REST API — paginated transactions for the logged-in user.
20 */
21 class UserAPI extends WP_REST_Controller {
22
23 protected $namespace = 'better-payment/v1';
24
25 public function __construct() {
26 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
27 }
28
29 public function register_routes() {
30 register_rest_route( $this->namespace, '/user-transactions', [
31 'methods' => WP_REST_Server::READABLE,
32 'callback' => [ $this, 'get_user_transactions' ],
33 'permission_callback' => [ $this, 'check_permissions' ],
34 'args' => [
35 'page' => [
36 'default' => 1,
37 'sanitize_callback' => 'absint',
38 ],
39 'per_page' => [
40 'default' => 20,
41 'sanitize_callback' => 'absint',
42 ],
43 'tab' => [
44 'default' => 'transactions',
45 'sanitize_callback' => 'sanitize_text_field',
46 ],
47 ],
48 ] );
49 }
50
51 public function check_permissions() {
52 return is_user_logged_in();
53 }
54
55 public function get_user_transactions( $request ) {
56 $current_user = wp_get_current_user();
57 $email = $current_user->user_email;
58 $tab = in_array( $request->get_param( 'tab' ), [ 'transactions', 'subscriptions' ], true )
59 ? $request->get_param( 'tab' )
60 : 'transactions';
61
62 // Subscription rows are Better Payment's own Stripe recurring and split
63 // payments — Pro features, shown only in the Subscriptions tab Pro renders.
64 // Without Pro nothing can display them, so don't query for them.
65 if ( 'subscriptions' === $tab && ! apply_filters( 'better_payment/pro_enabled', false ) ) {
66 return rest_ensure_response( [
67 'transactions' => [],
68 'total' => 0,
69 'page' => max( 1, (int) $request->get_param( 'page' ) ),
70 'per_page' => max( 1, (int) $request->get_param( 'per_page' ) ),
71 'pages' => 1,
72 ] );
73 }
74
75 $result = DB::get_user_transactions_paginated( $email, [
76 'page' => $request->get_param( 'page' ),
77 'per_page' => $request->get_param( 'per_page' ),
78 'type' => $tab,
79 ] );
80
81 $transactions = [];
82
83 foreach ( $result['transactions'] as $txn ) {
84 $form_fields_info = maybe_unserialize( $txn->form_fields_info );
85 if ( ! is_array( $form_fields_info ) ) {
86 $form_fields_info = [];
87 }
88
89 $customer_name = isset( $form_fields_info['primary_first_name'] )
90 ? trim( sanitize_text_field( $form_fields_info['primary_first_name'] ) . ' ' . sanitize_text_field( $form_fields_info['primary_last_name'] ?? '' ) )
91 : '';
92 if ( empty( $customer_name ) ) {
93 $customer_name = isset( $form_fields_info['first_name'] )
94 ? trim( sanitize_text_field( $form_fields_info['first_name'] ) . ' ' . sanitize_text_field( $form_fields_info['last_name'] ?? '' ) )
95 : '';
96 }
97
98 $customer_email = isset( $form_fields_info['primary_email'] )
99 ? sanitize_text_field( $form_fields_info['primary_email'] )
100 : '';
101 if ( empty( $customer_email ) ) {
102 $customer_email = isset( $form_fields_info['email'] )
103 ? sanitize_text_field( $form_fields_info['email'] )
104 : '';
105 }
106
107 $status_raw = $txn->status ? sanitize_text_field( $txn->status ) : '';
108 $payment_date_formatted = wp_date(
109 get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
110 strtotime( $txn->payment_date )
111 );
112
113 $row = [
114 'id' => intval( $txn->id ),
115 'amount' => floatval( $txn->amount ),
116 'currency' => sanitize_text_field( $txn->currency ),
117 'status' => $status_raw,
118 // From the stored row: an unconfirmed pre-2.4.0 free checkout reads Incomplete
119 // (Helper::transaction_status_type()), as it is in no dashboard figure.
120 'status_type' => Helper::transaction_status_type( $txn, 'v2' ),
121 'status_color' => Helper::transaction_status_color( $txn ),
122 'status_label' => Helper::transaction_status_label( $txn ),
123 'payment_date' => $payment_date_formatted,
124 'transaction_id' => sanitize_text_field( $txn->transaction_id ),
125 'source' => strtolower( sanitize_text_field( $txn->source ) ),
126 'customer_name' => $customer_name,
127 'customer_email' => $customer_email,
128 // 'One Time' unless Pro labels the row (better_payment/transaction/payment_type).
129 'is_subscription'=> Helper::payment_type_label( $form_fields_info ),
130 'is_imported' => ! empty( $form_fields_info['is_imported'] ) && 1 === intval( $form_fields_info['is_imported'] ),
131 ];
132
133 if ( 'subscriptions' === $tab ) {
134 // Pro builds the subscription columns on `better_payment/user_api/subscription_row`.
135 // Lite's copy serves only an active Pro that does not (<= 1.4.3).
136 if ( ProSubscriptionTransactionData::lite_copy_applies() ) {
137 $row = ProSubscriptionTransactionData::subscription_row( $row, $form_fields_info );
138 }
139
140 $built = apply_filters( 'better_payment/user_api/subscription_row', $row, $form_fields_info, $txn );
141 $row = is_array( $built ) ? $built : $row;
142
143 $row = apply_filters( 'better_payment/user_api/enrich_subscription_row', $row );
144 }
145
146 $transactions[] = $row;
147 }
148
149 return rest_ensure_response( [
150 'transactions' => $transactions,
151 'total' => $result['total'],
152 'page' => $result['page'],
153 'per_page' => $result['per_page'],
154 'pages' => $result['pages'],
155 ] );
156 }
157 }
158