How to get discount details by coupon code in magento 2 programatily

I wasn't that far from the solution:

protected $coupon;
protected $saleRule;     

public function __construct(
    \Magento\SalesRule\Model\Coupon $coupon,
    \Magento\SalesRule\Model\Rule $saleRule
)
{
    $this->coupon = $coupon;
    $this->saleRule = $saleRule;
    parent::__construct($context);
}

...

public function getDiscount($couponCode)
{
    $ruleId =   $this->coupon->loadByCode($couponCode)->getRuleId();
    $rule = $this->saleRule->load($ruleId);
    return $rule->getDiscountAmount();
}

Of course need a bit of validation to be sure the rule exist


As per Magento 2 coding standards, we should user API interface instead of direct use of the model.

protected $couponModel;

protected $ruleRepository;     

public function __construct(
    \Magento\SalesRule\Model\Coupon $couponModel,
    \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository
) {
    $this->couponModel = $coupon;
    $this->ruleRepository = $saleRule;
}


public function getDiscount($couponCode)
{
    $ruleId =  $this->couponModel->loadByCode($couponCode)->getRuleId();
    $rule = $this->ruleRepository->getById($ruleId);
    return $rule->getDiscountAmount();
}

Note: I used direct model \Magento\SalesRule\Model\Coupon because there is no method available to get the coupon by id in interface \Magento\SalesRule\Api\CouponRepositoryInterface