I'm very new to payment gateway integration. I learned that, orders can be created programmatically without adding products in woocommerce and done that.
Problem now is, I'm not sure how to automate the payment process once submit order button is clicked.
Currently when the order form is submitted, I create order programmatically into woocommerce and expect the user to redirected to selected(currently by default is billPlz) payment gateway page to make payment.
I already had woocomerce and billPlz payment gateway installed. And WC()->payment_gateways->get_available_payment_gateways()
does return me the billPlz.
However the payment form is not shown from the payment gateway for users to submit the payment into the billPlz.
I see 'success' message and redirected to mydomain/order-received/72?key=wc_order_5a0f8f3ead251
I assume this is the Thank you page?
How to redirect to payment gateway when order is created?
Order form (my own, not from woocomerce)
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="POST">
<input type="text" name="action" value="createOrder">
<input type="text" name="booking_id" value="123">
<input type="text" name="customer_id" value="1006">
<input type="text" name="order_amount" value="180.00">
<input type="text" name="order_status" value="1">
<input type="submit" value="PROCEED TO MAKE PAYMENT">
</form>
create order function
public function createOrder()
{
$product_id = 222;
$quantity = 1;
$args = array(
'variation' => array( 'attribute_color' => 'red'),
'status' => 'complete',
'customer_id'=> 23
);
$order = wc_create_order();
$order_id = trim(str_replace('#', '', $order->get_order_number()));
$order->add_product( get_product( $product_id ), $quantity, $args );
$order->set_status( $args['status'] );
$order->set_customer_id( is_numeric( $args['customer_id'] ) ? absint( $args['customer_id'] ) : 0 );
$order->set_total( ($discount['amount']/100) , 'order_discount'); // not pennies (use dollar amount)
update_post_meta( $order_id, '_payment_method', 'billplz' );
update_post_meta( $order_id, '_payment_method_title', 'Billplz Payment Gateway' );
$order->calculate_totals();
// Process Payment
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'billplz' ]->process_payment( $order_id );
// Redirect to success/confirmation/payment page
if ( $result['result'] == 'success' ) {
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
$return_url = $available_gateways[ 'billplz' ]->get_return_url( $order );
wp_redirect($return_url);
exit;
}
}