DataTransferObjects
4 years ago
Models
4 years ago
Repositories
4 years ago
Webhooks
4 years ago
AccountAdminNotices.php
4 years ago
AdminSettingFields.php
4 years ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
4 years ago
DonationDetailsPage.php
4 years ago
DonationFormPaymentMethod.php
4 years ago
DonationProcessor.php
4 years ago
PayPalClient.php
4 years ago
PayPalCommerce.php
4 years ago
RefreshToken.php
4 years ago
RefundPaymentHandler.php
4 years ago
ScriptLoader.php
4 years ago
Utils.php
4 years ago
onBoardingRedirectHandler.php
4 years ago
DonationProcessor.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | use Give\PaymentGateways\PayPalCommerce\Models\PayPalOrder; |
| 6 | use PayPalCheckoutSdk\Orders\OrdersGetRequest; |
| 7 | |
| 8 | /** |
| 9 | * Class DonationProcessor |
| 10 | * @package Give\PaymentGateways\PayPalCommerce |
| 11 | * |
| 12 | * @since 2.9.0 |
| 13 | */ |
| 14 | class DonationProcessor |
| 15 | { |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | private $donationFormData; |
| 20 | |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | private $formId; |
| 25 | |
| 26 | /** |
| 27 | * Handle donation form submission. |
| 28 | * |
| 29 | * @since 2.9.0 |
| 30 | * |
| 31 | * @param array $donationFormData |
| 32 | * |
| 33 | */ |
| 34 | public function handle($donationFormData) |
| 35 | { |
| 36 | $this->donationFormData = (array)$donationFormData; |
| 37 | |
| 38 | if ( ! $this->isOneTimeDonation()) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $this->formId = absint($this->donationFormData['post_data']['give-form-id']); |
| 43 | |
| 44 | $donationData = [ |
| 45 | 'price' => $this->donationFormData['price'], |
| 46 | 'give_form_title' => $this->donationFormData['post_data']['give-form-title'], |
| 47 | 'give_form_id' => $this->formId, |
| 48 | 'give_price_id' => isset($this->donationFormData['post_data']['give-price-id']) ? $this->donationFormData['post_data']['give-price-id'] : '', |
| 49 | 'date' => $this->donationFormData['date'], |
| 50 | 'user_email' => $this->donationFormData['user_email'], |
| 51 | 'purchase_key' => $this->donationFormData['purchase_key'], |
| 52 | 'currency' => give_get_currency(), |
| 53 | 'user_info' => $this->donationFormData['user_info'], |
| 54 | 'status' => 'pending', |
| 55 | 'gateway' => $this->donationFormData['gateway'], |
| 56 | ]; |
| 57 | |
| 58 | $donationId = give_insert_payment($donationData); |
| 59 | |
| 60 | if ( ! $donationId) { |
| 61 | $this->redirectBackToDonationForm(); |
| 62 | } |
| 63 | |
| 64 | $this->redirectDonorToSuccessPage($donationId); |
| 65 | |
| 66 | exit(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Return back to donation form page after logging error. |
| 71 | * |
| 72 | * @since 2.9.0 |
| 73 | */ |
| 74 | private function redirectBackToDonationForm() |
| 75 | { |
| 76 | // Record the error. |
| 77 | give_record_gateway_error( |
| 78 | esc_html__('Payment Error', 'give'), |
| 79 | /* translators: %s: payment data */ |
| 80 | sprintf( |
| 81 | esc_html__( |
| 82 | 'The payment creation failed before processing the PayPalCommerce gateway request. Payment data: %s', |
| 83 | 'give' |
| 84 | ), |
| 85 | print_r($this->donationFormData, true) |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | give_set_error( |
| 90 | 'give', |
| 91 | esc_html__('An error occurred while processing your payment. Please try again.', 'give') |
| 92 | ); |
| 93 | |
| 94 | // Problems? Send back. |
| 95 | give_send_back_to_checkout(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Redirect donor to success page. |
| 100 | * |
| 101 | * @since 2.9.0 |
| 102 | * |
| 103 | * @param int $donationId |
| 104 | * |
| 105 | */ |
| 106 | private function redirectDonorToSuccessPage($donationId) |
| 107 | { |
| 108 | $orderDetailRequest = new OrdersGetRequest($this->donationFormData['post_data']['payPalOrderId']); |
| 109 | $orderDetails = (array)give(PayPalClient::class)->getHttpClient()->execute($orderDetailRequest)->result; |
| 110 | |
| 111 | $order = PayPalOrder::fromArray($orderDetails); |
| 112 | |
| 113 | give_insert_payment_note( |
| 114 | $donationId, |
| 115 | sprintf( |
| 116 | __('Transaction Successful. PayPal Transaction ID: %1$s PayPal Order ID: %2$s', 'give'), |
| 117 | $order->payment->id, |
| 118 | $order->id |
| 119 | ) |
| 120 | ); |
| 121 | give_set_payment_transaction_id($donationId, $order->payment->id); |
| 122 | give('payment_meta')->update_meta($donationId, '_give_order_id', $order->id); |
| 123 | |
| 124 | // Do not need to set donation to complete if already completed by PayPal webhook. |
| 125 | if ('COMPLETED' === $order->payment->status) { |
| 126 | give_update_payment_status($donationId); |
| 127 | } |
| 128 | |
| 129 | wp_safe_redirect( |
| 130 | add_query_arg( |
| 131 | ['payment-confirmation' => 'paypal-commerce'], |
| 132 | give_get_success_page_url() |
| 133 | ) |
| 134 | ); |
| 135 | |
| 136 | exit(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return whether or not donation is onetime. |
| 141 | * |
| 142 | * @since 2.9.0 |
| 143 | * |
| 144 | * @return bool |
| 145 | */ |
| 146 | private function isOneTimeDonation() |
| 147 | { |
| 148 | return array_key_exists('post_data', $this->donationFormData) && array_key_exists( |
| 149 | 'payPalOrderId', |
| 150 | $this->donationFormData['post_data'] |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 |