Skip to content

Commit

Permalink
Annual Subscription Plan
Browse files Browse the repository at this point in the history
 * Moved subscription to GraphQL
 * Added Annual Plan
  • Loading branch information
dvmunjapara committed Dec 30, 2020
1 parent a132430 commit 1269c0a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/ShopifyApp/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Plan extends Model
const PLAN_RECURRING = 1;
const PLAN_ONETIME = 2;

const INTERVAL_MONTHLY = 1;
const INTERVAL_YEARLY = 2;

/**
* The attributes that should be casted to native types.
*
Expand Down Expand Up @@ -71,6 +74,27 @@ public function typeAsString($plural = false)
return $plural ? "{$type}s" : $type;
}

/**
* Returns the plan interval as a string (for API).
*
* @return string
*/
public function intervalAsString()
{
$type = null;
switch ($this->interval) {
case self::INTERVAL_YEARLY:
$type = 'ANNUAL';
break;
default:
case self::INTERVAL_MONTHLY:
$type = 'EVERY_30_DAYS';
break;
}

return $type;
}

/**
* Checks if this plan has a trial.
*
Expand Down
73 changes: 67 additions & 6 deletions src/ShopifyApp/Services/BillingPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,74 @@ public function getCharge()
public function confirmationUrl()
{
// Begin the charge request
$charge = $this->api->rest(
'POST',
"/admin/{$this->plan->typeAsString(true)}.json",
["{$this->plan->typeAsString()}" => $this->chargeParams()]
)->body->{$this->plan->typeAsString()};
$query = '
mutation appSubscriptionCreate(
$name: String!,
$returnUrl: URL!,
$trialDays: Int,
$test: Boolean,
$lineItems: [AppSubscriptionLineItemInput!]!
) {
appSubscriptionCreate(
name: $name,
returnUrl: $returnUrl,
trialDays: $trialDays,
test: $test,
lineItems: $lineItems
) {
appSubscription {
id
}
confirmationUrl
userErrors {
field
message
}
}
}';

if ($this->plan->typeAsString() == 'application_charge') {

$plan= [
'appUsagePricingDetails' => [
'cappedAmount' => $this->plan->capped_amount,
'terms' => $this->plan->terms
],
];
} else {

$plan = [
'appRecurringPricingDetails' => [
'price' => [
'amount' => $this->plan->price,
'currencyCode' => 'USD',
],
'interval' => $this->plan->intervalAsString(),
],
];
}

return $charge->confirmation_url;
$variables = [
'name' => $this->plan->name,
'returnUrl' => URL::secure(
Config::get('shopify-app.billing_redirect'),
['plan_id' => $this->plan->id]
),
'trialDays' => $this->determineTrialDays(),
'test' => $this->plan->isTest(),
'lineItems' => [
[
'plan' => $plan
],
],
];

$response = ShopifyApp::doRequestGraphQL($query,$variables);

if ($response['errors'] == false) {

return $response['body']['appSubscriptionCreate']['confirmationUrl'];
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/ShopifyApp/ShopifyApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ public function api()
return $api;
}


/**
* Do GraphQL call
*
* @param string $query
* @param array|null $payload
* @return mixed
*/
public function doRequestGraphQL(string $query, array $payload = null)
{
$response = json_decode(json_encode($this->shop->api()->graph($query, $payload)),true);
if ($response['errors'] !== false) {
$message = is_array($response['errors'])
? $response['errors'][0]['message'] : $response['errors'];

// Request error somewhere, throw the exception
throw new Exception($message);
}

return $response;
}

/**
* Ensures shop domain meets the specs.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddIntervalFieldToPlans extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('plans', function (Blueprint $table) {
$table->tinyInteger('interval')->default(1);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('plans', function (Blueprint $table) {
$table->dropColumn('interval');
});
}
}

0 comments on commit 1269c0a

Please sign in to comment.