PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.4.1
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.4.1
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.1, at includes/API/UserAPI.php

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