public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
$requestId = 'Ecomart' . uniqid();
// Payment API request
$payment_payload = array(
'msisdn' => $order->get_billing_phone(),
'merchantNumber'=> $this->merchant_id,
'merchCode' => $this->merchant_id,
'amount' => $order->get_total(),
'requestId' => $requestId,
'vendor_code' => $this->vendor_code,
'api_key' => $this->api_key,
'checksum' => 'string', // replace if needed
'callbackurl' => $this->callback_url,
);
$logger = wc_get_logger();
$logger->info( 'Sending Ecocash Payment Request: ' . json_encode( $payment_payload ), [ 'source' => 'ecocash' ] );
$payment_response = wp_remote_post( 'https://etlapp.etl.co.ls/prodpinfulpaymerchant/payment/', array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => json_encode( $payment_payload ),
'method' => 'POST',
'data_format' => 'body'
));
if ( is_wp_error( $payment_response ) ) {
wc_add_notice( 'Payment gateway error: ' . $payment_response->get_error_message(), 'error' );
return;
}
$payment_result = json_decode( wp_remote_retrieve_body( $payment_response ), true );
$logger->info( 'Ecocash Payment Response: ' . json_encode( $payment_result ), [ 'source' => 'ecocash' ] );
// If payment was accepted by gateway, then check transaction status
if ( isset( $payment_result['responseCode'] ) && $payment_result['responseCode'] == '200' ) {
sleep(5); // wait a few seconds before checking status
$status_payload = array(
'transactionId' => $requestId,
'vendor_code' => $this->vendor_code,
'api_key' => $this->api_key,
'checksum' => 'string', // replace if needed
);
$status_response = wp_remote_post( 'https://etlapp.etl.co.ls/prodpinfulpaymerchant/transactionstatus/', array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => json_encode( $status_payload ),
'method' => 'POST',
'data_format' => 'body'
));
if ( is_wp_error( $status_response ) ) {
wc_add_notice( 'Transaction status check failed: ' . $status_response->get_error_message(), 'error' );
return;
}
$status_result = json_decode( wp_remote_retrieve_body( $status_response ), true );
$logger->info( 'Ecocash Transaction Status Response: ' . json_encode( $status_result ), [ 'source' => 'ecocash' ] );
if ( isset( $status_result['txnstatus'] ) && $status_result['txnstatus'] == '200' ) {
$order->payment_complete();
$order->add_order_note( 'Ecocash payment successful. Txn ID: ' . $requestId );
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
);
} else {
$order->update_status( 'failed', 'Ecocash transaction failed or was not successful.' );
wc_add_notice( 'Transaction failed or was not confirmed.', 'error' );
return;
}
} else {
wc_add_notice( 'Payment was not accepted. Please try again.', 'error' );
return;
}
}
3D – ecomart
-
-

-

-
-

-
-
-

-
-

-
-
