Skip to content

Commit

Permalink
Merge pull request #989 from stripe/remi/codegen-f71053e
Browse files Browse the repository at this point in the history
Add support for the `PromotionCode` resource and APIs
  • Loading branch information
remi-stripe committed Aug 5, 2020
2 parents f87a526 + 10fb796 commit 01fa5d5
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -37,7 +37,7 @@ matrix:

env:
global:
- STRIPE_MOCK_VERSION=0.90.0
- STRIPE_MOCK_VERSION=0.95.0
cache:
directories:
- $HOME/.composer/cache/files
Expand Down
2 changes: 2 additions & 0 deletions init.php
Expand Up @@ -124,6 +124,7 @@
require __DIR__ . '/lib/Plan.php';
require __DIR__ . '/lib/Price.php';
require __DIR__ . '/lib/Product.php';
require __DIR__ . '/lib/PromotionCode.php';
require __DIR__ . '/lib/Radar/EarlyFraudWarning.php';
require __DIR__ . '/lib/Radar/ValueList.php';
require __DIR__ . '/lib/Radar/ValueListItem.php';
Expand Down Expand Up @@ -191,6 +192,7 @@
require __DIR__ . '/lib/Service/PlanService.php';
require __DIR__ . '/lib/Service/PriceService.php';
require __DIR__ . '/lib/Service/ProductService.php';
require __DIR__ . '/lib/Service/PromotionCodeService.php';
require __DIR__ . '/lib/Service/Radar/EarlyFraudWarningService.php';
require __DIR__ . '/lib/Service/Radar/ValueListService.php';
require __DIR__ . '/lib/Service/Radar/ValueListItemService.php';
Expand Down
1 change: 1 addition & 0 deletions lib/Checkout/Session.php
Expand Up @@ -24,6 +24,7 @@
*
* @property string $id Unique identifier for the object. Used to pass to <code>redirectToCheckout</code> in Stripe.js.
* @property string $object String representing the object's type. Objects of the same type share the same value.
* @property null|bool $allow_promotion_codes Enables user redeemable promotion codes.
* @property null|int $amount_subtotal Total of all items before discounts or taxes are applied.
* @property null|int $amount_total Total of all items after discounts and taxes are applied.
* @property null|string $billing_address_collection Describes whether Checkout should collect the customer's billing address.
Expand Down
1 change: 1 addition & 0 deletions lib/Coupon.php
Expand Up @@ -15,6 +15,7 @@
* @property string $id Unique identifier for the object.
* @property string $object String representing the object's type. Objects of the same type share the same value.
* @property null|int $amount_off Amount (in the <code>currency</code> specified) that will be taken off the subtotal of any invoices for this customer.
* @property \Stripe\StripeObject $applies_to
* @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
* @property null|string $currency If <code>amount_off</code> has been set, the three-letter <a href="https://stripe.com/docs/currencies">ISO code for the currency</a> of the amount to take off.
* @property string $duration One of <code>forever</code>, <code>once</code>, and <code>repeating</code>. Describes how long a customer who applies this coupon will get the discount.
Expand Down
33 changes: 33 additions & 0 deletions lib/PromotionCode.php
@@ -0,0 +1,33 @@
<?php

// File generated from our OpenAPI spec

namespace Stripe;

/**
* A Promotion Code represents a customer-redeemable code for a coupon. It can be
* used to create multiple codes for a single coupon.
*
* @property string $id Unique identifier for the object.
* @property string $object String representing the object's type. Objects of the same type share the same value.
* @property bool $active Whether the promotion code is currently active. A promotion code is only active if the coupon is also valid.
* @property string $code The customer-facing code. Regardless of case, this code must be unique across all active promotion codes for each customer.
* @property \Stripe\Coupon $coupon A coupon contains information about a percent-off or amount-off discount you might want to apply to a customer. Coupons may be applied to <a href="https://stripe.com/docs/api#invoices">invoices</a> or <a href="https://stripe.com/docs/api#create_order-coupon">orders</a>. Coupons do not work with conventional one-off <a href="https://stripe.com/docs/api#create_charge">charges</a>.
* @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
* @property null|string|\Stripe\Customer $customer The customer that this promotion code can be used by.
* @property null|int $expires_at Date at which the promotion code can no longer be redeemed.
* @property bool $livemode Has the value <code>true</code> if the object exists in live mode or the value <code>false</code> if the object exists in test mode.
* @property null|int $max_redemptions Maximum number of times this promotion code can be redeemed.
* @property \Stripe\StripeObject $metadata Set of <a href="https://stripe.com/docs/api/metadata">key-value pairs</a> that you can attach to an object. This can be useful for storing additional information about the object in a structured format.
* @property \Stripe\StripeObject $restrictions
* @property int $times_redeemed Number of times this promotion code has been used.
*/
class PromotionCode extends ApiResource
{
const OBJECT_NAME = 'promotion_code';

use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Retrieve;
use ApiOperations\Update;
}
2 changes: 2 additions & 0 deletions lib/Service/CoreServiceFactory.php
Expand Up @@ -39,6 +39,7 @@
* @property PlanService $plans
* @property PriceService $prices
* @property ProductService $products
* @property PromotionCodeService $promotionCodes
* @property Radar\RadarServiceFactory $radar
* @property RefundService $refunds
* @property Reporting\ReportingServiceFactory $reporting
Expand Down Expand Up @@ -95,6 +96,7 @@ class CoreServiceFactory extends \Stripe\Service\AbstractServiceFactory
'plans' => PlanService::class,
'prices' => PriceService::class,
'products' => ProductService::class,
'promotionCodes' => PromotionCodeService::class,
'radar' => Radar\RadarServiceFactory::class,
'refunds' => RefundService::class,
'reporting' => Reporting\ReportingServiceFactory::class,
Expand Down
72 changes: 72 additions & 0 deletions lib/Service/PromotionCodeService.php
@@ -0,0 +1,72 @@
<?php

// File generated from our OpenAPI spec

namespace Stripe\Service;

class PromotionCodeService extends \Stripe\Service\AbstractService
{
/**
* Returns a list of your promotion codes.
*
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\Collection
*/
public function all($params = null, $opts = null)
{
return $this->requestCollection('get', '/v1/promotion_codes', $params, $opts);
}

/**
* A promotion code points to a coupon. You can optionally restrict the code to a
* specific customer, redemption limit, and expiration date.
*
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\PromotionCode
*/
public function create($params = null, $opts = null)
{
return $this->request('post', '/v1/promotion_codes', $params, $opts);
}

/**
* Retrieves the promotion code with the given ID.
*
* @param string $id
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\PromotionCode
*/
public function retrieve($id, $params = null, $opts = null)
{
return $this->request('get', $this->buildPath('/v1/promotion_codes/%s', $id), $params, $opts);
}

/**
* Updates the specified promotion code by setting the values of the parameters
* passed. Most fields are, by design, not editable.
*
* @param string $id
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\PromotionCode
*/
public function update($id, $params = null, $opts = null)
{
return $this->request('post', $this->buildPath('/v1/promotion_codes/%s', $id), $params, $opts);
}
}
1 change: 1 addition & 0 deletions lib/StripeClient.php
Expand Up @@ -39,6 +39,7 @@
* @property \Stripe\Service\PlanService $plans
* @property \Stripe\Service\PriceService $prices
* @property \Stripe\Service\ProductService $products
* @property \Stripe\Service\PromotionCodeService $promotionCodes
* @property \Stripe\Service\Radar\RadarServiceFactory $radar
* @property \Stripe\Service\RefundService $refunds
* @property \Stripe\Service\Reporting\ReportingServiceFactory $reporting
Expand Down
1 change: 1 addition & 0 deletions lib/Util/ObjectTypes.php
Expand Up @@ -63,6 +63,7 @@ class ObjectTypes
\Stripe\Plan::OBJECT_NAME => \Stripe\Plan::class,
\Stripe\Price::OBJECT_NAME => \Stripe\Price::class,
\Stripe\Product::OBJECT_NAME => \Stripe\Product::class,
\Stripe\PromotionCode::OBJECT_NAME => \Stripe\PromotionCode::class,
\Stripe\Radar\EarlyFraudWarning::OBJECT_NAME => \Stripe\Radar\EarlyFraudWarning::class,
\Stripe\Radar\ValueList::OBJECT_NAME => \Stripe\Radar\ValueList::class,
\Stripe\Radar\ValueListItem::OBJECT_NAME => \Stripe\Radar\ValueListItem::class,
Expand Down
6 changes: 3 additions & 3 deletions tests/Stripe/AccountLinkTest.php
Expand Up @@ -18,9 +18,9 @@ public function testIsCreatable()
);
$resource = AccountLink::create([
'account' => 'acct_123',
'failure_url' => 'https://stripe.com/failure',
'success_url' => 'https://stripe.com/success',
'type' => 'custom_account_verification',
'refresh_url' => 'https://stripe.com/refresh_url',
'return_url' => 'https://stripe.com/return_url',
'type' => 'account_onboarding',
]);
static::assertInstanceOf(\Stripe\AccountLink::class, $resource);
}
Expand Down
72 changes: 72 additions & 0 deletions tests/Stripe/PromotionCodeTest.php
@@ -0,0 +1,72 @@
<?php

namespace Stripe;

/**
* @internal
* @covers \Stripe\PromotionCode
*/
final class PromotionCodeTest extends \PHPUnit\Framework\TestCase
{
use TestHelper;

const TEST_RESOURCE_ID = 'promo_123';

public function testIsListable()
{
$this->expectsRequest(
'get',
'/v1/promotion_codes'
);
$resources = PromotionCode::all();
static::assertInternalType('array', $resources->data);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resources->data[0]);
}

public function testIsRetrievable()
{
$this->expectsRequest(
'get',
'/v1/promotion_codes/' . self::TEST_RESOURCE_ID
);
$resource = PromotionCode::retrieve(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}

public function testIsCreatable()
{
$this->expectsRequest(
'post',
'/v1/promotion_codes'
);
$resource = PromotionCode::create([
'coupon' => 'co_123',
'code' => 'MYCODE',
]);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}

public function testIsSaveable()
{
$resource = PromotionCode::retrieve(self::TEST_RESOURCE_ID);
$resource->metadata['key'] = 'value';
$this->expectsRequest(
'post',
'/v1/promotion_codes/' . self::TEST_RESOURCE_ID
);
$resource->save();
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}

public function testIsUpdatable()
{
$this->expectsRequest(
'post',
'/v1/promotion_codes/' . self::TEST_RESOURCE_ID
);
$resource = PromotionCode::update(self::TEST_RESOURCE_ID, [
'metadata' => ['key' => 'value'],
]);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}
}
6 changes: 3 additions & 3 deletions tests/Stripe/Service/AccountLinkServiceTest.php
Expand Up @@ -33,9 +33,9 @@ public function testCreate()
);
$resource = $this->service->create([
'account' => 'acct_123',
'failure_url' => 'https://stripe.com/failure',
'success_url' => 'https://stripe.com/success',
'type' => 'custom_account_verification',
'refresh_url' => 'https://stripe.com/refresh_url',
'return_url' => 'https://stripe.com/return_url',
'type' => 'account_onboarding',
]);
static::assertInstanceOf(\Stripe\AccountLink::class, $resource);
}
Expand Down
75 changes: 75 additions & 0 deletions tests/Stripe/Service/PromotionCodeServiceTest.php
@@ -0,0 +1,75 @@
<?php

namespace Stripe\Service;

/**
* @internal
* @covers \Stripe\Service\PromotionCodeService
*/
final class PromotionCodeServiceTest extends \PHPUnit\Framework\TestCase
{
use \Stripe\TestHelper;

const TEST_RESOURCE_ID = 'promo_123';

/** @var \Stripe\StripeClient */
private $client;

/** @var PromotionCodeService */
private $service;

/**
* @before
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new PromotionCodeService($this->client);
}

public function testAll()
{
$this->expectsRequest(
'get',
'/v1/promotion_codes'
);
$resources = $this->service->all();
static::assertInternalType('array', $resources->data);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resources->data[0]);
}

public function testCreate()
{
$this->expectsRequest(
'post',
'/v1/promotion_codes'
);
$resource = $this->service->create([
'coupon' => 'co_123',
'code' => 'MYCODE',
]);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}

public function testRetrieve()
{
$this->expectsRequest(
'get',
'/v1/promotion_codes/' . self::TEST_RESOURCE_ID
);
$resource = $this->service->retrieve(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}

public function testUpdate()
{
$this->expectsRequest(
'post',
'/v1/promotion_codes/' . self::TEST_RESOURCE_ID
);
$resource = $this->service->update(self::TEST_RESOURCE_ID, [
'metadata' => ['key' => 'value'],
]);
static::assertInstanceOf(\Stripe\PromotionCode::class, $resource);
}
}
2 changes: 1 addition & 1 deletion tests/Stripe/Service/SubscriptionItemServiceTest.php
Expand Up @@ -60,7 +60,7 @@ public function testCreate()
'/v1/subscription_items'
);
$resource = $this->service->create([
'plan' => 'plan',
'price' => 'price_123',
'subscription' => 'sub_123',
]);
static::assertInstanceOf(\Stripe\SubscriptionItem::class, $resource);
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/SubscriptionItemTest.php
Expand Up @@ -45,7 +45,7 @@ public function testIsCreatable()
'/v1/subscription_items'
);
$resource = SubscriptionItem::create([
'plan' => 'plan',
'price' => 'price_123',
'subscription' => 'sub_123',
]);
static::assertInstanceOf(\Stripe\SubscriptionItem::class, $resource);
Expand Down

0 comments on commit 01fa5d5

Please sign in to comment.