Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Nov 3, 2023
1 parent 51009ef commit 6223eb2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion payment/stripe/config/stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
| Reference: https://stripe.com/docs/api/charges/object
*/
'status_mapping' => [
\Stripe\PaymentIntent::STATUS_REQUIRES_CAPTURE => 'awaiting-payment',
\Stripe\PaymentIntent::STATUS_REQUIRES_CAPTURE => 'requires-capture',
\Stripe\PaymentIntent::STATUS_CANCELED => 'cancelled',
\Stripe\PaymentIntent::STATUS_PROCESSING => 'processing',
\Stripe\PaymentIntent::STATUS_REQUIRES_ACTION => 'awaiting-payment',
Expand Down
3 changes: 2 additions & 1 deletion payment/stripe/src/Actions/UpdateOrderFromIntent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final public static function execute(
$charges = StripeFacade::getCharges($paymentIntent->id);

$order = app(StoreCharges::class)->store($order, $charges);
$requiresCapture = $paymentIntent->status === PaymentIntent::STATUS_REQUIRES_CAPTURE;

$statuses = config('lunar.stripe.status_mapping', []);

Expand All @@ -29,7 +30,7 @@ final public static function execute(
$placedAt = now();
}

if ($charges->isEmpty()) {
if ($charges->isEmpty() && !$requiresCapture) {
return $order;
}

Expand Down
5 changes: 5 additions & 0 deletions payment/stripe/src/Managers/StripeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public function getCharges(string $paymentIntentId): Collection
return collect();
}

public function getCharge($chargeId)
{
return $this->getClient()->charges->retrieve($chargeId);
}

/**
* Build the intent
*
Expand Down
33 changes: 9 additions & 24 deletions payment/stripe/src/StripePaymentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ public function capture(Transaction $transaction, $amount = 0): PaymentCapture
$payload['amount_to_capture'] = $amount;
}

$charge = StripeFacade::getCharge($transaction->reference);

$paymentIntent = StripeFacade::fetchIntent($charge->payment_intent);

try {
$response = $this->stripe->paymentIntents->capture(
$transaction->reference,
$paymentIntent->id,
$payload
);
} catch (InvalidRequestException $e) {
Expand All @@ -133,28 +137,7 @@ public function capture(Transaction $transaction, $amount = 0): PaymentCapture
);
}

$charges = $response->charges->data;

$transactions = [];

foreach ($charges as $charge) {
$card = $charge->payment_method_details->card;
$transactions[] = [
'parent_transaction_id' => $transaction->id,
'success' => $charge->status != 'failed',
'type' => 'capture',
'driver' => 'stripe',
'amount' => $charge->amount_captured,
'reference' => $response->id,
'status' => $charge->status,
'notes' => $charge->failure_message,
'card_type' => $card->brand,
'last_four' => $card->last4,
'captured_at' => $charge->amount_captured ? now() : null,
];
}

$transaction->order->transactions()->createMany($transactions);
UpdateOrderFromIntent::execute($transaction->order, $paymentIntent);

return new PaymentCapture(success: true);
}
Expand All @@ -166,9 +149,11 @@ public function capture(Transaction $transaction, $amount = 0): PaymentCapture
*/
public function refund(Transaction $transaction, int $amount = 0, $notes = null): PaymentRefund
{
$charge = StripeFacade::getCharge($transaction->reference);

try {
$refund = $this->stripe->refunds->create(
['payment_intent' => $transaction->reference, 'amount' => $amount]
['payment_intent' => $charge->payment_intent, 'amount' => $amount]
);
} catch (InvalidRequestException $e) {
return new PaymentRefund(
Expand Down

0 comments on commit 6223eb2

Please sign in to comment.