| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\PayPalCommerce; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalOrder; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class RefundPaymentHandler |
| 10 |
* |
| 11 |
* @since 2.9.0 |
| 12 |
*/ |
| 13 |
class RefundPaymentHandler |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @since 2.9.0 |
| 17 |
* |
| 18 |
* @var PayPalOrder |
| 19 |
*/ |
| 20 |
private $ordersRepository; |
| 21 |
|
| 22 |
/** |
| 23 |
* RefundPaymentHandler constructor. |
| 24 |
* |
| 25 |
* @since 2.9.0 |
| 26 |
* |
| 27 |
* @param PayPalOrder $ordersRepository |
| 28 |
*/ |
| 29 |
public function __construct(PayPalOrder $ordersRepository) |
| 30 |
{ |
| 31 |
$this->ordersRepository = $ordersRepository; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Refunds the payment when the donation is marked as refunded |
| 36 |
* |
| 37 |
* @since 2.9.0 |
| 38 |
* |
| 39 |
* @param int $donationId |
| 40 |
* |
| 41 |
* @throws Exception |
| 42 |
*/ |
| 43 |
public function refundPayment($donationId) |
| 44 |
{ |
| 45 |
if ( ! $this->isAdminOptInToRefundPaymentOnPayPal()) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$payPalPaymentId = give_get_payment_transaction_id($donationId); |
| 50 |
$paymentGateway = give_get_payment_gateway($donationId); |
| 51 |
$newDonationStatus = give_clean($_POST['give-payment-status']); |
| 52 |
|
| 53 |
if ('refunded' !== $newDonationStatus || PayPalCommerce::GATEWAY_ID !== $paymentGateway) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
try { |
| 58 |
$this->ordersRepository->refundPayment($payPalPaymentId); |
| 59 |
} catch (Exception $ex) { |
| 60 |
wp_safe_redirect( |
| 61 |
admin_url( |
| 62 |
"edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&id={$donationId}&paypal-error=refund-failure" |
| 63 |
) |
| 64 |
); |
| 65 |
exit(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* show Paypal Commerce payment refund failure notice. |
| 71 |
* |
| 72 |
* @since 2.9.0 |
| 73 |
*/ |
| 74 |
public function showPaymentRefundFailureNotice() |
| 75 |
{ |
| 76 |
if ( ! isset($_GET['paypal-error']) || 'refund-failure' !== $_GET['paypal-error']) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
give('notices')->register_notice( |
| 81 |
[ |
| 82 |
'id' => 'give-paypal-commerce-refund-failure', |
| 83 |
'type' => 'warning', |
| 84 |
'show' => true, |
| 85 |
'description' => sprintf( |
| 86 |
'<strong>%1$s</strong> %2$s %3$s<a href="%4$s" target="_blank">%5$s</a>%6$s', |
| 87 |
esc_html__('PayPal Donations:', 'give'), |
| 88 |
esc_html__('We were unable to process refund.', 'give'), |
| 89 |
esc_html__('Please ', 'give'), |
| 90 |
admin_url('edit.php?post_type=give_forms&page=give-tools&tab=logs'), |
| 91 |
esc_html__('check log', 'give'), |
| 92 |
esc_html__(' for detailed information.', 'give') |
| 93 |
), |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* This function will display field to opt for refund. |
| 100 |
* |
| 101 |
* @since 2.5.0 |
| 102 |
* |
| 103 |
* @param int $donationId Donation ID. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function optInForRefundFormField($donationId) |
| 108 |
{ |
| 109 |
if (PayPalCommerce::GATEWAY_ID !== give_get_payment_gateway($donationId)) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
?> |
| 114 |
<div id="give-paypal-commerce-opt-refund-wrap" |
| 115 |
class="give-paypal-commerce-opt-refund give-admin-box-inside give-hidden"> |
| 116 |
<p> |
| 117 |
<input type="checkbox" id="give-paypal-commerce-opt-refund" |
| 118 |
name="give_paypal_donations_optin_for_refund" value="1" /> |
| 119 |
<label for="give-paypal-commerce-opt-refund"> |
| 120 |
<?php |
| 121 |
esc_html_e('Refund Charge in PayPal?', 'give'); ?> |
| 122 |
</label> |
| 123 |
</p> |
| 124 |
</div> |
| 125 |
|
| 126 |
<?php |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Return whether or not admin optin for refund payment in PayPal |
| 131 |
* |
| 132 |
* @since 2.9.0 |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
private function isAdminOptInToRefundPaymentOnPayPal() |
| 137 |
{ |
| 138 |
return ! empty($_POST['give_paypal_donations_optin_for_refund']) ? |
| 139 |
(bool)absint($_POST['give_paypal_donations_optin_for_refund']) |
| 140 |
: false; |
| 141 |
} |
| 142 |
} |
| 143 |
|