PHPackages                             bangerkuwranger/magento-2-coupon-code-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. bangerkuwranger/magento-2-coupon-code-api

ActiveMagento2-module[API Development](/categories/api)

bangerkuwranger/magento-2-coupon-code-api
=========================================

API Methods for generating a coupon code for a given cart price rule

1.0.1(9y ago)121045MITPHPPHP ~5.5.0|~5.6.0|~7.0.0

Since Mar 23Pushed 9y ago2 watchersCompare

[ Source](https://github.com/bangerkuwranger/Magento-2-Coupon-Code-API)[ Packagist](https://packagist.org/packages/bangerkuwranger/magento-2-coupon-code-api)[ RSS](/packages/bangerkuwranger-magento-2-coupon-code-api/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (3)Used By (0)

Magento 2 Coupon Code API
=========================

[](#magento-2-coupon-code-api)

This module adds three API endpoints that provide methods for generating coupon codes for an existing Magento 2 Cart Price Rule from an external source. It has been tested with the REST API, although it should work equally well with the SOAP/XML API as well. Usage documentation is for REST usage only.

Why?
----

[](#why)

Well, although Magento 2 provides a built-in method for generating a list of coupon codes for a promotional rule, in order to use these with another system (like a marketing email platform, or for contest systems, or even CRM systems like SalesForce), you have to generate a set number of coupon codes, and then export them via CSV. For on-demand generation, it makes more sense to generate codes as they're needed.

Installation
------------

[](#installation)

Installation is available via composer. The package name is bangerkuwranger/magento-2-coupon-code-api. Just run these commands at your Magento root: `composer require bangerkuwranger/magento-2-coupon-code-api``php bin/magento module:enable Bangerkuwranger_Couponcodeapi``php bin/magento setup:upgrade`

Usage
-----

[](#usage)

Make sure to familiarize yourself with the [Magento API](http://devdocs.magento.com/guides/v2.1/get-started/bk-get-started-api.html) system first.

In order to use the endpoints properly, you'll need to create a [Cart Price Rule](http://docs.magento.com/m2/ee/user_guide/marketing/price-rules-cart.html) that has the Coupon field set to Auto. The other settings are up to you. Make sure that your rule is working on your system before setting up the coupon generation logic.

This module allows you to check to make sure that a Magento customer can actually use the generated code by including a customer id when generating a new code. Magento will see which customer group the customer belongs to, and then verify that that group is allowed in your Cart Price Rule. It's optional, but useful if you are generating codes for specific customers. Setting the customer id parameter to zero will just generate the code without checking.

Anyhow, once installed, there will be three endpoints available:

1. GET /V1/bangerkuwranger/couponcode/getCartRule/
2. POST /V1/bangerkuwranger/couponcode/getCustIdByEmail/
3. POST /V1/bangerkuwranger/couponcode/getCouponCode/

The third one is the only really necessary endpoint; it's what actually generates the code for your Cart Price Rule. The other two are useful if you want to verify the Cart Rule's ID and the customer ID. All three methods require authentication of some sort; make sure to familiarize yourself with the [Magento API Authentication process](http://devdocs.magento.com/guides/v2.1/get-started/authentication/gs-authentication.html) so your logic incorporates the proper authentication steps. Once that's take care of, you can generate the code by making an API request to the getCouponCode endpoint, and you'll get a new coupon code back.

Recommended Logic Flows
-----------------------

[](#recommended-logic-flows)

### Without Customer Group Validation

[](#without-customer-group-validation)

1. Perform authentication to Magento API
2. Request information about the Cart Rule from the API (getCartRule).
3. Verify that Cart Rule details match as expected.
4. Create a new Coupon Code for the Cart Rule using ruleId and setting custId to 0. (getCouponCode)

### With Customer Group Authentication

[](#with-customer-group-authentication)

1. Perform authentication to Magento API
2. Request information about the Cart Rule from the API (getCartRule).
3. Verify that Cart Rule details match as expected.
4. Get custId and verify cust exists for email address using getCustIdByEmail endpoint.
5. If cust exists, continue. If not, you can create customer using Magento's built in API methods if that makes sense for your workflow.
6. Create a new Coupon Code for the Cart Rule using ruleId and custId (getCouponCode)

Endpoint Documentation
======================

[](#endpoint-documentation)

/bangerkuwranger/couponcode/getCartRule/
----------------------------------------

[](#bangerkuwrangercouponcodegetcartrule)

### Description

[](#description)

Given a cart rule id, this method returns an array of values from the cart rule if the rule exists, or an error if it does not exist or something else went wrong with the API transaction.

### Example

[](#example)

`curl -X GET "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCartRule/?ruleId=20" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre" `

### Method

[](#method)

GET

### Request *Query String* Parameters

[](#request-query-string-parameters)

`int $ruleId unique identifier for magento cart discount rule`

### Successful Response

[](#successful-response)

#### Format

[](#format)

```
[
	[0] (string): name,
	[1] (string): description,
	[2] (string): fromDate,
	[3] (string): toDate,
	[4] (int): usesPerCust,
	[5] (int): usesPerCoupon,
	[6] (bool): isActive,
	[7] (array):
		groupIds [
			(string) groupId,
			...additional groupIds...
		]
]

```

### HTTP 400 Response

[](#http-400-response)

#### Reason

[](#reason)

Bad Request

#### Format

[](#format-1)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 401 Response

[](#http-401-response)

#### Reason

[](#reason-1)

Not Authorized / Invalid Token

#### Format

[](#format-2)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 500 Response

[](#http-500-response)

#### Reason

[](#reason-2)

Server Error / Local Exception

#### Format

[](#format-3)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

/bangerkuwranger/couponcode/getCustIdByEmail/
---------------------------------------------

[](#bangerkuwrangercouponcodegetcustidbyemail)

### Description

[](#description-1)

Given an email address, this method returns a unique identifier for the magento customer, or an error if it does not exist or something else went wrong with the API transaction.

### Example

[](#example-1)

`curl -X POST "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCustIdByEmail/" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre" -d '{"email":"customer1@example.com"}'`

### Method

[](#method-1)

POST

### Request *Body* Parameters

[](#request-body-parameters)

`string $email email address to search for customer with`

### Successful Response

[](#successful-response-1)

#### Format

[](#format-4)

`(string) “custId”`

### HTTP 400 Response

[](#http-400-response-1)

#### Reason

[](#reason-3)

Bad Request

#### Format

[](#format-5)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 401 Response

[](#http-401-response-1)

#### Reason

[](#reason-4)

Not Authorized / Invalid Token

#### Format

[](#format-6)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 500 Response

[](#http-500-response-1)

#### Reason

[](#reason-5)

Server Error / Local Exception

#### Format

[](#format-7)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

/bangerkuwranger/couponcode/getCouponCode/
------------------------------------------

[](#bangerkuwrangercouponcodegetcouponcode)

### Description

[](#description-2)

Given a cart rule id and customer id (use the number zero if customer id is not being used), this method returns a unique coupon code for the magento cart rule, or an error if it does not exist or something else went wrong with the API transaction. If customer id is being used, this will check if customer belongs to a group that can use the coupon code first, returning an error if customer is not authorized.

### Example

[](#example-2)

`curl -X POST "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCouponCode/" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre" -d '{"ruleId":31,”custId”:1055}'`

### Method

[](#method-2)

POST

### Request *Body* Parameters

[](#request-body-parameters-1)

`int $ruleId unique identifier for magento cart discount rule``int $custId unique identifier for magento customer`

### Successful Response

[](#successful-response-2)

#### Format

[](#format-8)

`(string) “couponCode”`

### HTTP 400 Response

[](#http-400-response-2)

#### Reason

[](#reason-6)

Bad Request

#### Format

[](#format-9)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 401 Response

[](#http-401-response-2)

#### Reason

[](#reason-7)

Not Authorized / Invalid Token

#### Format

[](#format-10)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

### HTTP 500 Response

[](#http-500-response-2)

#### Reason

[](#reason-8)

Server Error / Local Exception

#### Format

[](#format-11)

```
error-response {
	message (string): Error message ,
	errors (error-errors, optional),
	code (integer, optional): Error code ,
	parameters (error-parameters, optional),
	trace (string, optional): Stack trace
}
error-errors [
	error-errors-item
]
error-parameters [
	error-parameters-item
]
error-errors-item {
	message (string, optional): Error message ,
	parameters (error-parameters, optional)
}
error-parameters-item {
	resources (string, optional): ACL resource ,
	fieldName (string, optional): Missing or invalid field name ,
	fieldValue (string, optional): Incorrect field value
}

```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Total

2

Last Release

3343d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/854f3c149da70ca3f31efaeb189016eb8b83386ca06a68e825496c7e610d4513?d=identicon)[bangerkuwranger](/maintainers/bangerkuwranger)

---

Top Contributors

[![bangerkuwranger](https://avatars.githubusercontent.com/u/2318848?v=4)](https://github.com/bangerkuwranger "bangerkuwranger (17 commits)")

### Embed Badge

![Health badge](/badges/bangerkuwranger-magento-2-coupon-code-api/health.svg)

```
[![Health](https://phpackages.com/badges/bangerkuwranger-magento-2-coupon-code-api/health.svg)](https://phpackages.com/packages/bangerkuwranger-magento-2-coupon-code-api)
```

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35816.3M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24015.5M18](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172437.8k11](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

94452.6k6](/packages/botman-driver-telegram)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
