Source of file RecurringPayment.php
Size: 5,264 Bytes - Last Modified: 2021-12-23T10:33:28+00:00
/var/www/docs.ssmods.com/process/src/code/RecurringPayment.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 | <?php class RecurringPayment extends DataObject { public static $db = array( 'Status' => "Enum('Incomplete,Success,Failure,Pending','Incomplete')", 'Amount' => 'Money', 'Message' => 'Text', 'IP' => 'Varchar', 'ProxyIP' => 'Varchar', 'PaidForID' => "Int", 'PaidForClass' => 'Varchar', //The following fields store the recurring payment schedulling info 'Frequency' => "Enum('Weekly,Monthly,Yearly','Monthly')", 'StartingDate' => "Date", 'Times' => "Int", //Usered for store any Exception during this payment Process. 'ExceptionError' => 'Text' ); public static $has_one = array( 'PaidBy' => 'Member' ); public static $has_many = array( 'Payments' => "Payment" ); public static $create_table_options = array( 'MySQLDatabase' => 'ENGINE=InnoDB' ); public function populateDefaults() { parent::populateDefaults(); $this->Amount->Currency = Payment::site_currency(); $this->setClientIP(); } /** * Set the IP address of the user to this payment record. * This isn't perfect - IP addresses can be hidden fairly easily. */ public function setClientIP() { $proxy = null; $ip = null; if (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } else { $ip = null; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $proxy = $ip; $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } // Only set the IP and ProxyIP if none currently set if (!$this->IP) { $this->IP = $ip; } if (!$this->ProxyIP) { $this->ProxyIP = $proxy; } } public function CanPayNext() { $success = $this->Status = 'Success'; if ($this->Times && $successPayments = $this->SuccessPayments()) { return $success && $this->Times > $successPayments->count(); } else { return $success; } } public function SuccessPayments() { return DataObject::get("Payment", "\"RecurringPaymentID\" = '".(int)$this->ID."' AND \"Status\" = 'Success'", "\"PaymentDate\" DESC"); } public function Payments() { return DataObject::get("Payment", "\"RecurringPaymentID\" = '".(int)$this->ID."'"); } public function getNextPayment() { if ($latest = $this->getLatestPayment()) { $next = $this->generateNextPaymentFrom($latest); } else { $next = $this->generateFirstPayment(); } DB::getConn()->transactionSavepoint("NextPaymentGot"); return $next; } public function getLatestPayment($successonly = true) { if ($successonly) { return DataObject::get_one("Payment", "\"RecurringPaymentID\" = '".(int)$this->ID."' AND \"Status\" = 'Success'", false, "\"PaymentDate\" DESC"); } else { return DataObject::get_one("Payment", "\"RecurringPaymentID\" = '".(int)$this->ID."'", false, "\"PaymentDate\" DESC, \"LastEdited\" DESC"); } } public function payNext() { DB::getConn()->transactionStart(); try { if ($next = $this->getNextPayment()) { $next->payAsRecurring(); } DB::getConn()->transactionEnd(); } catch (Exception $e) { DB::getConn()->transactionRollback('NextPaymentGot'); DB::getConn()->transactionEnd(); $latestPayment = $this->getLatestPayment($successonly = false); $latestPayment->handleError($e); } } public function generateFirstPayment() { $payment = new Payment(); $payment->RecurringPaymentID = $this->ID; $payment->PaymentDate = $this->StartingDate; $payment->Amount->Amount = $this->Amount->Amount; $payment->Amount->Currency = $this->Amount->Currency; $payment->write(); return $payment; } public function generateNextPaymentFrom($latest) { switch ($this->Frequency) { case 'Weekly': $date = date('Y-m-d', strtotime('+1 week', strtotime($latest->PaymentDate))); break; case 'Monthly': $date = date('Y-m-d', strtotime('+1 month', strtotime($latest->PaymentDate))); break; case 'Yearly': $date = date('Y-m-d', strtotime('+1 year', strtotime($latest->PaymentDate))); break; } $payment = new Payment(); $payment->RecurringPaymentID = $this->ID; $payment->PaymentDate = $date; $payment->Amount->Amount = $this->Amount->Amount; $payment->Amount->Currency = $this->Amount->Currency; $payment->write(); return $payment; } public function handleError($e) { $this->ExceptionError = $e->getMessage(); $this->write(); } } |