Skip to content

Commit 321448b

Browse files
authored
[PHP] Enable stripe tests on PHP (#6421)
1 parent 5fc74e4 commit 321448b

9 files changed

Lines changed: 129 additions & 4 deletions

File tree

manifests/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ manifest:
358358
tests/appsec/test_automated_login_events.py::Test_V3_Login_Events_Anon::test_login_wrong_user_failure_basic: missing_feature (Basic auth not implemented)
359359
tests/appsec/test_automated_login_events.py::Test_V3_Login_Events_Blocking: v1.8.0
360360
tests/appsec/test_automated_login_events.py::Test_V3_Login_Events_RC: v1.8.0
361-
tests/appsec/test_automated_payment_events.py: missing_feature
361+
tests/appsec/test_automated_payment_events.py: v1.17.0-dev
362362
tests/appsec/test_automated_user_and_session_tracking.py::Test_Automated_Session_Blocking: missing_feature
363363
tests/appsec/test_automated_user_and_session_tracking.py::Test_Automated_User_Blocking: v1.8.0
364364
tests/appsec/test_automated_user_and_session_tracking.py::Test_Automated_User_Tracking: v1.8.0

utils/build/docker/php/common/composer.gte8.2.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"weblog/acme": "*",
66
"monolog/monolog": "*",
77
"openai-php/client": "*",
8-
"guzzlehttp/guzzle": "*"
8+
"guzzlehttp/guzzle": "*",
9+
"stripe/stripe-php": "^10.0"
910
},
1011
"repositories": [
1112
{

utils/build/docker/php/common/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"type": "project",
44
"require": {
55
"weblog/acme": "*",
6-
"monolog/monolog": "*"
6+
"monolog/monolog": "*",
7+
"stripe/stripe-php": "^10.0"
78
},
89
"repositories": [
910
{

utils/build/docker/php/common/install_ddtrace.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ set -eux
44

55
IS_APACHE=${1:-0}
66

7+
if [ -z "$PHP_VERSION" ]; then
8+
PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")
9+
fi
10+
11+
cd /var/www/html
12+
export COMPOSER=composer.json
13+
if [ "$(printf '%s\n' "$PHP_VERSION" "8.2" | sort -V | head -n1)" = "8.2" ]; then
14+
export COMPOSER=composer.gte8.2.json
15+
fi
16+
if [ -f "$COMPOSER" ] && grep -Fq 'stripe/stripe-php' "$COMPOSER"; then
17+
apt-get update -qq && apt-get install -y --no-install-recommends unzip
18+
if [[ "$(printf '%s\n' "$PHP_VERSION" "8.1" | sort -V | head -n1)" = "8.1" ]]; then
19+
# Also require open-telemetry/sdk so composer does not remove it from vendor/:
20+
# the Dockerfile ADD overwrites composer.json with the repo version (which has
21+
# stripe but not OTel), causing a bare `composer require stripe` to drop OTel
22+
# from the resolved tree and wipe it from vendor/.
23+
COMPOSER_DISCARD_CHANGES=true composer require stripe/stripe-php "^10.0" "open-telemetry/sdk:^1.0.0" --no-interaction --ignore-platform-req=ext-mbstring
24+
else
25+
COMPOSER_DISCARD_CHANGES=true composer require stripe/stripe-php "^10.0" --no-interaction --ignore-platform-req=ext-mbstring
26+
fi
27+
fi
28+
729
cd /binaries
830

931
ARCH=$(uname -m)

utils/build/docker/php/common/rewrite-rules.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ RewriteRule "^/exceptionreplay/(.*)$" "/debugger/exceptionreplay/$1" [QSA]
3939
RewriteRule "^/llm$" "/llm/"
4040
RewriteRule "^/stats-unique$" "/stats-unique/"
4141
RewriteRule "^/await-agent-info$" "/await-agent-info/"
42+
RewriteRule "^/stripe/webhook$" "/stripe_webhook.php" [QSA,L]
43+
RewriteRule "^/stripe/create_checkout_session$" "/stripe_create_checkout_session.php" [QSA,L]
44+
RewriteRule "^/stripe/create_payment_intent$" "/stripe_create_payment_intent.php" [QSA,L]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require_once '/var/www/html/vendor/autoload.php';
4+
5+
header('Content-Type: application/json');
6+
7+
try {
8+
// Configure Stripe client
9+
\Stripe\Stripe::setApiKey('sk_FAKE');
10+
\Stripe\Stripe::$apiBase = 'http://internal_server:8089';
11+
12+
// Get JSON request body
13+
$input = file_get_contents('php://input');
14+
$data = json_decode($input, true);
15+
16+
if (json_last_error() !== JSON_ERROR_NONE) {
17+
throw new Exception('Invalid JSON: ' . json_last_error_msg());
18+
}
19+
20+
// Create checkout session with the request body data
21+
$result = \Stripe\Checkout\Session::create($data);
22+
23+
// Return the result as JSON
24+
echo json_encode($result);
25+
} catch (\Stripe\Exception\ApiErrorException $e) {
26+
http_response_code(500);
27+
echo json_encode(['error' => $e->getMessage()]);
28+
} catch (Exception $e) {
29+
http_response_code(500);
30+
echo json_encode(['error' => $e->getMessage()]);
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require_once '/var/www/html/vendor/autoload.php';
4+
5+
header('Content-Type: application/json');
6+
7+
try {
8+
// Configure Stripe client
9+
\Stripe\Stripe::setApiKey('sk_FAKE');
10+
\Stripe\Stripe::$apiBase = 'http://internal_server:8089';
11+
12+
// Get JSON request body
13+
$input = file_get_contents('php://input');
14+
$data = json_decode($input, true);
15+
16+
if (json_last_error() !== JSON_ERROR_NONE) {
17+
throw new Exception('Invalid JSON: ' . json_last_error_msg());
18+
}
19+
20+
// Create payment intent with the request body data
21+
$result = \Stripe\PaymentIntent::create($data);
22+
23+
// Return the result as JSON
24+
echo json_encode($result);
25+
} catch (\Stripe\Exception\ApiErrorException $e) {
26+
http_response_code(500);
27+
echo json_encode(['error' => $e->getMessage()]);
28+
} catch (Exception $e) {
29+
http_response_code(500);
30+
echo json_encode(['error' => $e->getMessage()]);
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once '/var/www/html/vendor/autoload.php';
4+
5+
header('Content-Type: application/json');
6+
7+
try {
8+
// Configure Stripe client
9+
\Stripe\Stripe::setApiKey('sk_FAKE');
10+
\Stripe\Stripe::$apiBase = 'http://internal_server:8089';
11+
12+
// Get raw request body
13+
$payload = file_get_contents('php://input');
14+
15+
// Get Stripe signature from header
16+
$sigHeader = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '';
17+
18+
// Webhook secret
19+
$webhookSecret = 'whsec_FAKE';
20+
21+
// Construct and verify the event
22+
$event = \Stripe\Webhook::constructEvent(
23+
$payload,
24+
$sigHeader,
25+
$webhookSecret
26+
);
27+
28+
// Return the event.data.object as JSON
29+
echo json_encode($event->data->object);
30+
} catch (\Stripe\Exception\SignatureVerificationException $e) {
31+
http_response_code(403);
32+
echo json_encode(['error' => $e->getMessage()]);
33+
} catch (Exception $e) {
34+
http_response_code(403);
35+
echo json_encode(['error' => $e->getMessage()]);
36+
}

utils/build/docker/php/php-fpm/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ nala update
3131
printf '#!/bin/sh\n\nexit 101\n' > /usr/sbin/policy-rc.d
3232
chmod +x /usr/sbin/policy-rc.d
3333

34-
nala install -y --no-install-recommends tzdata publicsuffix curl apache2 libapache2-mod-fcgid jq ca-certificates git php$PHP_VERSION-fpm php$PHP_VERSION-curl apache2 php$PHP_VERSION-mysql php$PHP_VERSION-pgsql php$PHP_VERSION-xml php$PHP_VERSION-mongodb
34+
nala install -y --no-install-recommends tzdata publicsuffix curl apache2 libapache2-mod-fcgid jq ca-certificates git unzip php$PHP_VERSION-fpm php$PHP_VERSION-curl apache2 php$PHP_VERSION-mysql php$PHP_VERSION-pgsql php$PHP_VERSION-xml php$PHP_VERSION-mongodb
3535

3636
rm -rf /usr/sbin/policy-rc.d
3737
rm -rf /var/lib/apt/lists/*

0 commit comments

Comments
 (0)