| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\Controllers; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\DonationForms\Actions\GetOrCreateDonor; |
| 7 |
use Give\DonationForms\DataTransferObjects\DonateControllerData; |
| 8 |
use Give\Donations\Models\Donation; |
| 9 |
use Give\Donors\Models\Donor; |
| 10 |
use Give\Framework\PaymentGateways\Controllers\GatewayPaymentController; |
| 11 |
use Give\Framework\PaymentGateways\Controllers\GatewaySubscriptionController; |
| 12 |
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 13 |
use Give\Framework\PaymentGateways\PaymentGateway; |
| 14 |
use Give\PaymentGateways\Actions\GetGatewayDataFromRequest; |
| 15 |
use Give\Subscriptions\Models\Subscription; |
| 16 |
|
| 17 |
/** |
| 18 |
* @since 3.0.0 |
| 19 |
*/ |
| 20 |
class DonateController |
| 21 |
{ |
| 22 |
/** |
| 23 |
* First we create a donation and/or subscription, then move on to the gateway processing |
| 24 |
* |
| 25 |
* @since 3.2.0 Pass the form ID to match updated signature for getOrCreateDonor(). |
| 26 |
* @since 3.0.0 |
| 27 |
* |
| 28 |
* @return void |
| 29 |
* @throws Exception|PaymentGatewayException |
| 30 |
*/ |
| 31 |
public function donate(DonateControllerData $formData, PaymentGateway $gateway) |
| 32 |
{ |
| 33 |
/** |
| 34 |
* Fires at the start of donation form processing, before any data is processed. |
| 35 |
* |
| 36 |
* @since 3.4.0 |
| 37 |
* |
| 38 |
* @param DonateControllerData $formData |
| 39 |
* @param string $gatewayId |
| 40 |
*/ |
| 41 |
do_action('givewp_donation_form_processing_start', $formData, $gateway::id()); |
| 42 |
|
| 43 |
$donor = $this->getOrCreateDonor( |
| 44 |
$formData->formId, |
| 45 |
$formData->wpUserId, |
| 46 |
$formData->email, |
| 47 |
$formData->firstName, |
| 48 |
$formData->lastName, |
| 49 |
$formData->honorific, |
| 50 |
$formData->phone |
| 51 |
); |
| 52 |
|
| 53 |
if ($formData->donationType->isSingle()) { |
| 54 |
$donation = $formData->toDonation($donor->id); |
| 55 |
$donation->save(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Internal hook that fires after a donation is created during the donate controller. |
| 59 |
* |
| 60 |
* @since 3.0.0 |
| 61 |
* |
| 62 |
* @param DonateControllerData $formData |
| 63 |
* @param Donation $donation |
| 64 |
* @param Subscription|null $subscription |
| 65 |
*/ |
| 66 |
do_action('givewp_donate_controller_donation_created', $formData, $donation, null); |
| 67 |
|
| 68 |
/** |
| 69 |
* Fires after a donation is created during donation form processing. |
| 70 |
* |
| 71 |
* @since 3.4.0 |
| 72 |
* |
| 73 |
* @param Donation $donation |
| 74 |
* @param Subscription|null $subscription |
| 75 |
*/ |
| 76 |
do_action('givewp_donation_form_processing_donation_created', $donation, null); |
| 77 |
|
| 78 |
/** |
| 79 |
* Filter for adding modifying custom $gatewayData sent to the $gateway->createPayment() method. |
| 80 |
* |
| 81 |
* @since 3.0.0 |
| 82 |
*/ |
| 83 |
$gatewayData = apply_filters( |
| 84 |
"givewp_create_payment_gateway_data_{$gateway::id()}", |
| 85 |
(new GetGatewayDataFromRequest)(), |
| 86 |
$donation |
| 87 |
); |
| 88 |
|
| 89 |
$controller = new GatewayPaymentController($gateway); |
| 90 |
$controller->create($donation, $gatewayData); |
| 91 |
} |
| 92 |
|
| 93 |
if ($formData->donationType->isSubscription()) { |
| 94 |
$this->validateGatewaySupportsSubscriptions($gateway); |
| 95 |
|
| 96 |
$subscription = $formData->toSubscription($donor->id); |
| 97 |
$subscription->save(); |
| 98 |
|
| 99 |
$donation = $formData->toInitialSubscriptionDonation($donor->id, $subscription->id); |
| 100 |
$donation->save(); |
| 101 |
|
| 102 |
/** |
| 103 |
* Internal hook that fires after a donation is created in the donate controller. |
| 104 |
* |
| 105 |
* @since 3.0.0 |
| 106 |
* |
| 107 |
* @param DonateControllerData $formData |
| 108 |
* @param Donation $donation |
| 109 |
* @param Subscription $subscription |
| 110 |
*/ |
| 111 |
do_action('givewp_donate_controller_donation_created', $formData, $donation, $subscription); |
| 112 |
|
| 113 |
/** |
| 114 |
* Fires after a donation is created during donation form processing. |
| 115 |
* |
| 116 |
* @since 3.4.0 |
| 117 |
* |
| 118 |
* @param Donation $donation |
| 119 |
* @param Subscription|null $subscription |
| 120 |
*/ |
| 121 |
do_action('givewp_donation_form_processing_donation_created', $donation, $subscription); |
| 122 |
|
| 123 |
/** |
| 124 |
* Internal hook that fires after a subscription is created in the donate controller. |
| 125 |
* |
| 126 |
* @since 3.0.0 |
| 127 |
* |
| 128 |
* @param DonateControllerData $formData |
| 129 |
* @param Subscription $subscription |
| 130 |
* @param Donation $donation |
| 131 |
*/ |
| 132 |
do_action('givewp_donate_controller_subscription_created', $formData, $subscription, $donation); |
| 133 |
|
| 134 |
/** |
| 135 |
* Fires after a subscription is created during donation form processing. |
| 136 |
* |
| 137 |
* @since 3.4.0 |
| 138 |
* |
| 139 |
* @param Subscription $subscription |
| 140 |
* @param Donation $donation |
| 141 |
*/ |
| 142 |
do_action('givewp_donation_form_processing_subscription_created', $subscription, $donation); |
| 143 |
|
| 144 |
/** |
| 145 |
* Filter for adding modifying custom $gatewayData sent to the $gateway->createSubscription() method. |
| 146 |
* |
| 147 |
* @since 3.0.0 |
| 148 |
*/ |
| 149 |
$gatewayData = apply_filters( |
| 150 |
"givewp_create_subscription_gateway_data_{$gateway::id()}", |
| 151 |
(new GetGatewayDataFromRequest)(), |
| 152 |
$donation, |
| 153 |
$subscription |
| 154 |
); |
| 155 |
|
| 156 |
$controller = new GatewaySubscriptionController($gateway); |
| 157 |
$controller->create($donation, $subscription, $gatewayData); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @since 3.9.0 Add support for "phone" property |
| 163 |
* @since 3.2.0 Added $formId to the signature for passing to do_action hooks. Added honorific and use GetOrCreateDonor action |
| 164 |
* @since 3.0.0 |
| 165 |
* |
| 166 |
* @throws Exception |
| 167 |
*/ |
| 168 |
private function getOrCreateDonor( |
| 169 |
int $formId, |
| 170 |
?int $userId, |
| 171 |
string $donorEmail, |
| 172 |
string $firstName, |
| 173 |
string $lastName, |
| 174 |
?string $honorific, |
| 175 |
?string $donorPhone |
| 176 |
): Donor { |
| 177 |
$getOrCreateDonorAction = new GetOrCreateDonor(); |
| 178 |
|
| 179 |
$donor = $getOrCreateDonorAction( |
| 180 |
$userId, |
| 181 |
$donorEmail, |
| 182 |
$firstName, |
| 183 |
$lastName, |
| 184 |
$honorific, |
| 185 |
$donorPhone |
| 186 |
); |
| 187 |
|
| 188 |
if ($getOrCreateDonorAction->donorCreated) { |
| 189 |
/** |
| 190 |
* Internal hook to differentiate when a v3 form creates a new donor. |
| 191 |
* |
| 192 |
* @since 3.2.0 |
| 193 |
* @param Donor $donor |
| 194 |
* @param int $formId |
| 195 |
*/ |
| 196 |
do_action('givewp_donate_controller_donor_created', $donor, $formId); |
| 197 |
|
| 198 |
/** |
| 199 |
* Fires after a donor is created during donation form processing. |
| 200 |
* |
| 201 |
* @since 3.4.0 |
| 202 |
* |
| 203 |
* @param Donor $donor |
| 204 |
* @param int $formId |
| 205 |
*/ |
| 206 |
do_action('givewp_donation_form_processing_donor_created', $donor, $formId); |
| 207 |
} |
| 208 |
|
| 209 |
return $donor; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @throws PaymentGatewayException |
| 214 |
*/ |
| 215 |
private function validateGatewaySupportsSubscriptions(PaymentGateway $gateway) |
| 216 |
{ |
| 217 |
if (!$gateway->supportsSubscriptions()) { |
| 218 |
$gatewayName = $gateway->getName(); |
| 219 |
|
| 220 |
throw new PaymentGatewayException( |
| 221 |
sprintf( |
| 222 |
__( |
| 223 |
"[%s] This payment gateway does not support recurring payments, please try selecting another payment gateway.", |
| 224 |
'give' |
| 225 |
), |
| 226 |
$gatewayName |
| 227 |
) |
| 228 |
); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|