function commerce_stripe_pi_capture in Commerce Stripe Payment Intent 7
Capture a transaction.
Parameters
object $transaction: The commerce payment transaction object.
string $amount: The amount to capture.
1 call to commerce_stripe_pi_capture()
- commerce_stripe_pi_capture_form_submit in includes/
commerce_stripe_pi.admin.inc - Submit handler: process a prior authorization capture via AIM.
File
- ./
commerce_stripe_pi.module, line 2083 - Payment intent stripe payment integration.
Code
function commerce_stripe_pi_capture($transaction, $amount, $is_decimal = FALSE) {
if (!commerce_stripe_pi_load_library()) {
watchdog('commerce_stripe_pi', 'Error loading payment intent library to capture payment : @transaction', array(
'@transaction' => print_r($transaction, TRUE),
), WATCHDOG_ERROR);
return;
}
// Set the amount that is to be captured. This amount has already been
// validated, but needs to be converted to cents for Stripe.
$capture_amount = $amount;
if ($is_decimal) {
$dec_amount = number_format($amount, 2, '.', '');
$capture_amount = commerce_currency_decimal_to_amount($dec_amount, $transaction->currency_code);
}
$cparams = array(
'amount_to_capture' => $capture_amount,
);
try {
$paymentIntent = PaymentIntent::retrieve($transaction->remote_id);
// Should only capture payment intent requiring capture.
if ($paymentIntent->status !== COMMERCE_STRIPE_PI_REQUIRES_CAPTURE) {
watchdog('commerce_stripe_pi', 'Following payment intent @paymentintent_id can\'t be captured because his status is not \'requires_capture\' but \'@status\'.', array(
'@paymentintent_id' => $paymentIntent->id,
'@status' => $paymentIntent->status,
), WATCHDOG_NOTICE);
return;
}
drupal_alter('commerce_stripe_pi_capture', $transaction, $paymentIntent, $cparams);
$response = $paymentIntent
->capture($cparams);
if (method_exists($response, '__toJSON')) {
$response_json = $response
->__toJSON();
}
else {
$response_json = $response
->toJSON();
}
$transaction->payload[REQUEST_TIME] = $response_json;
$transaction->remote_status = commerce_stripe_pi_get_remote_status(NULL, $transaction, 'capture');
$transaction->message .= '<br />' . t('Captured: @date', array(
'@date' => format_date(REQUEST_TIME, 'short'),
));
$transaction->message .= '<br />' . t('Captured Amount: @amount', array(
'@amount' => commerce_currency_amount_to_decimal($capture_amount, $transaction->currency_code),
));
$transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$transaction->amount = $capture_amount;
commerce_payment_transaction_save($transaction);
} catch (Exception $e) {
drupal_set_message(t('We received the following error when trying to capture the transaction.'), 'error');
drupal_set_message(check_plain($e
->getMessage()), 'error');
watchdog('commerce_stripe_pi', 'Following error received when processing card for capture @paymentintent : @stripe_pi_error.', array(
'@paymentintent' => $transaction->remote_id,
'@stripe_pi_error' => $e
->getMessage(),
), WATCHDOG_NOTICE);
$transaction->payload[REQUEST_TIME] = $e->json_body;
$transaction->message = t('Capture processing error: @stripe_pi_error', array(
'@stripe_pi_error' => $e
->getMessage(),
));
$transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
$transaction->remote_status = 'FAILED';
commerce_payment_transaction_save($transaction);
}
}