Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mattias-persson committed Sep 10, 2019
1 parent fd36f29 commit 55c38ab
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function charge(Request $request)
$response = ['token' => $order->token];

if (config('shopr.templates.order-confirmation')) {
$response['redirect'] = route('shopr.order-confirmation').'?token='.$order->token;
$response['redirect'] = route('shopr.order-confirmation', ['token' => $order->token]);
}

return response()->json($response, 201);
Expand Down
17 changes: 9 additions & 8 deletions src/Controllers/Web/PaymentConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@

class PaymentConfirmationController extends Controller
{
/**
* Attempts to confirm a payment. Returns an error view if unsuccessful and reidrects to
* the order confirmation view otherwise.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$request->validate(['gateway' => 'required']);

try {
$response = PaymentProviderManager::make($request)->confirm($request->payment_intent);
$response = PaymentProviderManager::make($request)->confirmPayment();
} catch (PaymentFailedException $e) {
return view('shopr::payments.error')->with('message', $e->getMessage());
}

info('Confirmation response', $response);

$order = Order::where('transaction_reference', $request->payment_intent)->firstOrFail();


$order->update([
'payment_status' => 'paid',
'transaction_reference' => $response['transaction_reference'],
]);

// Return redirectable response.
return redirect()->route('shopr.order-confirmation', [
'token' => $order->token,
]);
return redirect()->route('shopr.order-confirmation', ['token' => $order->token]);
}
}
2 changes: 1 addition & 1 deletion src/Middleware/RequireOrderToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RequireOrderToken
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->hasValidOrderToken($request)) {
if (!$this->hasValidOrderToken($request)) {
return redirect('/');
}

Expand Down
29 changes: 29 additions & 0 deletions src/PaymentProviders/PaymentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function __construct()
*/
abstract public function purchase();

/**
* The data used for confirming a payment, used for example when confirming a payment using SCA.
* The payment reference should be found in the $this->input-array.
*
* @return array
*/
abstract public function getPaymentConfirmationData() : array;

/**
* Makes the purchase and returns the results if successful. Throws exception if unsuccessful.
*
Expand Down Expand Up @@ -61,6 +69,27 @@ public function payForCart()
];
}

/**
* Confirms a payment if needed.
*
* @return array
*/
public function confirmPayment()
{
$response = $this->gateway->confirm($this->getPaymentConfirmationData())->send();

if (! $response->isSuccessful()) {
throw new PaymentFailedException($response->getMessage());
}

return [
'success' => true,
'transaction_reference' => $response->getTransactionReference(),
'transaction_id' => $response->getTransactionId(),
'payment_status' => 'paid',
];
}

/**
* Initializes and authorizes the gateway with the credentials.
*
Expand Down
22 changes: 5 additions & 17 deletions src/PaymentProviders/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Happypixels\Shopr\PaymentProviders;

use Omnipay\Omnipay;
use Happypixels\Shopr\Exceptions\PaymentFailedException;

class Stripe extends PaymentProvider
{
Expand All @@ -24,27 +23,16 @@ public function purchase()
}

/**
* Confirms a payment if needed.
* The data used for confirming a payment, used for example when confirming a payment using SCA.
* The payment reference should be found in the $this->input-array.
*
* @param string $reference
* @return array
*/
public function confirm($reference)
public function getPaymentConfirmationData() : array
{
$response = $this->gateway->confirm([
'paymentIntentReference' => $reference,
'returnUrl' => route('shopr.order-confirmation').'?gateway=Stripe',
])->send();

if (! $response->isSuccessful()) {
throw new PaymentFailedException($response->getMessage());
}

return [
'success' => true,
'transaction_reference' => $response->getTransactionReference(),
'transaction_id' => $response->getTransactionId(),
'payment_status' => 'paid',
'paymentIntentReference' => $this->input['payment_intent'],
'returnUrl' => route('shopr.order-confirmation', ['gateway' => 'Stripe']),
];
}

Expand Down

0 comments on commit 55c38ab

Please sign in to comment.