| 1 |
<?php |
| 2 |
/** |
| 3 |
* Give - Stripe Core Payment Intent |
| 4 |
* |
| 5 |
* @since 2.5.0 |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Stripe Core |
| 9 |
* @copyright Copyright (c) 2019, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
*/ |
| 12 |
|
| 13 |
use Give\PaymentGateways\Stripe\ApplicationFee; |
| 14 |
|
| 15 |
// Exit, if accessed directly. |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Check for class Give_Stripe_Payment_Intent exists. |
| 23 |
* |
| 24 |
* @since 2.5.0 |
| 25 |
*/ |
| 26 |
if ( ! class_exists( 'Give_Stripe_Payment_Intent' ) ) { |
| 27 |
|
| 28 |
class Give_Stripe_Payment_Intent { |
| 29 |
|
| 30 |
public function __construct() { |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* This function is used to create payment intent in Stripe. |
| 36 |
* |
| 37 |
* @since 2.27.1 Stripe payment intent error sets a more user friendly Exception message. |
| 38 |
* @since 2.5.0 |
| 39 |
* @access public |
| 40 |
* |
| 41 |
* @param array $args List of parameters required to create payment intent. |
| 42 |
* |
| 43 |
* @return bool|\Stripe\PaymentIntent |
| 44 |
*/ |
| 45 |
public function create( $args ) { |
| 46 |
|
| 47 |
// Add application fee, if the Stripe premium add-on is not active. |
| 48 |
if ( ApplicationFee::canAddfee() ) { |
| 49 |
$args['application_fee_amount'] = give_stripe_get_application_fee_amount( $args['amount'] ); |
| 50 |
} |
| 51 |
|
| 52 |
// Set Stripe Application Info. |
| 53 |
give_stripe_set_app_info(); |
| 54 |
|
| 55 |
try { |
| 56 |
return \Stripe\PaymentIntent::create( |
| 57 |
$args, |
| 58 |
give_stripe_get_connected_account_options() |
| 59 |
); |
| 60 |
} catch (Exception $e) { |
| 61 |
give_record_gateway_error( |
| 62 |
__('Stripe Payment Intent Error', 'give'), |
| 63 |
sprintf( |
| 64 |
/* translators: %s Exception Error Message */ |
| 65 |
__('Unable to create a payment intent. Details: %s', 'give'), |
| 66 |
$e->getMessage() |
| 67 |
) |
| 68 |
); |
| 69 |
|
| 70 |
give_set_error('stripe_payment_intent_error', |
| 71 |
sprintf( |
| 72 |
__('There was an issue with your donation transaction: %s<br>Please check your payment method or contact your card issuer for assistance. If the issue persists, try a different payment method or contact the site administrators.', 'give'), |
| 73 |
$e->getMessage() |
| 74 |
) |
| 75 |
); |
| 76 |
give_send_back_to_checkout( '?payment-mode=' . give_clean( $_GET['payment-mode'] ) ); |
| 77 |
return false; |
| 78 |
} // End try(). |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* This function is used to retrieve payment intent in Stripe. |
| 83 |
* |
| 84 |
* @param string $client_secret Client Secret represents unique string for the payment intent. |
| 85 |
* |
| 86 |
* @since 2.5.0 |
| 87 |
* @access public |
| 88 |
* |
| 89 |
* @return \Stripe\PaymentIntent |
| 90 |
*/ |
| 91 |
public function retrieve( $client_secret ) { |
| 92 |
|
| 93 |
// Set Application Info. |
| 94 |
give_stripe_set_app_info(); |
| 95 |
|
| 96 |
try { |
| 97 |
return \Stripe\PaymentIntent::retrieve( |
| 98 |
$client_secret, |
| 99 |
give_stripe_get_connected_account_options() |
| 100 |
); |
| 101 |
} catch ( Exception $e ) { |
| 102 |
|
| 103 |
give_record_gateway_error( |
| 104 |
__( 'Stripe Payment Intent Error', 'give' ), |
| 105 |
sprintf( |
| 106 |
/* translators: %s Exception Error Message */ |
| 107 |
__( 'Unable to retrieve a payment intent. Details: %s', 'give' ), |
| 108 |
$e |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
give_set_error( 'stripe_payment_intent_error', __( 'Error retrieving payment intent with Stripe. Please try again.', 'give' ) ); |
| 113 |
} // End try(). |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* This function is used to update existing payment intent in Stripe. |
| 118 |
* |
| 119 |
* @param string $client_secret Client Secret represents unique string for the payment intent. |
| 120 |
* @param array $args List of parameters required to create payment intent. |
| 121 |
* |
| 122 |
* @since 2.5.0 |
| 123 |
* @access public |
| 124 |
* |
| 125 |
* @return \Stripe\PaymentIntent |
| 126 |
*/ |
| 127 |
public function update( $client_secret, $args ) { |
| 128 |
|
| 129 |
// Add application fee, if the Stripe premium add-on is not active. |
| 130 |
if ( ApplicationFee::canAddfee() ) { |
| 131 |
$args['application_fee_amount'] = give_stripe_format_amount( give_stripe_get_application_fee_amount( $args['amount'] ) ); |
| 132 |
} |
| 133 |
|
| 134 |
// Set Stripe Application Info. |
| 135 |
give_stripe_set_app_info(); |
| 136 |
|
| 137 |
try { |
| 138 |
return \Stripe\PaymentIntent::update( |
| 139 |
$client_secret, |
| 140 |
$args, |
| 141 |
give_stripe_get_connected_account_options() |
| 142 |
); |
| 143 |
} catch (Exception $e) { |
| 144 |
give_record_gateway_error( |
| 145 |
__('Stripe Payment Intent Error', 'give'), |
| 146 |
sprintf( |
| 147 |
/* translators: %s Exception Error Message */ |
| 148 |
__('Unable to update a payment intent. Details: %s', 'give'), |
| 149 |
$e->getMessage() |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
give_set_error('stripe_payment_intent_error', |
| 154 |
__('Error updating payment intent with Stripe. Please try again.', 'give')); |
| 155 |
} // End try(). |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|