. */ /** * An interface to the payment table in the database */ class Payment extends Model { /** {@inheritDoc} */ public static $table = 'payment', $fillable_columns = ['offerId', 'date', 'braintree_id', 'braintree_status'], $timestamps = ['date']; /** * Get the offer that this payment is linked to * * @return offer The offer */ public function getOffer() { return new Offer($this->pdo, $this->offerId); } /** * Make a mailer to send about this payment * * @return Mailer The mailer */ public function mailer() { $mailer = new Mailer($this->pdo); $mailer->setOffer($this->getOffer()); $lang = $this->getOffer()->getContact()->language; $mailer->Subject = Correspondence::__('mail-offer-paid-subject', $lang); $mailer->Body = Correspondence::__r('mail-offer-paid', $lang, $this); return $mailer; } /** * Send the payment notification to the offer's contact * * @return bool The result of Mailer::send */ public function send() { return $this->mailer()->send(); } /** * Refresh the Braintree status * * @return string|null The new status, or null if this Payment does not have a Braintree id */ public function refreshBraintreeStatus() { if (is_null($this->braintree_id)) { return null; } $trans = Braintree_Transaction::find($this->braintree_id); $this->braintree_status = $trans->status; } /** * Is the Braintree transaction finished? * * @return bool|null True iff the Braintree transaction is finished, or null if this Payment does not have a Braintree id */ public function isBraintreeFinished() { if (is_null($this->braintree_id)) { return null; } return in_array($this->braintree_status, [ Braintree_Transaction::AUTHORIZATION_EXPIRED, Braintree_Transaction::GATEWAY_REJECTED, Braintree_Transaction::FAILED, Braintree_Transaction::PROCESSOR_DECLINED, Braintree_Transaction::SETTLED, Braintree_Transaction::VOIDED, Braintree_Transaction::SETTLEMENT_DECLINED, ]); } /** * Is the Braintree transaction successful? * * A transaction can be successful if it is not yet finished. * * @return bool|null True iff the Braintree transaction is successful, or null if this Payment does not have a Braintree id */ public function isBraintreeSuccessful() { if (is_null($this->braintree_id)) { return null; } return in_array($this->braintree_status, [ Braintree_Transaction::AUTHORIZING, Braintree_Transaction::AUTHORIZED, Braintree_Transaction::SETTLED, Braintree_Transaction::SETTLING, Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT, Braintree_Transaction::SETTLEMENT_PENDING, Braintree_Transaction::SETTLEMENT_CONFIRMED, ]); } }