gnikyt / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
1.24k stars 374 forks source link

Receiving an duplicate content error when displaying billing declined in an embedded app #94

Closed ncpope closed 6 years ago

ncpope commented 6 years ago

When displaying within an embedded app it will redirect which will cause the billing controller to try and create the charge more than once in the db. This causes an error like SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1234567890' for key 'charges_charge_id_unique'

https://github.com/ohmybrew/laravel-shopify/blob/4258693b3eb7430d426530faf1932cf70d1aa6cd/src/ShopifyApp/Traits/BillingControllerTrait.php#L51-L55

These lines could be changed to:

$charge = Charge::firstOrCreate([
    "type" => $this->chargeType() === 'recurring' ? Charge::CHARGE_RECURRING : Charge::CHARGE_ONETIME,
    "charge_id" => $chargeId,
    "status" => $status,
    "shop_id" => $shop->id,
]);

This will prevent the error and will select the charge rather than creating a new one.

https://github.com/ohmybrew/laravel-shopify/blob/4258693b3eb7430d426530faf1932cf70d1aa6cd/src/ShopifyApp/Traits/BillingControllerTrait.php#L83-L84

This could then become:

// Save the charge
$charge->save();
gnikyt commented 6 years ago

Thank you for the note, I have adjusted this for next release.

ncpope commented 6 years ago

Hi, I just left a comment on the commit but I'm not exactly sure whether or not it shows since I've never done one before, so I'll just post it here as well:

https://github.com/ohmybrew/laravel-shopify/commit/82b07788218fc5dbc8699dab92d02a566050c61d#r30494464

gnikyt commented 6 years ago

Present in v4.0.0