Skip to content

Instantly share code, notes, and snippets.

@swport
Last active June 25, 2023 06:31
Show Gist options
  • Save swport/afd9292412752df9e2e086ac38030e8f to your computer and use it in GitHub Desktop.
Save swport/afd9292412752df9e2e086ac38030e8f to your computer and use it in GitHub Desktop.
WordPress Rest API endpoint to process payments via WC
<?php
/*
* Suitable for NON-HOSTED payment gateways wallets, funds, etc.
*
* You can also process HOSTED payment gateways like paypal, but you'll get a rediect URL at the end
* and you have to handle that depending on what client (android, ios) you're catering to.
* You can have the redirect URL open up in a web-view-client and collect payment.
*
**/
function process_checkout($request)
{
$params = $request->get_json_params();
// fields
$_billing = array(
'billing_first_name', 'billing_last_name', 'billing_city', 'billing_state',
'billing_country', 'billing_address_1', 'billing_postcode', 'billing_phone', 'billing_email'
);
// only when ship_to_different_address is set to 1
$_shipping = array(
'shipping_first_name', 'shipping_last_name', 'shipping_address_1', 'shipping_city',
'shipping_state', 'shipping_postcode'
);
// extra non-required fields
$_extras = array(
'billing_company', 'shipping_company', 'billing_address_2', 'shipping_address_2',
'shipping_phone'
);
if( isset($params['ship_to_different_address']) && $params['ship_to_different_address'] == '1' ) {
$intersect = array_diff( array_merge($_billing, $_shipping), array_keys($params));
} else {
$intersect = array_diff( $_billing, array_keys($params));
}
// CHECKS BEGINS
if( ! empty($intersect) ) {
jwt_auth_wp_json_error_data(
__('Missing Billing / Shipping Details', 'jwt-rest-api'),
array_values($intersect)
);
}
// TODO: check for valid payment/ shipping method
if(! isset($params['payment_method']) ) {
jwt_auth_wp_json_error_data(
__('Payment Method Missing / Invalid', 'jwt-rest-api'),
array_values($intersect)
);
}
else if(! isset($params['shipping_method']) ) {
jwt_auth_wp_json_error_data(
__('Shipping Method Missing / Invalid', 'jwt-rest-api'),
array_values($intersect)
);
}
else if(! isset($params['terms']) || $params['terms'] != '1' ) {
jwt_auth_wp_json_error_data(
__('Please read and accept the terms and conditions to proceed with your order.', 'jwt-rest-api'),
array_values($intersect)
);
}
else if( isset($params['createaccount']) && (!isset($params['account_password']) || empty($params['account_password'])) ) {
jwt_auth_wp_json_error_data(
__('Please provide a password if you want to create an account.', 'jwt-rest-api'),
array_values($intersect)
);
}
// CHECKS PASSED
// start building post global var for checkout
$_POST = array();
$postvars =
isset($params['ship_to_different_address']) ?
array_merge($_billing, $_shipping) :
$_billing;
foreach ( $postvars as $value ) {
$_POST[ $value ] = sanitize_text_field( $params[$value] );
}
foreach ( $_extras as $value ) {
if( isset($params[$value]) ) {
$_POST[ $value ] = sanitize_text_field( $params[$value] );
}
}
// $_POST['woocommerce_checkout_update_totals'] = '1';
$_POST['terms'] = '1';
$_POST['payment_method'] = $payment_method = $params['payment_method'];
$_POST['shipping_method'] = $params['shipping_method'];
if( isset($params['createaccount']) ) {
$_POST['account_password'] = $params['account_password'];
}
// create a nonce & verify at the same request; otherwise it'll fail the payment
$_REQUEST['_wpnonce'] =
wp_create_nonce( 'woocommerce-process_checkout' );
// clear all accidental uncleard notices; otherwise the payment might fail
wc_clear_notices();
// make it look like an ajax request; so that it won't accidently redirect anywhere
wc_maybe_define_constant( 'DOING_AJAX', 1 );
// for orders that need payments
add_filter('woocommerce_payment_successful_result', function($data) use($payment_method) {
// handle checkout response however you want wp_send_json()
// it may have a redirect ($data['redirect']) url if you processed checkout using Hosted PG.
// you can handle depending on what your client is [ mobile app - android, ios ], open this redirect url in
// a webview maybe
}, 99999);
// for orders that don't need any payment - store pick-up, cash on delivery, etc.
// or maybe when the entire order is paid using some sort of funds and order total turns to zero
add_filter('woocommerce_checkout_no_payment_needed_redirect', function($data) use($payment_method) {
// handle checkout response however you want wp_send_json()
});
WC()->checkout()->process_checkout();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment