PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / stripe / donations / charitable-stripe-donation-functions.php

charitable-stripe-donation-functions.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/stripe/donations/charitable-stripe-donation-functions.php

154 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Stripe donation functions.
4 *
5 * @package Charitable Stripe/Functions/Donations
6 * @author David Bisset
7 * @copyright Copyright (c) 2021-2022, David Bisset
8 * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
9 * @since 1.3.0
10 * @version 1.4.4
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Return the charge id for a particular donation.
20 *
21 * As of version 1.3, the charge for a donation is stored as the
22 * gateway transaction key, but prior to this version only renewal
23 * donations were stored with the gateway transaction key.
24 *
25 * However, the charge id has been listed in the donation logs since
26 * 1.2.2, so if available we can grab the charge id from there. In
27 * this case, we also record the charge as the gateway transaction
28 * for future reference.
29 *
30 * @since 1.3.0
31 *
32 * @param int $donation_id The donation ID.
33 * @return false|string False if no charge id can be found. Charge id otherwise.
34 */
35 function charitable_stripe_get_charge_id_for_donation( $donation_id ) {
36 $donation = charitable_get_donation( $donation_id );
37 $charge_id = $donation->get_gateway_transaction_id();
38
39 if ( $charge_id ) {
40 return $charge_id;
41 }
42
43 /* Return false by default. */
44 $charge_id = false;
45
46 /* Pattern to match in the donation logs. */
47 $pattern = '/dashboard\.stripe\.com\/(?:test\/)?payments\/([a-z,A-Z,0-9,_]*)/';
48
49 foreach ( $donation->get_donation_log() as $log ) {
50 if ( false !== $charge_id ) {
51 continue;
52 }
53
54 if ( ! preg_match( $pattern, $log['message'], $matches ) ) {
55 continue;
56 }
57
58 $charge_id = $matches[1];
59 }
60
61 /**
62 * Filter the charge id.
63 *
64 * @since 1.3.0
65 *
66 * @param string|false $charge_id The charge id, or false if none was stored.
67 * @param int $donation_id The donation id.
68 */
69 $charge_id = apply_filters( 'charitable_stripe_donation_charge_id', $charge_id, $donation_id );
70
71 /* If the charge id is not false, save it. */
72 if ( $charge_id ) {
73 $donation->set_gateway_transaction_id( $charge_id );
74 }
75
76 return $charge_id;
77 }
78
79 /**
80 * Return the account id for a particular donation.
81 *
82 * By default, this will check for the _stripe_account_id meta value. If
83 * this is not set, it will return false.
84 *
85 * However, this uses a filter which allows Stripe Connect to return the
86 * account_id linked to a particular donation if that was not set.
87 *
88 * @since 1.3.0
89 *
90 * @param int $donation_id The donation ID.
91 * @return false|string False if no account id can be found. Account id otherwise.
92 */
93 function charitable_stripe_get_account_id_for_donation( $donation_id ) {
94 $account_id = get_post_meta( $donation_id, '_stripe_account_id', true );
95
96 if ( empty( $account_id ) ) {
97 $account_id = false;
98 }
99
100 /**
101 * Filter the account id.
102 *
103 * @since 1.3.0
104 *
105 * @param string|false $account_id The account id, or false if none was stored.
106 * @param int $donation_id The donation id.
107 */
108 $account_id = apply_filters( 'charitable_stripe_donation_account_id', $account_id, $donation_id );
109
110 /* If the account id is set, save it. */
111 if ( $account_id ) {
112 update_post_meta( $donation_id, '_stripe_account_id', $account_id );
113 }
114
115 return $account_id;
116 }
117
118 /**
119 * Get the metadata to pass to Stripe for a particular donation.
120 *
121 * @since 1.4.0
122 *
123 * @param Charitable_Donation $donation The donation object.
124 * @return array
125 */
126 function charitable_stripe_get_donation_metadata( Charitable_Donation $donation ) {
127 /**
128 * Filter the donation fields that are included in the donation metadata.
129 *
130 * @since 1.4.0
131 *
132 * @param array $fields The field keys.
133 */
134 $fields = apply_filters(
135 'charitable_stripe_metadata_fields',
136 array_merge(
137 [
138 'donation_id',
139 'donor',
140 ],
141 array_keys( charitable()->donation_fields()->get_donation_form_fields() )
142 )
143 );
144
145 return array_reduce(
146 $fields,
147 function( $carry, $field ) use ( $donation ) {
148 $carry[ $field ] = substr( (string) $donation->get( $field ), 0, 500 );
149 return $carry;
150 },
151 []
152 );
153 }
154