Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions classes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ static function currency($code)
)));
$code = strtoupper($code);
$currencies = Q::json_decode($json, true);
if (!isset($currencies['symbols'][$code])) {
if (!Q::ifset($currencies, 'symbols', $code, null)) {
throw new Q_Exception_BadValue(array(
'internal' => 'currency',
'problem' => "no symbol found for $code"
), 'currency');
}
if (!isset($currencies['names'][$code])) {
if (!Q::ifset($currencies, 'names', $code, null)) {
throw new Q_Exception_BadValue(array(
'internal' => 'currency',
'problem' => "no name found for $code"
Expand Down Expand Up @@ -169,7 +169,7 @@ static function format($code, $amount, $short, $locale = null)
// -----------------------------------------------------

// Ensure locale exists
if (!isset($ASSETS_CURRENCY_LOCALES[$locale])) {
if (!Q::ifset($ASSETS_CURRENCY_LOCALES, $locale, null)) {
$locale = 'en_US';
}

Expand All @@ -182,7 +182,7 @@ static function format($code, $amount, $short, $locale = null)

// Determine number of decimals
$digits = 2;
if (isset($fmt['zero_decimals']) && $fmt['zero_decimals']) {
if (Q::ifset($fmt, 'zero_decimals', null)) {
$digits = 0;
}

Expand Down Expand Up @@ -276,10 +276,10 @@ static function pay($communityId, $userId, $amount, $reason, $options = array())
// }
// }

$fromPublisherId = isset($options["fromPublisherId"]) ? $options["fromPublisherId"] : null;
$fromStreamName = isset($options["fromStreamName"]) ? $options["fromStreamName"] : null;
$fromPublisherId = Q::ifset($options, "fromPublisherId", null);
$fromStreamName = Q::ifset($options, "fromStreamName", null);

$items = isset($options["items"]) ? $options["items"] : null;
$items = Q::ifset($options, "items", null);
if (!empty($items)) {
foreach ($items as $k => $item) {
$options['items'][$k]['amount'] = Assets_Credits::convert($options['items'][$k]['amount'], $currency, "credits");
Expand Down Expand Up @@ -339,7 +339,6 @@ static function pay($communityId, $userId, $amount, $reason, $options = array())
$referrerUserId
);


if ($discountCredits > 0) {
$needCredits = max(0, $needCredits - $discountCredits);
}
Expand All @@ -357,7 +356,7 @@ static function pay($communityId, $userId, $amount, $reason, $options = array())
$missingCredits = $needCredits - $haveCredits;
$amountCurrency = Assets_Credits::convert($missingCredits, "credits", $currency);

$metadata = isset($options["metadata"]) ? $options["metadata"] : array();
$metadata = Q::ifset($options, "metadata", array());

$instructions = array(
"userId" => $userId,
Expand Down Expand Up @@ -397,7 +396,7 @@ static function pay($communityId, $userId, $amount, $reason, $options = array())
try {
Assets::autoCharge(
$missingCredits,
$reason,
self::BOUGHT_CREDITS,
array(
"userId" => $userId,
"currency" => "credits",
Expand Down Expand Up @@ -678,7 +677,7 @@ static function charged($payments, $amount, $currency = 'USD', $options = array(
return false;
}

$charge->description = 'BoughtCredits';
$charge->description = self::BOUGHT_CREDITS;
if (!empty($options['reason'])) {
$charge->description .= ": ".$options['reason'];
}
Expand Down Expand Up @@ -797,7 +796,7 @@ static function honorOutstandingSuccessfulCharges($payments, $userId, $options =
// -------------------------------------------------
// Resolve user
// -------------------------------------------------
if (isset($options['user'])) {
if (Q::ifset($options, 'user', null)) {
$user = $options['user'];
if (!($user instanceof Users_User)) {
throw new Q_Exception_WrongType(array(
Expand Down Expand Up @@ -859,7 +858,7 @@ static function honorOutstandingSuccessfulCharges($payments, $userId, $options =
// ---------------------------------------------
// Skip refunded charges + cancel intent
// ---------------------------------------------
if (!$skipRefunds && isset($refunded[$chargeId])) {
if (!$skipRefunds && Q::ifset($refunded, $chargeId, null)) {

$intentToken = Q::ifset($c, 'metadata', 'intentToken', null);
if ($intentToken) {
Expand Down Expand Up @@ -1060,4 +1059,5 @@ static function autoCharge($amount, $reason, $options = array())
const JOINED_PAID_STREAM = 'JoinedPaidStream';
const LEFT_PAID_STREAM = 'LeftPaidStream';
const CREATED_COMMUNITY = 'CreatedCommunity';
const BOUGHT_CREDITS = 'BoughtCredits';
};
7 changes: 5 additions & 2 deletions classes/Assets/Credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static function grant($communityId, $amount, $reason, $userId = null, $attribute
'app' => Q::app(),
'operation' => '+'
), self::attributesSnapshot($assets_credits, $attributes));
if ($reason == 'BoughtCredits') {
if ($reason == Assets::BOUGHT_CREDITS) {
$type = 'Assets/credits/bought';
} elseif ($reason == 'BonusCredits') {
$type = 'Assets/credits/bonus';
Expand Down Expand Up @@ -937,7 +937,10 @@ static function getPaymentsInfo($userId, $toStream, $fromStream = null)
if ($conclusion["amount"] > 0) {
$stream = Streams::fetchOne($toPublisherId, $toPublisherId, $toStreamName);
$payment = $stream->getAttribute("payment");
if ($conclusion["amount"] >= self::convert(Q::ifset($payment, 'amount', 0), Q::ifset($payment, 'currency', 'credits'), 'credits')) {
$discount = self::discountInfo($stream, $userId);
$paymentCredits = self::convert(Q::ifset($payment, 'amount', 0), Q::ifset($payment, 'currency', 'credits'), 'credits');
$totalCredits = $paymentCredits - $discount['credits'];
if ($conclusion["amount"] >= $totalCredits) {
$conclusion["fullyPaid"] = true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions handlers/Assets/after/Assets_charged.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ function Assets_after_Assets_charged($params)

// issue community's currency to user
$communityId = Q::ifset($params, 'communityId', null);
Assets_Credits::grant($communityId, $credits, 'BoughtCredits', $userId, array(
Assets_Credits::grant($communityId, $credits, Assets::BOUGHT_CREDITS, $userId, array(
"charge" => @compact("amount", "currency"),
"token" => Q::ifset($options, 'token', null)
));

// check Assets/credits/bonus
$reason = Q::ifset($params, 'options', 'reason', null);
if ($reason == 'BoughtCredits') {
if ($reason == Assets::BOUGHT_CREDITS) {
Assets_Credits::awardBonus(null, $amount, $userId);
}

$text = Q_Text::get('Assets/content', array('language' => Users::getLanguage($userId)));
$description = Q::interpolate(Q::ifset($text, 'credits', 'forMessages', 'BoughtCredits', Q::ifset($text, 'credits', 'BoughtCredits', 'Bought {{amount}} credits')), array('amount' => $credits));
$description = Q::interpolate(Q::ifset($text, 'credits', 'forMessages', Assets::BOUGHT_CREDITS, Q::ifset($text, 'credits', 'BoughtCredits', 'Bought {{amount}} credits')), array('amount' => $credits));

$stream = Q::ifset($options, 'stream', null);
if ($stream) {
Expand Down
4 changes: 2 additions & 2 deletions text/Assets/content/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"PaymentToUser": "Transfer to {{toUserName}}",
"CreatePrivateChat": "for private chat with {{toUserName}}",
"CreatePaidStream": "for creating {{toStreamName}}",
"JoinedPaidStream": "for reservation in \"<a data-publisherId='{{toPublisherId}}' data-streamName='{{toStreamName}}'>{{toStreamTitle}}</a>\"",
"JoinedPaidStream": "for participation in \"<a data-publisherId='{{toPublisherId}}' data-streamName='{{toStreamName}}'>{{toStreamTitle}}</a>\"",
"CreatedCommunity": "for creating community \"{{communityId}}\"",
"LeftPaidStream": "as a reservation refund in \"<a data-publisherId='{{toPublisherId}}' data-streamName='{{toStreamName}}'>{{toStreamTitle}}</a>\"",
"LeftPaidStream": "as a reservation refund from \"<a data-publisherId='{{fromPublisherId}}' data-streamName='{{fromStreamName}}'>{{fromStreamTitle}}</a>\"",
"BoughtCredits": "Bought {{amount}} credits",
"BonusCredits": "Bonus {{amount}} credits",
"BuyAmountCredits": "Buy {{amount}} credits",
Expand Down
5 changes: 4 additions & 1 deletion web/js/Assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var Assets = Q.Assets = Q.plugins.Assets = Q.Method.define({
* @param {String} currency
*/
convertToCredits: function (amount, currency) {
if (currency.toLowerCase() === 'credits') {
return amount;
}
var exchange = Q.getObject(["exchange", currency], Q.Assets.Credits);

if (!exchange) {
Expand Down Expand Up @@ -704,7 +707,7 @@ Q.onInit.add(function () {

var _createNotice = function (message) {
// check if message already displayed
var messageId = message.getInstruction('messageId') || message.getInstruction('token');
var messageId = message.getInstruction('creditsId') || message.getInstruction('token');
if (Q.isEmpty(this.usedIds)) {
this.usedIds = [messageId];
} else if (this.usedIds.includes(messageId)) {
Expand Down
14 changes: 14 additions & 0 deletions web/js/methods/Assets/Credits/buy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Q.exports(function () {
amount: 10,
currency: 'USD',
missing: false,
reason: 'BoughtCredits',
skipDialog: false
}, options);

Expand All @@ -40,6 +41,19 @@ Q.exports(function () {
}));
});

Q.Template.set('Assets/credits/missing',
'<div class="Assets_credits_buy_missing">{{YouMissingCredits}}</div>' +
'<input type="hidden" name="amount" value="{{amount}}">' +
'<button class="Q_button" name="buy">{{texts.PurchaseCredits}}</button>'
);
Q.Template.set('Assets/credits/buy',
'{{#each bonuses}}' +
' <div class="Assets_credits_bonus">{{{this}}}</div>' +
'{{/each}}' +
'<div class="Assets_credits_buy"><input name="amount" value="{{amount}}"> {{texts.Credits}}</div>' +
'<button class="Q_button" name="buy">{{texts.PurchaseCredits}}</button>'
);

// Load payment lib
Q.Assets.Payments.load();

Expand Down
5 changes: 2 additions & 3 deletions web/js/methods/Assets/Payments/standardStripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ Q.exports(function(Assets, priv){

options = Q.extend({}, options);
if (options.reason) {
options.description = Q.text.Assets.payments.reasons[options.reason].interpolate({
amount: options.amount,
currency: options.currency
options.description = Q.text.Assets.credits.BuyAmountCredits.interpolate({
amount: Q.Assets.Credits.convertToCredits(options.amount, options.currency)
});
}

Expand Down