Skip to content

Commit

Permalink
fix: improve getPurchases API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkhansrc committed Mar 7, 2022
1 parent 7ef8958 commit 13b1b13
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
14 changes: 7 additions & 7 deletions src/Factory/Api/PurchaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public static function buildPurchase(array $purchaseData): Purchase
$purchaseData['userId'],
$purchaseData['userIds'] ?? null,
$purchaseData['receipt'],
$purchaseData['androidToken'],
$purchaseData['androidToken'] ?? null,
$purchaseData['product'],
$purchaseData['productSku'],
$purchaseData['productType'],
$purchaseData['productGroupName'] ?? null,
$purchaseData['listing'],
$purchaseData['store'],
$purchaseData['storeSegmentIndex'],
$purchaseData['listing'] ?? null,
$purchaseData['store'] ?? null,
$purchaseData['storeSegmentIndex'] ?? null,
$purchaseData['currency'],
$purchaseData['price'],
$purchaseData['convertedCurrency'],
Expand All @@ -55,7 +55,7 @@ public static function buildPurchase(array $purchaseData): Purchase
$purchaseData['isFamilyShare'],
$purchaseData['isPromo'],
$purchaseData['isRefunded'],
new DateTime($purchaseData['refundDate']) ?? null,
isset($purchaseData['refundDate']) ? new DateTime($purchaseData['refundDate']) : null,
$purchaseData['refundReason'] ?? null,
$purchaseData['refundAmount'] ?? null,
$purchaseData['convertedRefundAmount'] ?? null,
Expand All @@ -71,8 +71,8 @@ public static function buildPurchase(array $purchaseData): Purchase
$purchaseData['subscriptionProrationMode'] ?? null,
$purchaseData['subscriptionRenewalProduct'] ?? null,
$purchaseData['subscriptionRenewalProductSku'] ?? null,
new DateTime($purchaseData['expirationDate']) ?? null,
new DateTime($purchaseData['autoResumeDate']) ?? null,
isset($purchaseData['expirationDate']) ? new DateTime($purchaseData['expirationDate']) : null,
isset($purchaseData['autoResumeDate']) ? new DateTime($purchaseData['autoResumeDate']) : null,
$purchaseData['nextPurchase'] ?? null,
$purchaseData['linkedPurchase'] ?? null,
$purchaseData['originalPurchase'] ?? null
Expand Down
32 changes: 16 additions & 16 deletions src/Model/Api/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Purchase
/**
* Android purchase token.
*/
private string $androidToken;
private ?string $androidToken;
/**
* Product id.
*/
Expand All @@ -85,15 +85,15 @@ class Purchase
/**
* Listing id.
*/
private string $listing;
private ?string $listing;
/**
* Store id.
*/
private string $store;
private ?string $store;
/**
* Store segment index (when a store is used for testing, 0 by default).
*/
private int $storeSegmentIndex;
private ?int $storeSegmentIndex;
/**
* Price currency.
*/
Expand Down Expand Up @@ -292,14 +292,14 @@ public function __construct(string $id,
string $userId,
?array $userIds,
string $receipt,
string $androidToken,
?string $androidToken,
string $product,
string $productSku,
string $productType,
?string $productGroupName,
string $listing,
string $store,
int $storeSegmentIndex,
?string $listing,
?string $store,
?int $storeSegmentIndex,
string $currency,
float $price,
string $convertedCurrency,
Expand Down Expand Up @@ -600,7 +600,7 @@ public function setReceipt(string $receipt): Purchase
/**
* @return string
*/
public function getAndroidToken(): string
public function getAndroidToken(): ?string
{
return $this->androidToken;
}
Expand All @@ -609,7 +609,7 @@ public function getAndroidToken(): string
* @param string $androidToken
* @return Purchase
*/
public function setAndroidToken(string $androidToken): Purchase
public function setAndroidToken(?string $androidToken): Purchase
{
$this->androidToken = $androidToken;
return $this;
Expand Down Expand Up @@ -690,7 +690,7 @@ public function setProductGroupName(?string $productGroupName): Purchase
/**
* @return string
*/
public function getListing(): string
public function getListing(): ?string
{
return $this->listing;
}
Expand All @@ -699,7 +699,7 @@ public function getListing(): string
* @param string $listing
* @return Purchase
*/
public function setListing(string $listing): Purchase
public function setListing(?string $listing): Purchase
{
$this->listing = $listing;
return $this;
Expand All @@ -708,7 +708,7 @@ public function setListing(string $listing): Purchase
/**
* @return string
*/
public function getStore(): string
public function getStore(): ?string
{
return $this->store;
}
Expand All @@ -717,7 +717,7 @@ public function getStore(): string
* @param string $store
* @return Purchase
*/
public function setStore(string $store): Purchase
public function setStore(?string $store): Purchase
{
$this->store = $store;
return $this;
Expand All @@ -726,7 +726,7 @@ public function setStore(string $store): Purchase
/**
* @return int
*/
public function getStoreSegmentIndex(): int
public function getStoreSegmentIndex(): ?int
{
return $this->storeSegmentIndex;
}
Expand All @@ -735,7 +735,7 @@ public function getStoreSegmentIndex(): int
* @param int $storeSegmentIndex
* @return Purchase
*/
public function setStoreSegmentIndex(int $storeSegmentIndex): Purchase
public function setStoreSegmentIndex(?int $storeSegmentIndex): Purchase
{
$this->storeSegmentIndex = $storeSegmentIndex;
return $this;
Expand Down
47 changes: 46 additions & 1 deletion tests/Model/PurchaseAndSubWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Johnkhansrc\IaphubBundle\Tests\Model;

use Johnkhansrc\IaphubBundle\Factory\Api\PurchaseFactory;
use Johnkhansrc\IaphubBundle\Factory\Api\TransactionFactory;
use Johnkhansrc\IaphubBundle\Factory\WebhookFactory;
use Johnkhansrc\IaphubBundle\Model\Api\Purchase;
use Johnkhansrc\IaphubBundle\Model\Api\Transaction;
use Johnkhansrc\IaphubBundle\Model\Webhook\PurchaseWebhook;
use JsonException;
Expand Down Expand Up @@ -50,7 +52,7 @@ private function assertSuccessfullyInstantiatePurchaseAndSubWebhook(string $mock
self::assertInstanceOf(PurchaseWebhook::class, $webhook);
}

public function testThis(): void
public function testTransactionFactory(): void
{
$transactionData = [
"id" => "XXXXXX",
Expand All @@ -71,4 +73,47 @@ public function testThis(): void

self::assertInstanceOf(Transaction::class, TransactionFactory::build($transactionData));
}

public function testPurchaseFactory(): void
{
$datas = [
"id" => "xxx",
"purchaseDate" => "2022-03-07T11:56:43.000Z",
"quantity" => 1,
"platform" => "ios",
"country" => "XX",
"orderId" => "xxx",
"app" => "xxxx",
"user" => "xxxx",
"product" => "xxx",
"receipt" => "xxx",
"currency" => "EUR",
"price" => 29.99,
"convertedCurrency" => "EUR",
"convertedPrice" => 29.99,
"isSandbox" => false,
"isRefunded" => false,
"isFamilyShare" => false,
"isPromo" => false,
"isSubscription" => true,
"isSubscriptionActive" => true,
"isSubscriptionRenewable" => true,
"isSubscriptionRetryPeriod" => false,
"isSubscriptionGracePeriod" => false,
"isSubscriptionPaused" => false,
"isTrialConversion" => false,
"subscriptionState" => "active",
"subscriptionPeriodType" => "normal",
"expirationDate" => "2022-06-07T10:56:43.000Z",
"linkedPurchase" => "xxxx",
"originalPurchase" => "xxxx",
"tags" => [],
"userId" => "xxx",
"productSku" => "xxxx",
"productType" => "renewable_subscription",
"productGroupName" => "Group 1"
];

self::assertInstanceOf(Purchase::class, PurchaseFactory::buildPurchase($datas));
}
}

0 comments on commit 13b1b13

Please sign in to comment.