1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class UserPayCredits extends CreditsLib 9{ 10 function __construct() 11 { 12 global $user, $prefs; 13 $valid_credits = unserialize($prefs['payment_tikicredits_types']); 14 $credits_xcrates = unserialize($prefs['payment_tikicredits_xcrates']); 15 16 $userId = $this->get_user_id($user); 17 $uc = $this->getScaledCredits($userId); 18 19 $ret = []; 20 for ($i = 0, $cvalid_credits = count($valid_credits); $i < $cvalid_credits; $i++) { 21 $one = []; 22 $k = $valid_credits[$i]; 23 if (! empty($credits_xcrates[$i])) { 24 $one['xcrate'] = $credits_xcrates[$i]; 25 } else { 26 $one['xcrate'] = 1; 27 } 28 if (isset($uc[$k])) { 29 $one['remain'] = $uc[$k]['remain']; 30 $one['unit_text'] = $uc[$k]['unit_text']; 31 $one['display_text'] = $uc[$k]['display_text']; 32 } 33 $ret[$k] = $one; 34 } 35 36 $this->credits = $ret; 37 } 38 39 function setPrice($price) 40 { 41 $credits = $this->credits; 42 foreach ($credits as $k => $uc) { 43 $credits[$k]['price'] = $price * $credits[$k]['xcrate']; 44 if ($credits[$k]['price'] > $credits[$k]['remain']) { 45 $credits[$k]['enough'] = 0; 46 } else { 47 $credits[$k]['enough'] = 1; 48 } 49 } 50 $this->credits = $credits; 51 } 52 53 function payAmount($creditType, $amount, $invoice) 54 { 55 global $user; 56 $tikilib = TikiLib::lib('tiki'); 57 $paymentlib = TikiLib::lib('payment'); 58 $userId = $this->get_user_id($user); 59 $uc = $this->getCredits($userId); 60 if ($amount > $uc[$creditType]['remain']) { 61 return false; 62 } 63 $credits_amount = $amount * $this->credits[$creditType]['xcrate']; 64 if ($this->useCredits($userId, $creditType, $credits_amount)) { 65 $msg = tr("Tiki credits payment done on %0 for %1 (using %2)", $tikilib->get_short_datetime($tikilib->now), $amount, $creditType); 66 $paymentlib->enter_payment( 67 $invoice, 68 $amount, 69 'tikicredits', 70 [ 71 'info' => $msg, 72 'username' => $user, 73 'creditType' => $creditType, 74 'creditAmount' => $credits_amount 75 ] 76 ); 77 return true; 78 } else { 79 return false; 80 } 81 } 82} 83