<?php
namespace App\Entity;
use DateTime;
use DateTimeZone;
use Doctrine\ORM\Mapping as ORM;
/**
* UserSubscriptions
*
* @ORM\Table(name="user_subscriptions")
* @ORM\Entity(repositoryClass="App\Repository\UserSubscriptionsRepository")
*/
class UserSubscriptions
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="user_id", type="integer")
*/
private $userId;
/**
* @var \DateTime
*
* @ORM\Column(name="start_subscription", type="datetime", nullable=true)
*/
private $startSubscription;
/**
* @var \DateTime
*
* @ORM\Column(name="end_subscription", type="datetime", nullable=true)
*/
private $endSubscription;
/**
* @var \DateTime
*
* @ORM\Column(name="removed_at", type="datetime", nullable=true)
*/
private $removedAt;
/**
* @var string
* Platform subscription was performed on;
*
* @ORM\Column(name="platform", type="string", nullable=false)
*/
private $platform;
// Apple subscription transaction fields
/**
* @var string
*
* @ORM\Column(name="transaction_id", type="string", nullable=false, unique=true)
*/
private $transactionId;
/**
* @var string
* @ORM\Column(name="original_transaction_id", type="string", nullable=true)
*/
private $originalTransactionId;
/**
* @var string
* @ORM\Column(name="web_order_line_item_id", type="string", nullable=true)
*/
private $webOrderLineItemId;
/**
* @var string
* @ORM\Column(name="bundle_id", type="string", nullable=true)
*/
private $bundleId;
/**
* @var string
* @ORM\Column(name="product_id", type="string", nullable=true)
*/
private $productId;
/**
* @var string
* @ORM\Column(name="subscription_group_identifier", type="string", nullable=true)
*/
private $subscriptionGroupIdentifier;
/**
* @var \DateTime
* @ORM\Column(name="purchase_date", type="datetime", nullable=true)
*/
private $purchaseDate;
/**
* @var \DateTime
* @ORM\Column(name="original_purchase_date", type="datetime", nullable=true)
*/
private $originalPurchaseDate;
/**
* @var \DateTime
* @ORM\Column(name="expires_date", type="datetime", nullable=true)
*/
private $expiresDate;
/**
* @var int
* @ORM\Column(name="quantity", type="integer", nullable=true)
*/
private $quantity;
/**
* @var string
* @ORM\Column(name="type", type="string", nullable=true)
*/
private $type;
/**
* @var string
* @ORM\Column(name="in_app_ownership_type", type="string", nullable=true)
*/
private $inAppOwnershipType;
/**
* @var \DateTime
* @ORM\Column(name="signed_date", type="datetime", nullable=true)
*/
private $signedDate;
/**
* @var int
* @ORM\Column(name="offer_type", type="integer", nullable=true)
*/
private $offerType;
/**
* @var string
* @ORM\Column(name="environment", type="string", nullable=true)
*/
private $environment;
/**
* @var string
* @ORM\Column(name="transaction_reason", type="string", nullable=true)
*/
private $transactionReason;
/**
* @var string
* @ORM\Column(name="storefront", type="string", nullable=true)
*/
private $storefront;
/**
* @var int
* @ORM\Column(name="store_front_id", type="integer", nullable=true)
*/
private $storefrontId;
/**
* @var float
* @ORM\Column(name="price", type="float", nullable=true)
*/
private $price;
/**
* @var string
* @ORM\Column(name="currency", type="string", nullable=true)
*/
private $currency;
/**
* @var string
* @ORM\Column(name="offer_discount_type", type="string", nullable=true)
*/
private $offerDiscountType;
// Google subscription transaction fields
/**
* @var string
* @ORM\Column(name="subscription_state", type="string", nullable=true)
*/
private $subscriptionState;
/**
* @var string
* @ORM\Column(name="acknowledgement_state", type="string", nullable=true)
*/
private $acknowledgementState;
/**
* @var string
* @ORM\Column(name="latest_order_id", type="string", nullable=true)
*/
private $latestOrderId;
/**
* @param string transactionId
* @param int userId
* @param string platform {apple|google}
*
* @return UserSubscriptions
*/
public function __construct($transactionId, $userId, $platform)
{
$this->transactionId = $transactionId;
$this->startSubscription = new DateTime();
$this->platform = $platform;
$this->userId = $userId;
}
/**
* @param transaction dictionary
*/
public function updateAppleSubscription($transaction)
{
$this->originalTransactionId = $transaction->originalTransactionId;
$this->webOrderLineItemId = $transaction->webOrderLineItemId;
$this->bundleId = $transaction->bundleId;
$this->productId = $transaction->productId;
$this->subscriptionGroupIdentifier =
$transaction->subscriptionGroupIdentifier;
$this->purchaseDate = (new DateTime(
'@'.(string)($transaction->purchaseDate/1000)
));
$this->originalPurchaseDate = (new DateTime(
'@'.(string)($transaction->originalPurchaseDate/1000)
));
$this->expiresDate = (new DateTime(
'@'.(string)($transaction->expiresDate/1000)
));
$this->quantity = $transaction->quantity;
$this->type = $transaction->type;
$this->inAppOwnershipType = $transaction->inAppOwnershipType;
$this->signedDate = (new DateTime(
'@'.(string)($transaction->signedDate/1000)
));
// $this->offerType = $transaction->offerType;
$this->environment = $transaction->environment;
$this->transactionReason = $transaction->transactionReason;
$this->storefront = $transaction->storefront;
$this->storefrontId = $transaction->storefrontId;
$this->price = $transaction->price;
$this->currency = $transaction->currency;
// $this->offerDiscountType = $transaction->offerDiscountType;
$this->startSubscription = $this->purchaseDate;
$this->endSubscription = $this->expiresDate;
}
/**
* @param transaction dictionary
*/
public function updateGoogleSubscription($transaction)
{
$this->originalTransactionId = $transaction->linkedPurchaseToken;
$this->subscriptionState = $transaction->subscriptionState;
$this->acknowledgementState = $transaction->acknowledgementState;
$this->latestOrderId = $transaction->latestOrderId;
$this->productId = $transaction->lineItems[0]->productId;
$this->bundleId = $transaction->bundleId;
$this->purchaseDate = new DateTime(
$transaction->startTime, new DateTimeZone('UTC')
);
$this->expiresDate = new DateTime(
$transaction->lineItems[0]->expiryTime, new DateTimeZone('UTC')
);
$this->quantity = 1;
$this->storefront = $transaction->regionCode;
if (isset($transaction->lineItems[0]->autoRenewingPlan) &&
$transaction->lineItems[0]->autoRenewingPlan->autoRenewEnabled === true
)
$this->type = "Auto-Renewable Subscription";
$this->startSubscription = $this->purchaseDate;
$this->endSubscription = $this->expiresDate;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
public function hasExpired()
{
return $this->expiresDate <
new DateTime('now', new DateTimeZone('UTC'));
}
/**
* Set userId
*
* @param integer $userId
*
* @return UserSubscriptions
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return int
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set platform
* @return UserSubscriptions
*/
public function setPlatform($platform)
{
$this->platform = $platform;
return $this;
}
/**
* Get platform
* @return string
*/
public function getPlatform()
{
return $this->platform;
}
/**
* Set startSubscription
*
* @param \DateTime $startSubscription
*
* @return UserSubscriptions
*/
public function setStartSubscription($startSubscription)
{
$this->startSubscription = $startSubscription;
return $this;
}
/**
* Get startSubscription
*
* @return \DateTime
*/
public function getStartSubscription()
{
return $this->startSubscription;
}
/**
* Set endSubscription
*
* @param \DateTime $endSubscription
*
* @return UserSubscriptions
*/
public function setEndSubscription($endSubscription)
{
$this->endSubscription = $endSubscription;
return $this;
}
/**
* Get endSubscription
*
* @return \DateTime
*/
public function getEndSubscription()
{
return $this->endSubscription;
}
/**
* Set removedAt
*
* @param \DateTime $removedAt
*
* @return UserSubscriptions
*/
public function setRemovedAt($removedAt)
{
$this->removedAt = $removedAt;
return $this;
}
/**
* Get removedAt
*
* @return \DateTime
*/
public function getRemovedAt()
{
return $this->removedAt;
}
/**
* Get the value of transactionId
*/
public function getTransactionId()
{
return $this->transactionId;
}
/**
* Set the value of transactionId
*/
public function setTransactionId($transactionId): self
{
$this->transactionId = $transactionId;
return $this;
}
/**
* Get the value of originalTransactionId
*/
public function getOriginalTransactionId()
{
return $this->originalTransactionId;
}
/**
* Set the value of originalTransactionId
*/
public function setOriginalTransactionId($originalTransactionId): self
{
$this->originalTransactionId = $originalTransactionId;
return $this;
}
}