Payment gateways
Poly Community takes payments through the payments part of the WordPlus SDK, which carries Stripe, PayPal, Mollie, CCBill and bank transfer. Any other gateway is a plugin of its own that adds a provider to the part. Once added, it shows under Settings → Services → Payments beside the others, with its own settings, check and test payment, and every component that sells (plans, tickets, courses, the items of members) can sell through it. Nothing of Poly Community changes for it.
The money goes to the account of the site owner at the gateway. The part never reads what the gateway sends: your provider turns it into a few normalized changes (paid, failed, renewed, refunded…), and the part applies them once, checked against what it fixed for the order.
Registering
function mypay_payment_providers( $providers ) {
$pay = function_exists( 'wordplus_payments' ) ? wordplus_payments() : null;
if ( ! $pay || $pay->api() < 4 ) {
return $providers; // a part older than this provider: leave it out
}
require_once __DIR__ . '/class-mypay.php';
$providers['mypay'] = new MyPay_Provider(); // the id: [a-z0-9_-]{1,40}, stored on every row, never changed
return $providers;
}
add_filter( 'wordplus_payment_providers', 'mypay_payment_providers' );
- Require the class inside the filter. The part loads its base class,
WordPlus_SDK_Payment_Provider, just before it runs the filter. A file that extends it at the top of your plugin would fail on a site where the part is not loaded yet. - Say the level you were written for from
needs()(4 today). On a site whose part is older, your provider is listed as waiting for an update and never called half. - The id is for good. It is kept on every order and subscription.
The class
class MyPay_Provider extends WordPlus_SDK_Payment_Provider {
public function needs() { return 4; }
public function label() { return 'MyPay'; }
public function fields( $mode ) {
return array(
'api_key' => array( 'label' => 'API key', 'type' => 'secret' ),
'public_key' => array( 'label' => 'Public key', 'type' => 'text' ),
);
}
public function ready( array $settings, $mode ) {
return '' !== (string) ( $settings['api_key'] ?? '' );
}
public function capabilities() {
return array( 'one_time' => true, 'recurring' => true, 'refunds' => true, 'on_site' => true );
}
public function start( array $order, array $product, array $price, array $buyer, array $urls, array $settings ) { /* … */ }
public function verify_webhook( WP_REST_Request $request, array $settings, $endpoint = '' ) { /* … */ }
public function notices( WP_REST_Request $request ) { /* … */ }
public function resolve( array $notice, array $settings ) { /* … */ }
public function fetch_order( array $order, array $settings ) { /* … */ }
}
| Method | What it does |
|---|---|
fields( $mode ) | The settings of each mode (test and live). A secret field is sealed on the site and never shown again. |
validate( $name, $value, $mode ) | Refuses a value with words for the owner, such as a live key under test mode. |
ready( $settings, $mode ) | Whether the keys of a mode are there. No request. |
check( $settings, $mode ) | Rows for Check: what works and what is missing, in the words of your gateway. |
capabilities() | What you can do (below). Everything you do not say is off. |
prepare( $product, $price, $settings ) | Makes a price at your gateway where it needs one first (plans for subscriptions). |
start( $order, $product, $price, $buyer, $urls, $settings ) | Begins a checkout: url (your page) or mount (your form on the site), with session. |
accepts( $price, $amount, $currency ) | Whether you can charge this amount in your own units (some gateways take whole units only for some currencies). Asked before you are offered at all. |
verify_webhook(), notices() | Believes a notice (its signature, or by asking your API) and names the events in it. |
resolve( $notice, $settings ) | Asks your API for the object as it is now and answers changes. |
fetch_order(), fetch_subscription() | The same, when the member comes back and on a schedule, so a lost notice changes nothing. |
refund(), cancel() | Money given back, and a subscription cancelled (at the end of its period, or at once). |
pause(), resume(), undo_cancel() | A subscription stopped for now and started again, and a cancel taken back, where you can. |
switch_plan( $subscription, $price, $timing, $urls, $settings ) | A member moves to another plan from Billing (below). |
dashboard( $kind, $ref, $mode ) | Your page of a payment or a subscription, for the Open in link an administrator sees. Never shown to members. |
unsupported() | (Protected) the error to answer for something you cannot do, in the words of the site. |
Capabilities
What you can do. Each one you leave out keeps its default, which is what every gateway did before it existed, so a gateway written earlier never has to say more.
| Capability | Default | Say it when |
|---|---|---|
one_time, recurring, custom | off | You take one-off payments, subscriptions, an amount the buyer chooses. |
trials, refunds, partial_refunds, tax, promo_codes | refunds on, the rest off | You do them by API. refunds off shows Refund in the dashboard of your gateway in place of Refund. |
currencies | every one | A list of the currencies you take, in upper case. |
adult | off | You accept adult content, for an 18+ site. |
portal | off | You have a page where a buyer manages what they pay. Billing in the community never sends members there: it is for plugins that choose to. |
sellers | off | Members sell on accounts of their own at your gateway, set up on its own pages. |
cancel_at_period_end | on | Off where cancelling ends billing at once: give the subscription as active with cancel_at_period_end and its paid period_end until then. |
subscription_objects | on | Off where renewals are payments and you have no subscription object to ask: you build each subscription change yourself from the payments you saw. |
verify | signature | fetch where your notices carry no signature: a notice is then believed only by asking your API for every object it names. |
events | off | You can list the events since a time, so the reconcile catches what a lost notice would have brought. |
choice_on_page | off | The buyer picks the plan on your page: start() gets the other prices of the product, and your order_paid names the price_id taken. |
country_first | off | You need the country of the buyer before a price (tax, local prices). |
portal_customer | on | Off where your portal needs no customer kept on the site. |
manual | off | An administrator confirms your payments (mark_paid(), instructions()). |
pause, undo_cancel | off | You can stop charging for now, and take back a cancel at the end of the period. |
on_site | off | The buyer can pay in your form on the payment page of the site (below). |
change_method | off | A member can give a subscription another card from Billing (below). |
switch_plan | none | A list of when a plan can change: renewal, now, or both (below). |
The Ways to pay screen, the choice of how to pay and Billing all follow them: a member is offered only what your gateway can do, and reads plainly what it cannot, with no way to your own pages.
Changes
A change is an array with a type and the facts of it:
| Type | When |
|---|---|
order_paid | Money taken, or nothing due (a free trial). Names the order, the amount, the currency and your references. |
order_processing | A payment on its way (a bank debit that takes days). |
order_failed, order_expired | Not paid. |
subscription | The whole current state of a subscription: status, paid period, set to end or not. |
renewal_paid, renewal_failed | A renewal. |
refund, dispute, dispute_closed | Money given back or questioned. |
Give only what your gateway said, never a guess. The part applies each change once, in any order, and ignores one whose amounts are not what it fixed.
Paying on the site
With on_site, the member pays in your form on the payment page of the site, never leaving it.
-
on_site_ready( $settings )says whether the keys of a mode can draw the form (a public key is there). The owner can still keep your gateway on its own page with Pay on this site. -
start()is then given$urls['on_site']. Answermountin place ofurl:return array(
'session' => $session_id,
'url' => '',
'mount' => array(
'scripts' => array( 'https://js.mypay.example/v2/pay.js', plugins_url( 'on-site.js', __FILE__ ) ),
'data' => array( 'key' => $settings['public_key'], 'token' => $client_token ),
),
);The scripts load on the payment page alone, in order.
datareaches the browser as it is: public values only. -
Your script registers the form:
window.wordplusPaymentsOnSite.register( 'mypay', {
async mount( element, data, { failed, done, submit } ) {
if ( typeof window.MyPay !== 'function' ) return failed();
const form = await window.MyPay( data.key ).form( data.token );
return form.mount( element );
},
} );End it the way your form ends: by itself on the return address (Stripe), with
done()(a form that ends in the page, as the buttons of PayPal), or withawait submit( { token } )(a form that ends with a token the server charges with).submitcalls youron_site_submit( $order, $payload, $settings ), which charges and answersurl, where the member goes next. Words you mark'buyer' => truein a WP_Error reach the member (a card declined). Any other error reaches them as plain words of the site.
Where the form cannot open, the page says nothing was charged and offers your own page: the part closes the session and calls start() again without on_site. Where your gateway cannot close a session (PayPal voids no order), answer url beside mount in the same answer: your own page for the same session, which the part keeps and sends the member to instead, with nothing closed or made twice. Nothing is believed from the browser: the part asks your API, as after your page.
Notices you must ask about
Where your gateway signs its notices, verify_webhook() checks the signature with the secret the owner saved, before anything of the request is read. Where it does not sign them (some send only an id), say 'verify' => 'fetch': verify_webhook() may then answer a mode without a signature, and resolve() asks your API for every object a notice names, so a forged notice changes nothing. The conformance suite sends one to make sure.
A card change from Billing
With change_method, a member gives a running subscription another card from Billing, on the payment page of the site:
change_method( $subscription, $urls, $settings )begins it, answeringurlormountasstart()does. Nothing is charged.change_method_done( $subscription, $session, $settings )makes the saved card the one the subscription is charged to, once your gateway says it was saved, and a WP_Errorpayments_waitinguntil then. Your notice of it should finish it too, for a member who closed the page.- On a subscription whose renewal failed, the new card should pay that renewal at once, not at your next retry days later. Stripe pays its open invoice with it. Your notices tell the part, as of any renewal.
Without it, Billing says plainly what to do instead, and nothing leads to the pages of your gateway.
Another plan from Billing
With switch_plan, a member moves a running subscription to another plan of the same group (another plan of the site, another tier of the same creator) from the Plans page and Billing. List the timings you can do:
renewal: nothing is charged now, and the next renewal takes the new price. The part keeps the member on the plan they paid for until then, whatever price your gateway already names, and makes the new plan theirs once a paid period past it is heard.now: the new plan at once, the difference charged (or credited) as your gateway reckons it, and the change made only once that charge went through.
The administrator chooses, per gateway, under When a member switches plan: at the next renewal (the default where you can), at once, dearer plans at once and cheaper ones at the renewal (where you list both), or not at all. switch_plan( $subscription, $price, $timing, $urls, $settings ) is given the new price prepared for you as for a checkout, and answers the changes you made (a subscription change naming the new price), or url with session where the member must approve at your gateway first (for a switch at once only, for now), or an error whose words marked 'buyer' => true reach the member, nothing changed. The price they pay now, asked again, takes back a switch kept for the renewal.
Without it, the member reads plainly that plans cannot be switched with your gateway: cancel, and join the other plan once theirs ends.
A pause from Billing
With pause, the administrator may let members pause and resume their own plan from Billing (Members can pause, off at first). pause() stops charging for now and resume() starts it again, each answering the subscription changes it made. The part keeps the access to the end of the time paid for, whatever period your notices name while you skip or void the bills. Resume so that no paused time is free and none is charged twice. Within the time paid for, charge nothing more. After it, charge a period from that day, at once.
The check
wp wordplus payments check mypay
On a site, it checks what can be checked without a sale: your provider loads for the level of the part, its capabilities are well formed, its fields and its check() answer in test mode (no live key is ever used), and a forged notice is refused. It writes nothing.
The full checks with money (a purchase paid however its notices come, a whole subscription life, refunds, a reconcile that finds what a lost notice would have brought) are the conformance suite of the SDK, tests/payments/ProviderConformance.php, run over a stand-in for your API. Its complete example provider, example-pay, is written by these rules and passes both.
What stays stable
A gateway you write today keeps working after every update. The WordPlus SDK holds it by tests on every change:
- The public surface is written down and checked. Every method of the base class and its arguments, every capability and its default, the change types and their keys, the hooks, the routes and the calls of the script are kept in one file, and a test fails when any of them is removed or changed in a way that would break a gateway: a method of the base class keeps its arguments exactly (your class declares the same ones), no method becomes one you must write, and a capability keeps its default.
- An old gateway runs on every update. A gateway written for level 3 and frozen, never edited again, goes through the whole conformance suite on every change of the SDK. A change that would make it fail is undone or made to work with it.
- The level only grows. A new level adds optional methods and capabilities whose defaults are what every gateway did before. A gateway written for an older level runs unchanged, and is never called for what it did not declare.
- Nothing you rely on is renamed or removed without a release that announces it first and keeps the old name working.
- Ask the level, not the version:
wordplus_payments()->api().