isInitialPayment shows false even though it should be true
pmochine opened this issue · comments
- Cashier Paddle Version: v1.5.2
- Laravel Version: v9.5.1
- PHP Version: 8.0.14
- Database Driver & Version:
Description:
I'm trying to work with the event SubscriptionPaymentSucceeded
but while using the public method isInitialPayment()
, I noticed that it's not working correctly.
To fix the "bug" it should be
public function isInitialPayment()
{
(int) return $this->payload['initial_payment'] === 1;
}
Hmm that's odd since it should definitely be an integer: https://developer.paddle.com/webhook-reference/ZG9jOjI1MzUzOTk3-subscription-payment-succeeded
I sent in a PR for this: #159
@driesvints thanks for your fast response and PR. I made a small logger test for you
logger($event->payload['initial_payment'] === 1 ? 'true' : 'false');
logger((int) $event->payload['initial_payment'] === 1 ? 'true' : 'false');
And if I look into my logs, I see:
[2022-03-28 12:10:53] production.DEBUG: false
[2022-03-28 12:10:53] production.DEBUG: true
@pmochine so it works for you? Btw, you're not placing the array call and type cast between ( )
.
Can you dump the contents of $event->payload
?
@driesvints No, it doesn't work. I just wanted to show you that I need to type cast $event->payload['initial_payment']
to integer so only then does it show "true".
And here is the payload:
"payload":{
"alert_id":"...",
"alert_name":"subscription_payment_succeeded",
"balance_currency":"EUR",
"balance_earnings":"292.46",
"balance_fee":"17",
"balance_gross":"331.12",
"balance_tax":"21.66",
"checkout_id":"...",
"country":"TH",
"coupon":null,
"currency":"THB",
"customer_name":"...",
"earnings":"11121.89",
"email":"...",
"event_time":"2022-03-28 10:47:56",
"fee":"646.49",
"initial_payment":"1",
"instalments":"1",
"marketing_consent":"0",
"next_bill_date":"2023-03-28",
"next_payment_amount":"12592.17",
"order_id":"...",
"passthrough":"....",
"payment_method":"card",
"payment_tax":"823.79",
"plan_name":"Yearly",
"quantity":"1",
"receipt_url":"...",
"sale_gross":"12592.17",
"status":"active",
"subscription_id":"...",
"subscription_payment_id":"...",
"subscription_plan_id":"...",
"unit_price":"12592.17",
"user_id":"...",
"p_signature":"..."
}
And as you notice, every property from payload
comes as a string. Not sure if this should be like that. If I look at the dump contents of receipt
, I see integer values.
Besides, I'm not sure what you mean with you're not placing the array call and type cast between ( )
I just wanted to show you that I need to type cast $event->payload['initial_payment'] to integer so only then does it show "true".
That's exactly what I'm doing in my PR. The snippet you posted:
logger((int) $event->payload['initial_payment'] === 1 ? 'true' : 'false');
Is wrong. You need to do ((int) $event->payload['initial_payment']) === 1
@driesvints I think it was just a misunderstanding since English is not my first language. I just wanted to show you an extra example of why your PR wasn't a faulty one since you wrote Apparently this isn't always the promised type from the docs.
. I wanted to show that it's not "apparently".
And personal interest, why should you use an extra ( )
?
For example:
echo "1" === 1 ? 'true' : 'false'; // false
echo (int) "1" === 1 ? 'true' : 'false'; // true
echo ((int) "1") === 1 ? 'true' : 'false'; // true
Cast operators have a very high precedence, for example (int)$a/$b is evaluated as ((int)$a)/$b, not as (int)($a/$b) [which would be like intdiv($a,$b) if both $a and $b are integers].
The only exceptions (as of PHP 8.0) are the exponentiation operator ** [i.e. (int)$a**$b is evaluated as (int)($a**$b) rather than ((int)$a)**$b] and the special access/invocation operators ->, ::, [] and () [i.e. in each of (int)$a->$b, (int)$a::$b, (int)$a[$b] and (int)$a($b), the cast is performed last on the result of the variable expression].
@pmochine right. But I still don't understand why my PR won't work for you?
@driesvints Ahh, now I get the misunderstanding! Of course the PR works!
When you wrote:
so it works for you?
I understood your question quite differently. I thought you were asking if it suddenly works without PR. Then I said no, it doesn't work without PR.
Nonetheless, thanks for the help <3
Hah glad we cleared that up 👍