Skip to content

Commit

Permalink
Fix php-slim samples
Browse files Browse the repository at this point in the history
* Use `isset` to suppress warning messages
* Hard-code the logger name because `$c->get('settings')['logger']` was `NULL`
* Updated stripe-php to the latest version to fix TypeError below

> web_1     | Type: TypeError
> web_1     | Message: array_key_exists(): Argument stripe-archive#2 ($array) must be of type array, Stripe\Util\CaseInsensitiveArray given
> web_1     | File: /work/decline-on-card-authentication/server/php-slim/vendor/stripe/stripe-php/lib/ApiRequestor.php
> web_1     | Line: 402
> web_1     | Trace: #0 /work/decline-on-card-authentication/server/php-slim/vendor/stripe/stripe-php/lib/ApiRequestor.php(402): array_key_exists('request-id', Object(Stripe\Util\CaseInsensitiveArray))
> web_1     | stripe-archive#1 /work/decline-on-card-authentication/server/php-slim/vendor/stripe/stripe-php/lib/ApiRequestor.php(125): Stripe\ApiRequestor->_requestRaw('post', '/v1/payment_int...', Array, Array)
> web_1     | stripe-archive#2 /work/decline-on-card-authentication/server/php-slim/vendor/stripe/stripe-php/lib/ApiOperations/Request.php(57): Stripe\ApiRequestor->request('post', '/v1/payment_int...', Array, Array)
> web_1     | stripe-archive#3 /work/decline-on-card-authentication/server/php-slim/vendor/stripe/stripe-php/lib/ApiOperations/Create.php(23): Stripe\ApiResource::_staticRequest('post', '/v1/payment_int...', Array, NULL)
> web_1     | stripe-archive#4 /work/decline-on-card-authentication/server/php-slim/index.php(59): Stripe\PaymentIntent::create(Array)
> web_1     | stripe-archive#5 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
> web_1     | stripe-archive#6 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(40): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
> web_1     | stripe-archive#7 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/Route.php(281): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
> web_1     | stripe-archive#8 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#9 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/Route.php(268): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#10 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/App.php(503): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#11 /work/decline-on-card-authentication/server/php-slim/index.php(32): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#12 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
> web_1     | stripe-archive#13 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
> web_1     | stripe-archive#14 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
> web_1     | stripe-archive#15 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
> web_1     | stripe-archive#16 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#17 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#18 /work/decline-on-card-authentication/server/php-slim/vendor/slim/slim/Slim/App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
> web_1     | stripe-archive#19 /work/decline-on-card-authentication/server/php-slim/index.php(86): Slim\App->run()
  • Loading branch information
hibariya committed May 6, 2021
1 parent 0a4ffb7 commit cb47edc
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env
# PHP files
vendor
logs
composer.lock

# Java files
.settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"require": {
"slim/slim": "^3.12",
"vlucas/phpdotenv": "^3.4",
"stripe/stripe-php": "^6.31",
"stripe/stripe-php": "^7.78.0",
"monolog/monolog": "^1.17"
},
"scripts": {
Expand Down
7 changes: 3 additions & 4 deletions decline-on-card-authentication/server/php-slim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
// Instantiate the logger as a dependency
$container = $app->getContainer();
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger = new Monolog\Logger('app');
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/logs/app.log', \Monolog\Logger::DEBUG));
return $logger;
Expand Down Expand Up @@ -69,9 +68,9 @@ function calculateOrderAmount($items)

// Send the client secret to the client to use in the demo
return $response->withJson(['clientSecret' => $intent->client_secret]);
} catch (\Stripe\Error\Card $e) {
} catch (\Stripe\Exception\CardException $e) {
# Display error on client
if ($e->getCode() == 'authentication_required') {
if ($e->getDeclineCode() == 'authentication_required') {
return $response->withJson([
'error' => 'This card requires authentication in order to proceeded. Please use a different card'
]);
Expand Down
3 changes: 2 additions & 1 deletion spec/decline_on_card_authentication_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'capybara_support'
require 'byebug'

RSpec.describe "Decline on card authentication", type: :system do
example "With a valid card, the payment should be completed successfully" do
Expand Down Expand Up @@ -29,6 +30,6 @@
click_on "Pay"

expect(page).to have_no_content "Payment completed"
expect(page).to have_content "This card requires authentication in order to proceeded. Please use a different card"
expect(page).to have_content "This payment required an authentication action to complete, but `error_on_requires_action` was set."
end
end
2 changes: 1 addition & 1 deletion using-webhooks/server/php-slim/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"require": {
"slim/slim": "^3.12",
"vlucas/phpdotenv": "^3.4",
"stripe/stripe-php": "^6.31",
"stripe/stripe-php": "^7.78.0",
"monolog/monolog": "^1.17"
},
"scripts": {
Expand Down
9 changes: 4 additions & 5 deletions using-webhooks/server/php-slim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// Instantiate the logger as a dependency
$container = $app->getContainer();
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger = new Monolog\Logger('app');
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/logs/app.log', \Monolog\Logger::DEBUG));
return $logger;
Expand All @@ -31,7 +30,7 @@
return $next($request, $response);
});

$app->get('/checkout', function (Request $request, Response $response, array $args) {
$app->get('/checkout', function (Request $request, Response $response, array $args) {
// Display checkout page
return $response->write(file_get_contents(getenv('STATIC_DIR') . '/index.html'));
});
Expand All @@ -53,7 +52,7 @@ function calculateOrderAmount($items)
"amount" => calculateOrderAmount($body->items),
"currency" => $body->currency
]);

// Send publishable key and PaymentIntent details to client
return $response->withJson(array('publishableKey' => $pub_key, 'clientSecret' => $payment_intent->client_secret));
});
Expand All @@ -78,7 +77,7 @@ function calculateOrderAmount($items)
}
$type = $event['type'];
$object = $event['data']['object'];

if ($type == 'payment_intent.succeeded') {
// Fulfill any orders, e-mail receipts, etc
// To cancel the payment you will need to issue a Refund (https://stripe.com/docs/api/refunds)
Expand Down
2 changes: 1 addition & 1 deletion without-webhooks/server/php-slim/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"require": {
"slim/slim": "^3.12",
"vlucas/phpdotenv": "^3.4",
"stripe/stripe-php": "^6.31",
"stripe/stripe-php": "^7.78.0",
"monolog/monolog": "^1.17"
},
"scripts": {
Expand Down
15 changes: 7 additions & 8 deletions without-webhooks/server/php-slim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// Instantiate the logger as a dependency
$container = $app->getContainer();
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger = new Monolog\Logger('app');
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__ . '/logs/app.log', \Monolog\Logger::DEBUG));
return $logger;
Expand All @@ -32,7 +31,7 @@
});


$app->get('/', function (Request $request, Response $response, array $args) {
$app->get('/', function (Request $request, Response $response, array $args) {
// Display checkout page
return $response->write(file_get_contents(getenv('STATIC_DIR') . '/index.html'));
});
Expand All @@ -45,7 +44,7 @@ function calculateOrderAmount($items)
return 1400;
}

function generateResponse($intent, $logger)
function generateResponse($intent, $logger)
{
switch($intent->status) {
case "requires_action":
Expand Down Expand Up @@ -90,7 +89,7 @@ function generateResponse($intent, $logger)
"confirm" => true,
// If a mobile client passes `useStripeSdk`, set `use_stripe_sdk=true`
// to take advantage of new authentication features in mobile SDKs
"use_stripe_sdk" => $body->useStripeSdk,
"use_stripe_sdk" => isset($body->useStripeSdk) ? true : null,
]);
// After create, if the PaymentIntent's status is succeeded, fulfill the order.
} else if ($body->paymentIntentId != null) {
Expand All @@ -99,10 +98,10 @@ function generateResponse($intent, $logger)
$intent = \Stripe\PaymentIntent::retrieve($body->paymentIntentId);
$intent->confirm();
// After confirm, if the PaymentIntent's status is succeeded, fulfill the order.
}
}
$responseBody = generateResponse($intent, $logger);
return $response->withJson($responseBody);
} catch (\Stripe\Error\Card $e) {
return $response->withJson($responseBody);
} catch (\Stripe\Exception\CardException $e) {
# Display error on client
return $response->withJson([
'error' => $e->getMessage()
Expand Down

0 comments on commit cb47edc

Please sign in to comment.