ethercreative / web-payments

Web Payments for Craft Commerce
Other
5 stars 7 forks source link

500 error: Cannot assign null to property craft\\elements\\Address::$countryCode of type string #32

Open robzor opened 1 year ago

robzor commented 1 year ago

Hi there, I'm getting a 500 error when trying to use the plugin, the pay popup will show but when I click 'Pay' I get the error.

I'm running Craft Pro 4.4.12 and Commerce 4.2.8, and this is on OSX Desktop in Chrome:

Template code:

            {{ craft.webPayments.button({
                cart: craft.commerce.carts.cart,
                onComplete: {
                    redirect: '/checkout/order={number}&success=true',
                },
            }) }}

500 error in console:

{
"name":"Exception",
"message":"Cannot assign null to property craft\\elements\\Address::$countryCode of type string",
"code":0,
"error":"Cannot assign null to property craft\\elements\\Address::$countryCode of type string",
"exception":"TypeError",
"file":"/website/vendor/ether/web-payments/src/services/StripeService.php",
"line":297,

"trace":[
{"file":"/website/vendor/ether/web-payments/src/controllers/StripeController.php",
"line":213,
"function":"setBillingAddress",
"class":"ether\\webpayments\\services\\StripeService",
"type":"->"},{
"function":"actionPay",
"class":"ether\\webpayments\\controllers\\StripeController",
"type":"->"}

If you need any more information please let me know! Thanks!

alexjcollins commented 1 year ago

Thanks for sharing, let me see if I can recreate the problem, first. I'll come back to you if we need anything else.

robzor commented 1 year ago

Hi @alexjcollins sorry to nag, did you have any luck with this?

alexjcollins commented 1 year ago

I haven't been able to replicate this on our local environment. I suspect that it's to do with your Countries & States settings (/commerce/store-settings/countries). The error suggests the country code for the billing address is being sent as null.

Before you hit the pay button, what does the cart look like in the Craft admin? Can you share a screenshot?

robzor commented 1 year ago

Hi @alexjcollins , the site only ships to the UK and we actually set this in our address fields in the checkout as well with:

{{ hiddenInput('countryCode', "GB") }}

Here's a screenshot of the cart:

CleanShot 2023-06-06 at 11 47 24

We also have this in our Store Settings:

CleanShot 2023-06-06 at 11 49 57

ArtDepartmentRF commented 1 year ago

I am experiencing the same issue while trying to integrate this plugin for the first time.

I have arrived at an understanding of what is causing the (my) issue. After the payment is submitted to Stripe a request is submitted to the endpoint /actions/web-payments/stripe/pay containing a token from Stripe in the payload. Below is an example from my test environment.

image

This includes an object "billing_details" which is utilised to set the order billing address if it's available initially:

https://github.com/ethercreative/web-payments/blob/b7a8fbf44b84e2e740c6b137fc7219b280b9d3db/src/controllers/StripeController.php#L213

https://github.com/ethercreative/web-payments/blob/b7a8fbf44b84e2e740c6b137fc7219b280b9d3db/src/services/StripeService.php#L279C2-L315C3

As you can see from the screenshot the token from Stripe contains null for the country where the country code could be. Hence the error Cannot assign null to property craft\\elements\\Address::$countryCode of type string.

https://github.com/ethercreative/web-payments/blob/b7a8fbf44b84e2e740c6b137fc7219b280b9d3db/src/services/StripeService.php#L284-L285

Perhaps the above line needs to be amended or added to with an additional validation check, because whilst all the properties of the billing_details object are null this will not cause empty() to return true.

Liamm3 commented 9 months ago

Is there any update on this? We are experiencing the same issue only when Apple Pay gets used.

cole007 commented 8 months ago

Just to chip in that this seems to be the cause of an issue we are experiencing on completing web payments. Has anyone got this working with Craft/Craft Commerce 4?

Cole

Liamm3 commented 8 months ago

Just to chip in that this seems to be the cause of an issue we are experiencing on completing web payments. Has anyone got this working with Craft/Craft Commerce 4?

Cole

We weren't able to fix the issue using this plugin without editing the source code. Our solution is to use the commerce-stripe gateway directly and implement the express checkout element from Stripe (https://docs.stripe.com/elements/express-checkout-element).

Unfortunately, commerce-stripe has no option for using the express checkout element, so it needs to be implemented on your own. But I think using the express checkout element is the future way of doing this. I also made a thread on the commerce-stripe github page: https://github.com/craftcms/commerce-stripe/discussions/286