Skip to content

Commit dcea7a1

Browse files
committed
Amend pr comments
1 parent a061638 commit dcea7a1

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/DDTrace/Integrations/Stripe/StripeIntegration.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ public static function extractCheckoutSessionFields($session): array
7979
$discounts = self::getNestedValue($session, 'discounts');
8080
if (is_array($discounts) && count($discounts) > 0) {
8181
$discount = $discounts[0];
82-
$payload['discounts.coupon'] = self::getNestedValue($discount, 'coupon');
83-
$payload['discounts.promotion_code'] = self::getNestedValue($discount, 'promotion_code');
84-
} else {
85-
$payload['discounts.coupon'] = null;
86-
$payload['discounts.promotion_code'] = null;
82+
$coupon = self::getNestedValue($discount, 'coupon');
83+
$promotionCode = self::getNestedValue($discount, 'promotion_code');
84+
if ($coupon !== null) {
85+
$payload['discounts.coupon'] = $coupon;
86+
}
87+
if ($promotionCode !== null) {
88+
$payload['discounts.promotion_code'] = $promotionCode;
89+
}
8790
}
8891

8992
return $payload;
@@ -140,7 +143,7 @@ public static function init(): int
140143
{
141144
self::hookCheckoutSessionCreate();
142145
self::hookPaymentIntentCreate();
143-
self::hookEventConstructFrom();
146+
self::hookWebhookConstructEvent();
144147

145148
return Integration::LOADED;
146149
}
@@ -171,11 +174,11 @@ private static function hookPaymentIntentCreate()
171174
\DDTrace\hook_method('Stripe\PaymentIntent', 'create', null, $onCreate);
172175
}
173176

174-
private static function hookEventConstructFrom()
177+
private static function hookWebhookConstructEvent()
175178
{
176179
\DDTrace\hook_method(
177-
'Stripe\Event',
178-
'constructFrom',
180+
'Stripe\Webhook',
181+
'constructEvent',
179182
null,
180183
static function ($This, $scope, $args, $retval, $exception) {
181184
if ($exception === null && $retval !== null) {

tests/Integrations/Stripe/Latest/StripeTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ private function hasEventWithAnyKey(array $eventWrappers, array $keys): bool
7373
return false;
7474
}
7575

76+
private function constructWebhookEvent(array $payload, string $secret = 'whsec_test_secret'): \Stripe\Event
77+
{
78+
$payloadJson = json_encode($payload);
79+
$timestamp = time();
80+
$signedPayload = $timestamp . '.' . $payloadJson;
81+
$signature = hash_hmac('sha256', $signedPayload, $secret);
82+
$sigHeader = "t={$timestamp},v1={$signature}";
83+
return \Stripe\Webhook::constructEvent($payloadJson, $sigHeader, $secret);
84+
}
85+
7686
protected function ddTearDown()
7787
{
7888
parent::ddTearDown();
@@ -97,7 +107,7 @@ public function testPaymentSuccessWebhook()
97107
]
98108
];
99109

100-
\Stripe\Event::constructFrom($payload);
110+
$this->constructWebhookEvent($payload);
101111

102112
$allEvents = AppsecStatus::getInstance()->getEvents(['push_addresses'], []);
103113

@@ -143,7 +153,7 @@ public function testPaymentFailureWebhook()
143153
]
144154
];
145155

146-
\Stripe\Event::constructFrom($payload);
156+
$this->constructWebhookEvent($payload);
147157

148158
$allEvents = AppsecStatus::getInstance()->getEvents(['push_addresses'], []);
149159

@@ -186,7 +196,7 @@ public function testPaymentCancellationWebhook()
186196
]
187197
];
188198

189-
\Stripe\Event::constructFrom($payload);
199+
$this->constructWebhookEvent($payload);
190200

191201
$allEvents = AppsecStatus::getInstance()->getEvents(['push_addresses'], []);
192202

@@ -222,7 +232,7 @@ public function testWebhookInvalidOrUnsupportedEvents()
222232
]
223233
];
224234

225-
\Stripe\Event::constructFrom($payload);
235+
$this->constructWebhookEvent($payload);
226236

227237
$allEvents = AppsecStatus::getInstance()->getEvents(['push_addresses'], []);
228238

@@ -257,14 +267,7 @@ public function testPaymentSuccessWebhookViaConstructEvent()
257267
]
258268
]
259269
];
260-
$payloadJson = json_encode($payload);
261-
$secret = 'whsec_test_secret';
262-
$timestamp = time();
263-
$signedPayload = $timestamp . '.' . $payloadJson;
264-
$signature = hash_hmac('sha256', $signedPayload, $secret);
265-
$sigHeader = "t={$timestamp},v1={$signature}";
266-
267-
$event = \Stripe\Webhook::constructEvent($payloadJson, $sigHeader, $secret);
270+
$event = $this->constructWebhookEvent($payload);
268271

269272
$this->assertSame('payment_intent.succeeded', $event->type);
270273

@@ -583,7 +586,7 @@ public function __construct(string $responseBody, int $responseCode = 200)
583586
$this->responseCode = $responseCode;
584587
}
585588

586-
public function request($method, $absUrl, $headers, $params, $hasFile)
589+
public function request($method, $absUrl, $headers, $params, $hasFile, $apiMode = null, $maxNetworkRetries = null)
587590
{
588591
return [$this->responseBody, $this->responseCode, []];
589592
}

0 commit comments

Comments
 (0)