AverageDonation.php
5 years ago
DonationsVsIncome.php
5 years ago
Endpoint.php
5 years ago
FormPerformance.php
5 years ago
Income.php
5 years ago
IncomeBreakdown.php
5 years ago
PaymentMethods.php
5 years ago
PaymentStatuses.php
5 years ago
RecentDonations.php
5 years ago
TopDonors.php
5 years ago
TotalDonors.php
5 years ago
TotalIncome.php
5 years ago
TotalRefunds.php
5 years ago
PaymentMethods.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Reports base endpoint |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\API\Endpoints\Reports; |
| 10 | |
| 11 | class PaymentMethods extends Endpoint { |
| 12 | |
| 13 | public function __construct() { |
| 14 | $this->endpoint = 'payment-methods'; |
| 15 | } |
| 16 | |
| 17 | public function getReport( $request ) { |
| 18 | |
| 19 | $paymentObjects = $this->getPayments( $request->get_param( 'start' ), $request->get_param( 'end' ), 'date', -1 ); |
| 20 | $gatewayObjects = give_get_payment_gateways(); |
| 21 | |
| 22 | if ( $this->testMode === false ) { |
| 23 | unset( $gatewayObjects['manual'] ); |
| 24 | } |
| 25 | |
| 26 | $gateways = []; |
| 27 | foreach ( $gatewayObjects as $gatewayId => $gatewayObject ) { |
| 28 | $gateways[ $gatewayId ] = [ |
| 29 | 'label' => $gatewayObject['admin_label'], |
| 30 | 'count' => 0, |
| 31 | 'amount' => 0, |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | if ( count( $paymentObjects ) > 0 ) { |
| 36 | foreach ( $paymentObjects as $paymentObject ) { |
| 37 | $gateways[ $paymentObject->gateway ]['count'] += 1; |
| 38 | $gateways[ $paymentObject->gateway ]['amount'] += $paymentObject->total; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | $gatewaysSorted = usort( |
| 43 | $gateways, |
| 44 | function ( $a, $b ) { |
| 45 | if ( $a['amount'] == $b['amount'] ) { |
| 46 | return 0; |
| 47 | } |
| 48 | return ( $a['amount'] > $b['amount'] ) ? -1 : 1; |
| 49 | } |
| 50 | ); |
| 51 | |
| 52 | $data = []; |
| 53 | $labels = []; |
| 54 | $tooltips = []; |
| 55 | |
| 56 | if ( $gatewaysSorted == true ) { |
| 57 | $gateways = array_slice( $gateways, 0, 5 ); |
| 58 | foreach ( $gateways as $gateway ) { |
| 59 | $labels[] = $gateway['label']; |
| 60 | $data[] = $gateway['amount']; |
| 61 | $tooltips[] = [ |
| 62 | 'title' => give_currency_filter( |
| 63 | give_format_amount( $gateway['amount'] ), |
| 64 | [ |
| 65 | 'currency_code' => $this->currency, |
| 66 | 'decode_currency' => true, |
| 67 | 'sanitize' => false, |
| 68 | ] |
| 69 | ), |
| 70 | 'body' => $gateway['count'] . ' ' . __( 'Payments', 'give' ), |
| 71 | 'footer' => $gateway['label'], |
| 72 | ]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return [ |
| 77 | 'labels' => $labels, |
| 78 | 'datasets' => [ |
| 79 | [ |
| 80 | 'data' => $data, |
| 81 | 'tooltips' => $tooltips, |
| 82 | ], |
| 83 | ], |
| 84 | ]; |
| 85 | } |
| 86 | } |
| 87 |