Skip to content

Enhancement: Add Function to Check Voucher Validity #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/.editorconfig export-ignore
76 changes: 57 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,66 @@ on:
pull_request:
branches: [master]


jobs:
test:
runs-on: ${{ matrix.os }}
php-tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.4, 8.0, 8.1]
laravel: [8.*, 10.*]
stability: [prefer-lowest, prefer-stable]
php:
- '8.3'
- '8.2'
- '8.1'
- '8.0'
laravel:
- '10.*'
- '9.*'
- '8.*'
dependency-version:
- 'prefer-stable'

exclude:
- laravel: '10.*'
php: '8.0'
include:
- laravel: 10.*
testbench: ^8.0
- laravel: 8.*
testbench: ^6.6
- laravel: '10.*'
php: '8.3'
testbench: '8.*'
- laravel: '10.*'
php: '8.2'
testbench: '8.*'
- laravel: '10.*'
php: '8.1'
testbench: '8.*'
- laravel: '9.*'
php: '8.3'
testbench: '7.*'
- laravel: '9.*'
php: '8.2'
testbench: '7.*'
- laravel: '9.*'
php: '8.1'
testbench: '7.*'
- laravel: '9.*'
php: '8.0'
testbench: '7.*'
- laravel: '8.*'
php: '8.3'
testbench: '6.*'
- laravel: '8.*'
php: '8.2'
testbench: '6.*'
- laravel: '8.*'
php: '8.1'
testbench: '6.*'
- laravel: '8.*'
php: '8.0'
testbench: '6.*'

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest

steps:
- name: Checkout code
Expand All @@ -32,18 +75,13 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit
19 changes: 0 additions & 19 deletions .scrutinizer.yml

This file was deleted.

23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Laravel Vouchers 🎟

[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers)
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-vouchers/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-vouchers)
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-vouchers.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-vouchers)
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers)

This package can associate vouchers with your Eloquent models. This might come in handy, if you need to associate voucher codes with content that is stored in your Eloquent models.
Expand Down Expand Up @@ -130,7 +128,7 @@ class VideoCourse extends Model
use HasVouchers;
# ...
}

```
## Creating Vouchers

### Using the facade
Expand Down Expand Up @@ -220,6 +218,14 @@ $voucher = $user->redeemCode('ABCD-EFGH');
$videoCourse = $voucher->model;
```

## Validating Vouchers & Voucher Codes
The `isValidCode` and `isValidVoucher` methods on the `Vouchers` facade allow you to check if a voucher code is valid or if a voucher model is valid.

```php
Vouchers::isValidCode('ABCD-EFGH'); // true or false
Vouchers::isValidVoucher($voucher); // true or false
```

## Handling Errors

The `redeemCode` and `redeemVoucher` methods throw a couple of exceptions that you will want to catch and react to in your application:
Expand Down
26 changes: 26 additions & 0 deletions src/Vouchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public function check(string $code)
return $voucher;
}

/**
* @param string $code
* @return bool
*/
public function isValidCode(string $code): bool
{
try {
$this->check($code);
} catch (VoucherIsInvalid $exception) {
return false;
} catch (VoucherExpired $exception) {
return false;
}

return true;
}

/**
* @param Voucher $voucher
* @return bool
*/
public function isValidVoucher(Voucher $voucher): bool
{
return $this->isValidCode($voucher->code);
}

/**
* @return string
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/CanRedeemVouchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,28 @@ public function redeeming_vouchers_fires_an_event()
return $e->user->id === $user->id && $e->voucher->id === $voucher->id;
});
}

public function test_is_valid_code()
{
$this->assertFalse(Vouchers::isValidCode('invalid'));

$item = Item::create(['name' => 'Foo']);

$voucher = $item->createVoucher();

$this->assertTrue(Vouchers::isValidCode($voucher->code));
}

public function test_is_valid_voucher()
{
$item = Item::create(['name' => 'Foo']);
$voucher = $item->createVoucher([], today()->subDay());

$this->assertFalse(Vouchers::isValidVoucher($voucher));

$item = Item::create(['name' => 'Foo']);
$voucher = $item->createVoucher();

$this->assertTrue(Vouchers::isValidVoucher($voucher));
}
}