PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / trunk
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More vtrunk
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 1.0.5 1.0.6 All 66 releases
better-payment / includes / API / UserAPI.php

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

141 lines 6.0 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 $result = DB::get_user_transactions_paginated( $email, [
62 'page' => $request->get_param( 'page' ),
63 'per_page' => $request->get_param( 'per_page' ),
64 'type' => $tab,
65 ] );
66
67 $helper = new Helper();
68 $transactions = [];
69
70 foreach ( $result['transactions'] as $txn ) {
71 $form_fields_info = maybe_unserialize( $txn->form_fields_info );
72 if ( ! is_array( $form_fields_info ) ) {
73 $form_fields_info = [];
74 }
75
76 $customer_name = isset( $form_fields_info['primary_first_name'] )
77 ? trim( sanitize_text_field( $form_fields_info['primary_first_name'] ) . ' ' . sanitize_text_field( $form_fields_info['primary_last_name'] ?? '' ) )
78 : '';
79 if ( empty( $customer_name ) ) {
80 $customer_name = isset( $form_fields_info['first_name'] )
81 ? trim( sanitize_text_field( $form_fields_info['first_name'] ) . ' ' . sanitize_text_field( $form_fields_info['last_name'] ?? '' ) )
82 : '';
83 }
84
85 $customer_email = isset( $form_fields_info['primary_email'] )
86 ? sanitize_text_field( $form_fields_info['primary_email'] )
87 : '';
88 if ( empty( $customer_email ) ) {
89 $customer_email = isset( $form_fields_info['email'] )
90 ? sanitize_text_field( $form_fields_info['email'] )
91 : '';
92 }
93
94 $status_raw = $txn->status ? sanitize_text_field( $txn->status ) : '';
95 $payment_date_formatted = wp_date(
96 get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
97 strtotime( $txn->payment_date )
98 );
99
100 $row = [
101 'id' => intval( $txn->id ),
102 'amount' => floatval( $txn->amount ),
103 'currency' => sanitize_text_field( $txn->currency ),
104 'status' => $status_raw,
105 'status_color' => $helper->get_color_by_transaction_status( $status_raw, 'v2' ),
106 'status_label' => ucfirst( $helper->get_type_by_transaction_status( $status_raw, 'v2' ) ),
107 'payment_date' => $payment_date_formatted,
108 'transaction_id' => sanitize_text_field( $txn->transaction_id ),
109 'source' => strtolower( sanitize_text_field( $txn->source ) ),
110 'customer_name' => $customer_name,
111 'customer_email' => $customer_email,
112 'is_subscription'=> ! empty( $form_fields_info['subscription_id'] ) ? 'Subscription' : 'One Time',
113 'is_imported' => ! empty( $form_fields_info['is_imported'] ) && 1 === intval( $form_fields_info['is_imported'] ),
114 ];
115
116 if ( 'subscriptions' === $tab ) {
117 $row['subscription_id'] = sanitize_text_field( $form_fields_info['subscription_id'] ?? '' );
118 $row['subscription_plan_id'] = sanitize_text_field( $form_fields_info['subscription_plan_id'] ?? '' );
119 $row['subscription_status'] = sanitize_text_field( $form_fields_info['subscription_status'] ?? '' );
120 $row['subscription_interval'] = sanitize_text_field( $form_fields_info['subscription_interval'] ?? '' );
121 $row['subscription_created_date'] = intval( $form_fields_info['subscription_created_date'] ?? 0 );
122 $row['subscription_current_period_end'] = intval( $form_fields_info['subscription_current_period_end'] ?? 0 );
123 $row['is_payment_split_payment'] = ! empty( $form_fields_info['is_payment_split_payment'] ) ? intval( $form_fields_info['is_payment_split_payment'] ) : 0;
124 $row['subscription_product_name'] = '';
125
126 $row = apply_filters( 'better_payment/user_api/enrich_subscription_row', $row );
127 }
128
129 $transactions[] = $row;
130 }
131
132 return rest_ensure_response( [
133 'transactions' => $transactions,
134 'total' => $result['total'],
135 'page' => $result['page'],
136 'per_page' => $result['per_page'],
137 'pages' => $result['pages'],
138 ] );
139 }
140 }
141