MyStudio module APIs: payments, points, alerts and hooks

amtekkie · Today

MyStudio modules expose PHP functions and MyBB hooks that your own modules and themes can use: charge members through Payments, add or spend points, send alerts, add permalink prefixes and sitemap entries, and react to orders, licence keys, likes and awards.

Before calling any API
  • Check it exists with function_exists(): the module may be switched off

Always check first

Every module can be switched off, so check that a function exists before calling it:

if (function_exists('ms_pay_create') && ms_pay_available('EUR')) { … }

Payments: charge a member

Work out the amount on the server from your own data, never from the browser:

$r = ms_pay_create(array(
    'context'     => 'acme',                 // your module's key
    'ref'         => 'order-' . $orderId,    // your id for what is paid for
    'description' => 'Order #' . $orderId,   // shown to the payer
    'amount'      => '19.99',                // or 'amount_minor' => 1999
    'currency'    => 'EUR',
    'return_url'  => 'misc.php?action=acme_order&id=' . $orderId,
    'meta'        => array('order' => $orderId),
));
if (!empty($r['error'])) error(htmlspecialchars_uni($r['error']));
header('Location: ' . $r['url']);
exit;

Then act on the outcome with hooks. They fire on the payer's return, on webhooks and on Admin CP refunds:

HookArgumentsWhen
ms_payments_completedtxnMoney received; fires exactly once
ms_payments_refundedtxn, amount, refund_idEach refund
ms_payments_reversedtxnChargeback: revoke what was bought
ms_payments_failedtxnDeclined
ms_payments_statustxn, from, toEvery status change
$plugins->add_hook('ms_payments_completed', 'acme_paid');
function acme_paid(&$args)
{
    $txn = $args['txn'];
    if ($txn['context'] !== 'acme') return;
    // fulfil $txn['meta']['order']
}

Helpers: ms_pay_available(), ms_pay_is_paid($context, $ref), ms_pay_find(), ms_pay_get(), ms_pay_refund(), ms_pay_format($minor, $currency), ms_pay_to_minor(). Amounts are stored as integers in minor units. The Shop and the Ads Manager's ad sales are complete examples.

Points

if (function_exists('ms_points_change')) {
    // add 25 points, or spend with a negative amount (refused below 0)
    ms_points_change($uid, 25, 'acme_bonus', 'Bonus for your review', 'review-' . $id);
    echo ms_points_format(ms_points_balance($uid));
}

Every change is logged; the hook ms_points_changed (uid, amount, balance, type, ref) fires after each one.

Alerts

if (function_exists('ms_alerts_add_many')) {
    ms_alerts_add_many(array($uid), 'reply', $fromUid, $pid);
}

Alert types are defined in User Profile Extras. Themes print the bell with {$ms_alerts_header}.

SEO: addresses and the sitemap

  • ms_routes_prefixes (filter): register a path prefix for your pages, such as acme → misc.php?action=acme&item=. The SEO module writes the server rules and rewrites links.
  • ms_route_url($plain): the pretty address for a plain one. Check function_exists first.
  • ms_sitemap_sources (filter): add a list of your pages to the XML sitemap.
  • ms_modules_loaded: fires once every module is loaded.

Other hooks MyStudio fires

HookWhen
ms_shop_order_paid, ms_shop_order_refunded, ms_shop_order_statusShop orders change
ms_shop_product_extraAdd content to a product page
ms_shop_sub_accessA subscription's access changes
ms_payments_sub_status, ms_payments_sub_paymentGateway subscriptions change or charge
ms_lic_key_issued, ms_lic_key_statusLicence keys are issued or change status
ms_pex_likedA post is liked
ms_award_givenA member receives an award

Theme-side variables

  • $mybb->ms_options: the admin's layout choices, checked and filled with defaults.
  • $mybb->ms_theme_options: the active theme's settings.
  • $GLOBALS['ms_nav_links'] and $GLOBALS['ms_usercp_links']: module links for menus.

Collections have their own query API: see querying collections.