/** * Create vouchers on order payment success event */ public function onPayment() { parent::onPayment(); if ($this->Coupons()->Count() < $this->Quantity) { $remaining = $this->Quantity - $this->Coupons()->Count(); for ($i = 0; $i < $remaining; $i++) { if ($coupon = $this->createCoupon()) { $this->sendVoucher($coupon); } } } }