Skip to content

Checking Charge Status

Luke Walsh edited this page Dec 18, 2022 · 1 revision

While this package stores the result of charging a shop for a plan, it does not actively check the charge's status; example: If the recurring charge was later cancelled, if the recurring charge could not complete for one month, etc.

It is not checked by the package because that would require custom jobs, custom checks, and custom logic which varies from app to app.

If you would like to check all your shops' charge statuses for plans, you can schedule a job to run every night or every week. Here's an example:

use Osiset\ShopifyApp\Services\ChargeHelper;
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
use App\User;

// ...

// Setup the "Charge Helper"
$chs = resolve(ChargeHelper::class);
$shops = User::all();

// Loop all shops
foreach ($shops as $shop) {
  $plan = $shop->plan;
  if ($plan === null) {
     // No plan, skip
     continue;
  }

  // Get the charge entry from database, set it to the charge helper to use for API calls
  $charge = $chs->chargeForPlan($plan->getId(), $shop);
  $chs->useCharge($charge->getReference());

  // Get the charge data from Shopify API
  $chargeData = $chs->retrieve($shop);

  // Now you can access `$chargeData['status']` to check its status and control your flow
}