/voucherify-java-sdk

Java SDK for Voucherify - coupons, vouchers, promo codes

Primary LanguageJava

Official Voucherify SDK for Java

Migration to 5.x | Setup | Synchronous, Rx or Async? | Error handling | Contributing | Changelog

API: Vouchers | Campaigns | Distributions | Validations | Redemptions | Customers | Orders | Loyalties | Rewards | Products | Validation Rules | Segments | Promotions | Events | Utils


Supported JRE versions

  • 8, 9, 10

Setup

Grab via Maven:

<dependency>
  <groupId>io.voucherify.client</groupId>
  <artifactId>voucherify-java-sdk</artifactId>
  <version>9.0.0</version>
</dependency>

or via Gradle

compile 'io.voucherify.client:voucherify-java-sdk:9.0.0'

Log-in to Voucherify web interface and obtain your Application Keys from Configuration:

VoucherifyClient voucherify = new VoucherifyClient.Builder()
            .setAppId("YOUR-APPLICATION-ID")
            .setClientSecretKey("YOUR-CLIENT-SECRET-KEY")
            .build();

API Endpoint

You can also specify additional client options - like API version or a different endpoint:

VoucherifyClient.Builder()
            .apiVersion(ApiVersion.V_2018_08_01)
            .setEndpoint("api.voucherify.io")
            .build();

Synchronous, Rx or Async?

All the methods in SDK are provided directly or in asynchronous or rx version:

Every method has a corresponding asynchronous extension which can be accessed through the async() or rx() method of the vouchers module.

try {
    VoucherResponse voucher = client.vouchers.create(createVoucher);
} catch (VoucherifyError e) {
    // error
}

or asynchronously:

client.vouchers().async().create(createVoucher, new VoucherifyCallback<VoucherResponse>() {
    @Override
    public void onSuccess(VoucherResponse result) {
    }

    @Override
    public void onFailure(VoucherifyError error) {
    // error
  }
});

or using RxJava:

client.vouchers()
        .rx()
        .create(createVoucher)
        .subscribe(new Action1<VoucherResponse>() {
            @Override
            public void call(VoucherResponse voucher) {

            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
            }
        });

API

This SDK is fully consistent with restful API Voucherify provides. Detailed descriptions and example responses you will find at official docs. Method headers point to more detailed params description you can use.

Each namespace provides method equivalents for RxJava and Async/callback style API calls.

Vouchers API

Methods are provided within voucherify.vouchers().* namespace.

voucherify.vouchers().create(CreateVoucher createVoucher);

Check voucher object.

voucherify.vouchers().get(String code);
voucherify.vouchers().update(String code, VoucherUpdate update)
voucherify.vouchers().delete(String code, boolean force)
voucherify.vouchers().list(VouchersFilter filter);
voucherify.vouchers().enable(String code);
voucherify.vouchers().disable(String code);
voucherify.vouchers().addBalance(String code, AddBalance addBalance);
voucherify.vouchers().importVouchers(ImportVouchers importVouchers);

[Get Qualified Vouchers]

voucherify.vouchers().getQualified(QualificationContext context, QualifiedResourceFilter filter);

Campaigns API

Methods are provided within voucherify.campaigns().* namespace.

voucherify.campaigns().create(CreateCampaign campaign);
voucherify.campaigns().addVoucher(String campaignName, AddVoucherToCampaign addVoucherToCampaing);
voucherify.campaigns().addVoucherWithCode(String campaignName, String code, AddVoucherToCampaign addVoucherToCampaing);
voucherify.campaigns().delete(String campaignName, DeleteCampaignParams params);
voucherify.campaigns().update(String name, UpdateCampaign updateCampaign);
voucherify.campaigns().importVouchers(String campaignName, String campaignName, CampaignImportVouchers importVouchers);

[Get Qualified Campaigns]

voucherify.campaigns().getQualified(QualificationContext context, QualifiedResourceFilter filter);

Distributions API

Methods are provided within voucherify.distributions().* namespace.

voucherify.distributions().publish(PublishVoucher publishVoucher);
voucherify.distributions().createExport(CreateExport createExport);
voucherify.distributions().getExport(String id);
voucherify.distributions().deleteExport(String id);
voucherify.distributions.list()
voucherify.distributions.list(ListPublicationsFilter filter)

Validations API

Methods are provided within voucherify.validations().* namespace.

voucherify.validations().validate(String code, VoucherValidation voucherValidation);
voucherify.validations().validate(PromotionValidation promotionValidation);

Redemptions API

Methods are provided within voucherify.redemptions.()* namespace.

voucherify.redemptions().redeem(String code, RedeemVoucher redeemVoucher);
voucherify.redemptions().redeem(String id, RedeemPromotion redeemPromotion);
voucherify.redemptions().list(RedemptionsFilter filter);
voucherify.redemptions().getForVoucher(String code);
voucherify.redemptions().get(String id);
voucherify.redemptions().rollback(String id, String reason, RollbackRedemption rollbackRedemption);

Check redemption rollback object.


Customers API

Methods are provided within voucherify.customers().* namespace.

voucherify.customers().create(Customer customer);

Check customer object.

voucherify.customers().get(String id);
voucherify.customers().update(Customer customer);
voucherify.customers().delete(String id);
voucherify.customers.list()
voucherify.customers.list(CustomersFilter filter)

Orders API

Methods are provided within voucherify.orders.* namespace.

voucherify.orders().create(CreateOrder order)

Check the order object.

voucherify.orders().get(String orderId)
voucherify.orders().update(UpdateOrder order)
voucherify.orders().list()
voucherify.orders().list(OrdersFilter filter)

Rewards API

voucherify.rewards().create(reward);
voucherify.rewards().get(id);
voucherify.rewards().update(id, reward);
voucherify.rewards().list(params);
voucherify.rewards().delete(id);
voucherify.rewards().listAssignments(reward_id, query);
voucherify.rewards().createAssignment(reward_id, assignment);
voucherify.rewards().updateAssignment(reward_id, assignment_id, assignment);
voucherify.rewards().deleteAssignment(reward_id, assignment_id);

Loyalties API

voucherify.loyalties().list(filter);
voucherify.loyalties().create(loyalty);
voucherify.loyalties().get(id);
voucherify.loyalties().update(id, loyalty);
voucherify.loyalties().delete(id, params);
voucherify.loyalties().listRewardAssignments(loyalty_id, query);
voucherify.loyalties().createRewardAssignment(loyalty_id, assignment);
voucherify.loyalties().updateRewardAssignment(loyalty_id, assignment_id, assignment);
voucherify.loyalties().deleteRewardAssignment(loyalty_id, assignment_id);
voucherify.loyalties().listEarningRules(loyalty_id, query);
voucherify.loyalties().createEarningRules(loyalty_id, earning_rule);
voucherify.loyalties().updateEarningRule(loyalty_id, earning_rule_id, earning_rule);
voucherify.loyalties().deleteEarningRule(loyalty_id, earning_rule_id);
voucherify.loyalties().listMembers(loyalty_id, query);
voucherify.loyalties().addMember(loyalty_id, member);
voucherify.loyalties().getMember(loyalty_id, member_id);
voucherify.loyalties().redeemReward(loyalty_id, member_id, add_balance);
voucherify.loyalties().addLoyaltyCardBalance(loyalty_id, member_id, redeem_reward);

Products API

Methods are provided within voucherify.products().* namespace.

voucherify.products().create(Product product);

Check product object.

voucherify.products().get(String id);
voucherify.products().update(Product product);
voucherify.products().delete(String id, DeleteProductParams params);
voucherify.products().list(ProductsFilter filter);
voucherify.products().createSKU(String productId, SKU sku);

Check SKU object.

voucherify.products().getSKU(String productId, String skuId);
voucherify.products().updateSKU(String product_id, SKU sku);
voucherify.products().deleteSKU(String productId, String skuId, DeleteSKUParams params);
voucherify.products().listSKU(String productId)

Validation Rules API

Methods are provided within voucherify.validationRules().* namespace.

voucherify.validationRules().create(CreateBusinessValidationRule rules);
voucherify.validationRules().get(String id);
voucherify.validationRules().update(UpdateBusinessValidationRule rule);

[List Validation Rules]

voucherify.validationRules().list(BusinessValidationRuleFilter filer);
voucherify.validationRules().delete(String id);

[Create Validation Rule Assignment]

voucherify.validationRules().createAssignment(String ruleId, CreateBusinessValidationRuleAssignment assignment);

[List Validation Rule Assignments]

voucherify.validationRules().listAssignments(String ruleId, BusinessValidationRuleAssignmentFilter filer);

[Delete Validation Rule Assignment]

voucherify.validationRules().delete(String ruleId, String assignmentId);

Segments API

Methods are provided within voucherify.segments().* namespace.

voucherify.segments().create(Segment segment);
voucherify.segments().get(String id);
voucherify.segments().delete(String id);

Promotions API

Methods are provided within voucherify.promotions().* namespace.

voucherify.promotions().create(CreatePromotionCampaign campaign);
voucherify.promotions().list(String campaignId);
voucherify.promotions().addPromotionTier(String tierId, Tier tier);
voucherify.promotions().updatePromotionTier(String tierId, Tier tier);
voucherify.promotions().deletePromotionTier(String tierId);

Events API

Methods are provided within voucherify.events.* namespace.

Check customer object.

voucherify.events.track(CustomEvent customEvent)

Migration to 6.0

Version 6.0 of the SDK is not backwards compatible with previous version Changes made in version 6.0 relate to PublishVoucherResponse class and RollbackRedemptionResponse class.

Classes changes

  • RollbackRedemptionResponse class uses VoucherResponse class as type for voucher field instead of PublishVoucherResponse

  • PublishVoucherResponse class has additional fields to be compatible with API version - 2017-04-20:

    • id
    • object
    • createdAt
    • customerId
    • trackingId
    • voucher

    When API version 2017-04-05 is used, the PublishVoucherResponse object returned from distributions().publish call will use older structure and new fields will be set to null. Developers using API version 2017-04-05 can still safely access returned fields as before.
    When API version 2017-04-20 is used, the PublisherVoucherResponse object returned from distributions().publish call will use new structure and new fields will be set to proper values.


Migration to 5.0

Version 5.x of the SDK is not backwards compatible with previous version Changes made in version 5.x mostly relate to grouping methods within namespaces.

Methods changes:

Package changes:

  • pl.rspective.voucherify.client -> io.voucherify.client

Classes changes

Most of the classes were moved under the model/moduleName package. For example:

  • model/customer and model/customer/response (which contains only response classes)

Builders

In most cases invoking builders has changed in the following way:

  • new Customer.Builder() -> Customer.builder()

Utils

Available methods

  • VoucherifyUtils.calculatePrice(BigDecimal basePrice, Voucher voucher, BigDecimal unitPrice)
  • VoucherifyUtils.calculateDiscount(BigDecimal basePrice, Voucher voucher, BigDecimal unitPrice)

Error handling

When an abnormal situation (http calls return 4xx or 5xx, network issues, )a VoucherifyError. It contains following properties:

  • code - HTTP status code.
  • message - a human-readable message providing short description about the error.
  • details - a human-readable message providing more details about the error, usually includes a hint on how to fix the error.
  • key - a short string describing the kind of error that occurred.

###Examples

Synchronous

try {
    voucherify.vouchers().create(createVoucher);
  } catch(VoucherifyError e) {
    // Error handling
  }

RX java

voucherify.vouchers().rx().create(createVoucher)
  .doOnError(new Action1<VoucherifyError>() {
    
    @Override
    public void call(VoucherifyError error) {
     // Error handling
    }
  });

Async

voucherify.vouchers().async().create(createVoucher, new VoucherifyCallback<VoucherResponse>() {
  
  @Override
  public void onFailure(VoucherifyError error) {
     // Error handling
  }
});

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rspective/voucherify-java-sdk.

Changelog

  • 2019-09-30 - 9.0.0 - Updated retrofit and rxjava to version >= 2.x. Dropped support for JDK 1.6 and 1.7
  • 2019-09-30 - 8.1.0 - Extended QualifiedResourceFilter to support order query param.
  • 2019-08-22 - 8.0.1 - Added missing id to VoucherResponse.
  • 2019-06-19 - 8.0.0 - Hide API versioning in setEndpoint method.
  • 2019-05-27 - 7.4.1 - Restore discount_amount for OrderResponse.
  • 2019-05-16 - 7.4.0 - API for getting qualified Vouchers and Campaigns.
  • 2019-04-23 - 7.3.0 - Loyalties API, Rewards API.
  • 2019-03-21 - 7.2.0 - Added ValidationRules assignments property to Voucher and Campaign responses, added Referral to CustomEvent, updated OrderResponse object with new properties.
  • 2019-02-01 - 7.1.3 - Fixed deserialisation of CampaignResponse for newest API version.
  • 2019-02-01 - 7.1.2 - Fixed deserialisation of VoucherResponse for newest API version.
  • 2019-01-28 - 7.1.1 - Fixed release.
  • 2019-01-25 - 7.1.0 - Extended OrderItem and OrderItemResponse with OrderItemProduct and OrderItemSKU.
  • 2018-12-20 - 7.0.0 - Replaced Validation Rules with Business Validation Rules. Added API Verion v2018-08-01.
  • 2018-12-04 - 6.2.0 - Added support for Order when listing vouchers.
  • 2018-11-08 - 6.1.0 - Added support for advanced filters for Vouchers.
  • 2018-11-08 - 6.0.9 - Fix Segment metadata entry builder.
  • 2018-10-24 - 6.0.8 - Add discountAmount to OrderItemResponse.
  • 2018-10-19 - 6.0.7 - Add amount to OrderItem.
  • 2018-10-17 - 6.0.6 - Added ApplicableTo object to response of redeem/validate methods for vouchers and promotions.
  • 2018-10-10 - 6.0.5 - Added ValidationRules in VoucherResponse object.
  • 2018-09-09 - 6.0.4 - Added filtering vouchers by customer.
  • 2018-05-21 - 6.0.3 - Added support for price when creating Price model.
  • 2018-04-05 - 6.0.2 - Added support for unitType when creating Discount Voucher, removed obsolete VoucherType from validation response. Added class PromotionTierValidationResponse for proper promotion tier response mapping for promotion validation returned from voucherify.validations().validate(PromotionValidation) method.
  • 2018-04-05 - 6.0.1 - Fix response class accessibility from package to public. OrderItem and OrderItemResponse classes now supports price field.
  • 2018-04-04 - 6.0.0 - Response from Publish Voucher method now includes additional fields and structure introduced when using ApiVersion.V_2017_04_20. RollbackRedemptionResponse object uses VoucherResponse as voucher field instead of PublishVoucherResponse.
  • 2018-04-03 - 5.3.2 - Add missing fields in Validation and Redeem responses, provide more examples related to percent discount voucher.
  • 2018-04-03 - 5.3.1 - Add Order to Validation response.
  • 2018-01-14 - 5.3.0 - API Version set in header by default, Events API, Orders API, added missing methods to Customers, Distributions and Campaigns modules.
  • 2017-09-28 - 5.2.0 - Support for additional ValidationRules properties, additional examples, license info update.
  • 2017-09-19 - 5.1.2 - Fix creating VoucherifyError from Retrofit errors, fix typo in RedeemVoucherBuilder.
  • 2017-08-21 - 5.1.1 - Added metadata to VoucherValidationResponse.
  • 2017-08-21 - 5.1.0 - Added metadata to VoucherValidation.
  • 2017-08-18 - 5.0.1 - Fix for retrofit response body mapping.
  • 2017-05-10 - 5.0.0 - Separate modules, general refactoring, updated models.
  • 2017-04-21 - 4.2.0 - Moved validation to a separate module.
  • 2017-04-20 - 4.1.0 - Added method to validate voucher.
  • 2017-04-11 - 4.0.0 - Fixed redemptions list filter and response format.
  • 2016-12-02 - 3.6.1 - Added gift balance. Enhanced utils to support gift vouchers.
  • 2016-10-07 - 3.6.0 - Added a method to publish voucher.

See more in Changelog

License

The SDK is available as open source under the terms of the MIT License.