Actions
3 years ago
Controllers
3 years ago
Migrations
2 years ago
Views
3 years ago
Webhooks
4 years ago
resources
2 years ago
PayPalStandard.php
2 years ago
PayPalStandard.php
285 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\PayPalStandard; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\Donations\ValueObjects\DonationStatus; |
| 7 | use Give\Framework\Exceptions\Primitives\Exception; |
| 8 | use Give\Framework\Http\Response\Types\RedirectResponse; |
| 9 | use Give\Framework\PaymentGateways\Commands\RedirectOffsite; |
| 10 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 11 | use Give\Framework\PaymentGateways\Traits\HandleHttpResponses; |
| 12 | use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile; |
| 13 | use Give\Helpers\Language; |
| 14 | use Give\PaymentGateways\Gateways\PayPalStandard\Actions\CreatePayPalStandardPaymentURL; |
| 15 | use Give\PaymentGateways\Gateways\PayPalStandard\Controllers\PayPalStandardWebhook; |
| 16 | use Give\PaymentGateways\Gateways\PayPalStandard\Views\PayPalStandardBillingFields; |
| 17 | |
| 18 | /** |
| 19 | * @since 3.0.0 added support for Give 3.0 forms |
| 20 | * @since 2.19.0 |
| 21 | */ |
| 22 | class PayPalStandard extends PaymentGateway |
| 23 | { |
| 24 | use HandleHttpResponses; |
| 25 | use HasScriptAssetFile; |
| 26 | |
| 27 | public $routeMethods = [ |
| 28 | 'handleIpnNotification', |
| 29 | ]; |
| 30 | |
| 31 | public $secureRouteMethods = [ |
| 32 | 'handleSuccessPaymentReturn', |
| 33 | 'handleCancelledPaymentReturn', |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * @since 2.19.0 |
| 38 | */ |
| 39 | public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 40 | { |
| 41 | return (new PayPalStandardBillingFields())($formId); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @since 2.19.0 |
| 46 | */ |
| 47 | public static function id(): string |
| 48 | { |
| 49 | return 'paypal'; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @since 2.19.0 |
| 54 | */ |
| 55 | public function getId(): string |
| 56 | { |
| 57 | return self::id(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 2.19.0 |
| 62 | */ |
| 63 | public function getName(): string |
| 64 | { |
| 65 | return __('PayPal Standard', 'give'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @since 2.19.0 |
| 70 | */ |
| 71 | public function getPaymentMethodLabel(): string |
| 72 | { |
| 73 | return esc_html__('PayPal', 'give'); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @since 3.0.0 |
| 78 | */ |
| 79 | public function formSettings(int $formId): array |
| 80 | { |
| 81 | return [ |
| 82 | 'fields' => [ |
| 83 | 'heading' => __('Make your donation quickly and securely with PayPal', 'give'), |
| 84 | 'subheading' => __('How it works', 'give'), |
| 85 | 'body' => __( |
| 86 | 'You will be redirected to PayPal to complete your donation with your debit card, credit card, or with your PayPal account. Once complete, you will be redirected back to this site to view your receipt.', |
| 87 | 'give' |
| 88 | ), |
| 89 | ] |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 3.1.0 set translations for script |
| 95 | * @since 3.0.0 |
| 96 | */ |
| 97 | public function enqueueScript(int $formId) |
| 98 | { |
| 99 | $assets = $this->getScriptAsset(GIVE_PLUGIN_DIR . 'build/payPalStandardGateway.asset.php'); |
| 100 | $handle = $this::id(); |
| 101 | |
| 102 | wp_enqueue_script( |
| 103 | $handle, |
| 104 | GIVE_PLUGIN_URL . 'build/payPalStandardGateway.js', |
| 105 | $assets['dependencies'], |
| 106 | $assets['version'], |
| 107 | true |
| 108 | ); |
| 109 | |
| 110 | Language::setScriptTranslations($handle); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @since 3.0.0 update to add `givewp-return-url` to the query params |
| 115 | * @since 2.19.0 |
| 116 | */ |
| 117 | public function createPayment(Donation $donation, $gatewayData = []): RedirectOffsite |
| 118 | { |
| 119 | return new RedirectOffsite( |
| 120 | (new CreatePayPalStandardPaymentURL())( |
| 121 | $donation, |
| 122 | $this->generateSecureGatewayRouteUrl( |
| 123 | 'handleSuccessPaymentReturn', |
| 124 | $donation->id, |
| 125 | [ |
| 126 | 'donation-id' => $donation->id, |
| 127 | 'givewp-return-url' => $gatewayData['successUrl'] |
| 128 | ] |
| 129 | ), |
| 130 | $this->generateSecureGatewayRouteUrl( |
| 131 | 'handleCancelledPaymentReturn', |
| 132 | $donation->id, |
| 133 | [ |
| 134 | 'donation-id' => $donation->id, |
| 135 | 'givewp-return-url' => $gatewayData['cancelUrl'] |
| 136 | ] |
| 137 | ), |
| 138 | $this->generateGatewayRouteUrl( |
| 139 | 'handleIpnNotification' |
| 140 | ) |
| 141 | ) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Handle payment redirect after successful payment on PayPal standard. |
| 147 | * |
| 148 | * @since 3.0.0 update to use the $gatewayParams return url |
| 149 | * @since 2.19.0 |
| 150 | * @since 2.19.4 Only pending PayPal Standard donation set to processing. |
| 151 | * @since 2.19.6 1. Do not set donation to "processing" 2. Add "payment-confirmation" param to receipt page url |
| 152 | */ |
| 153 | protected function handleSuccessPaymentReturn(array $queryParams): RedirectResponse |
| 154 | { |
| 155 | return new RedirectResponse(esc_url_raw($queryParams['givewp-return-url'])); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * This method is called when the user cancels the payment on PayPal. |
| 160 | * |
| 161 | * @since 3.0.0 |
| 162 | */ |
| 163 | protected function handleCancelledPaymentReturn(array $queryParams): RedirectResponse |
| 164 | { |
| 165 | $donationId = (int)$queryParams['donation-id']; |
| 166 | |
| 167 | /** @var Donation $donation */ |
| 168 | $donation = Donation::find($donationId); |
| 169 | $donation->status = DonationStatus::CANCELLED(); |
| 170 | $donation->save(); |
| 171 | |
| 172 | return new RedirectResponse(esc_url_raw($queryParams['givewp-return-url'])); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Handle PayPal IPN notification. |
| 177 | * |
| 178 | * @since 2.19.0 |
| 179 | */ |
| 180 | public function handleIpnNotification() |
| 181 | { |
| 182 | give(PayPalStandardWebhook::class)->handle(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * This function returns payment gateway settings. |
| 187 | * |
| 188 | * @since 2.19.0 |
| 189 | */ |
| 190 | public function getOptions(): array |
| 191 | { |
| 192 | $setting = [ |
| 193 | // Section 2: PayPal Standard. |
| 194 | [ |
| 195 | 'type' => 'title', |
| 196 | 'id' => 'give_title_gateway_settings_2', |
| 197 | ], |
| 198 | [ |
| 199 | 'name' => esc_html__('PayPal Email', 'give'), |
| 200 | 'desc' => esc_html__( |
| 201 | 'Enter the email address associated with your PayPal account to connect with the gateway.', |
| 202 | 'give' |
| 203 | ), |
| 204 | 'id' => 'paypal_email', |
| 205 | 'type' => 'email', |
| 206 | ], |
| 207 | [ |
| 208 | 'name' => esc_html__('PayPal Page Style', 'give'), |
| 209 | 'desc' => esc_html__( |
| 210 | 'Enter the name of the PayPal page style to use, or leave blank to use the default.', |
| 211 | 'give' |
| 212 | ), |
| 213 | 'id' => 'paypal_page_style', |
| 214 | 'type' => 'text', |
| 215 | ], |
| 216 | [ |
| 217 | 'name' => esc_html__('PayPal Transaction Type', 'give'), |
| 218 | 'desc' => esc_html__( |
| 219 | 'Nonprofits must verify their status to withdraw donations they receive via PayPal. PayPal users that are not verified nonprofits must demonstrate how their donations will be used, once they raise more than $10,000. By default, GiveWP transactions are sent to PayPal as donations. You may change the transaction type using this option if you feel you may not meet PayPal\'s donation requirements.', |
| 220 | 'give' |
| 221 | ), |
| 222 | 'id' => 'paypal_button_type', |
| 223 | 'type' => 'radio_inline', |
| 224 | 'options' => [ |
| 225 | 'donation' => esc_html__('Donation', 'give'), |
| 226 | 'standard' => esc_html__('Standard Transaction', 'give'), |
| 227 | ], |
| 228 | 'default' => 'donation', |
| 229 | ], |
| 230 | [ |
| 231 | 'name' => esc_html__('Billing Details', 'give'), |
| 232 | 'desc' => esc_html__( |
| 233 | 'If enabled, required billing address fields are added to PayPal Standard forms. These fields are not required by PayPal to process the transaction, but you may have a need to collect the data. Billing address details are added to both the donation and donor record in GiveWP.', |
| 234 | 'give' |
| 235 | ), |
| 236 | 'id' => 'paypal_standard_billing_details', |
| 237 | 'type' => 'radio_inline', |
| 238 | 'default' => 'disabled', |
| 239 | 'options' => [ |
| 240 | 'enabled' => esc_html__('Enabled', 'give'), |
| 241 | 'disabled' => esc_html__('Disabled', 'give'), |
| 242 | ], |
| 243 | ], |
| 244 | [ |
| 245 | 'id' => 'paypal_invoice_prefix', |
| 246 | 'name' => esc_html__('Invoice ID Prefix', 'give'), |
| 247 | 'desc' => esc_html__( |
| 248 | 'Enter a prefix for your invoice numbers. If you use your PayPal account for multiple fundraising platforms or ecommerce stores, ensure this prefix is unique. PayPal will not allow orders or donations with the same invoice number.', |
| 249 | 'give' |
| 250 | ), |
| 251 | 'type' => 'text', |
| 252 | 'default' => 'GIVE-', |
| 253 | ], |
| 254 | [ |
| 255 | 'name' => esc_html__('PayPal Standard Gateway Settings Docs Link', 'give'), |
| 256 | 'id' => 'paypal_standard_gateway_settings_docs_link', |
| 257 | 'url' => esc_url('http://docs.givewp.com/settings-gateway-paypal-standard'), |
| 258 | 'title' => esc_html__('PayPal Standard Gateway Settings', 'give'), |
| 259 | 'type' => 'give_docs_link', |
| 260 | ], |
| 261 | [ |
| 262 | 'type' => 'sectionend', |
| 263 | 'id' => 'give_title_gateway_settings_2', |
| 264 | ], |
| 265 | ]; |
| 266 | |
| 267 | /** |
| 268 | * filter the settings. |
| 269 | * |
| 270 | * @since 2.9.6 |
| 271 | */ |
| 272 | return apply_filters('give_get_settings_paypal_standard', $setting); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @since 2.20.0 |
| 277 | * @inerhitDoc |
| 278 | * @throws Exception |
| 279 | */ |
| 280 | public function refundDonation(Donation $donation) |
| 281 | { |
| 282 | throw new Exception('Method has not been implemented yet. Please use the legacy method in the meantime.'); |
| 283 | } |
| 284 | } |
| 285 |