Skip to content

Commit

Permalink
Track stripe changes in core
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Sep 6, 2021
1 parent 739f9f8 commit e718b9c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
53 changes: 31 additions & 22 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Admin\Models\Payments_model;
use Event;
use Igniter\Flame\Exception\ApplicationException;
use Omnipay\Omnipay;
use Stripe\StripeClient;
use System\Classes\BaseExtension;
use Thoughtco\OrderApprover\Events\OrderCreated;

Expand Down Expand Up @@ -56,17 +56,21 @@ public function boot()
$intentId = $this->getIntentFromOrder($order);

$gateway = $this->createGateway($order->payment_method);

try {

$response = $gateway->capture([
'paymentIntentReference' => $intentId,
])->send();
$response = $gateway->paymentIntents->capture($intentId, []);

if ($response->isSuccessful()) {
$order->logPaymentAttempt('Payment captured successfully', 1, [], $response->getData());
return;
if ($response->status == 'succeeded') {
$order->logPaymentAttempt('Payment captured successfully', 1, [], $response);
return;
}

throw new Exception('Status '.$response->status);

} catch (Exception $e) {
$order->logPaymentAttempt('Payment capture failed -> '.$e->getMessage(), 0, [], $response);
}

$order->logPaymentAttempt('Payment capture failed -> '.$response->getMessage(), 0, [], $response->getData());
});

// order rejected through orderApprover extension - cancel payment
Expand All @@ -80,18 +84,21 @@ public function boot()
$intentId = $this->getIntentFromOrder($order);

$gateway = $this->createGateway($order->payment_method);

try {

$response = $gateway->cancel([
'paymentIntentReference' => $intentId,
])->send();

$data = $response->getData();
if (array_get($data, 'status') === 'canceled') {
$order->logPaymentAttempt('Payment cancelled successfully', 1, [], $data);
return;
}

$order->logPaymentAttempt('Payment cancellation failed -> '.$response->getMessage(), 0, [], $data);
$response = $gateway->paymentIntents->cancel($intentId, []);

if ($response->status == 'canceled') {
$order->logPaymentAttempt('Payment cancelled successfully', 1, [], $response);
return;
}

throw new Exception('Status '.$response->status);

} catch (Exception $e) {
$order->logPaymentAttempt('Payment cancellation failed -> '.$e->getMessage(), 0, [], $response);
}

});
}
Expand All @@ -116,8 +123,10 @@ protected function getIntentFromOrder($order)

protected function createGateway($paymentMethod)
{
$gateway = Omnipay::create('Stripe\PaymentIntents');
$gateway->setApiKey($paymentMethod->transaction_mode != 'live' ? $paymentMethod->test_secret_key : $paymentMethod->live_secret_key);
$gateway = new StripeClient([
'api_key' => $paymentMethod->transaction_mode != 'live' ? $paymentMethod->test_secret_key : $paymentMethod->live_secret_key,
]);

return $gateway;
}

Expand Down
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "thoughtco/ti-ext-stripeauthorize",
"type": "tastyigniter-extension",
"keywords": ["stripe"],
"require": {
"stripe/stripe-php": "~7.93.0",
}
}
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"code": "thoughtco.stripeauthorize",
"name": "Stripe Authorize",
"description": "Listen for Order Approver events to capture or cancel Stripe payments",
"version": "v1.0.7",
"version": "v1.1.0",
"author": "ryanmitchell",
"icon": {
"class": "fa fa-funnel-dollar",
Expand Down

0 comments on commit e718b9c

Please sign in to comment.