-
Samsung Galaxy Watch 4
R6,600.00R6,800.00 -
test
R1.00
public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); $logger = wc_get_logger(); $secret_key = 'your_secret_key'; // Replace with your actual secret key // Generate a unique request ID $requestId = uniqid('ecocash_', true); // Prepare the payment request payload $payment_payload = array( 'amount' => (string) $order->get_total(), 'msisdn' => $order->get_billing_phone(), 'vendor_code' => $this->vendor_code, 'api_key' => $this->api_key, 'requestId' => $requestId, 'checksum' => $this->generate_checksum(array( 'amount' => $payment_payload['amount'], 'msisdn' => $payment_payload['msisdn'], 'vendor_code' => $payment_payload['vendor_code'], 'api_key' => $payment_payload['api_key'], 'requestId' => $payment_payload['requestId'] ), $secret_key) ); // Log the payment request $logger->info('Ecocash Payment Request Payload: ' . json_encode($payment_payload), ['source' => 'ecocash']); // Send the payment request $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' )); // Handle payment response errors if ( is_wp_error( $payment_response ) ) { wc_add_notice( 'Payment request failed: ' . $payment_response->get_error_message(), 'error' ); $logger->error('Payment request failed: ' . $payment_response->get_error_message(), ['source' => 'ecocash']); return; } // Parse the payment response $payment_result = json_decode( wp_remote_retrieve_body( $payment_response ), true ); if (json_last_error() !== JSON_ERROR_NONE) { wc_add_notice('Failed to parse payment response.', 'error'); $logger->error('Failed to parse payment response: ' . json_last_error_msg(), ['source' => 'ecocash']); return; } // Log the payment response $logger->info('Ecocash Payment Response: ' . json_encode($payment_result), ['source' => 'ecocash']); // Check if the initial payment request was successful if ( isset( $payment_result['responseCode'] ) && $payment_result['responseCode'] == '200' ) { // Check for the poll URL and verify the transaction status $poll_url = $payment_result['pollurl'] ?? null; if ($poll_url) { $status_response = wp_remote_get($poll_url); if (is_wp_error($status_response)) { wc_add_notice('Transaction status check failed: ' . $status_response->get_error_message(), 'error'); $logger->error('Status check failed: ' . $status_response->get_error_message(), ['source' => 'ecocash']); return; } $status_result = json_decode(wp_remote_retrieve_body($status_response), true); if (json_last_error() !== JSON_ERROR_NONE) { wc_add_notice('Failed to parse status response.', 'error'); $logger->error('Failed to parse status response: ' . json_last_error_msg(), ['source' => 'ecocash']); return; } // Log the transaction status response $logger->info('Ecocash Transaction Status Response: ' . json_encode($status_result), ['source' => 'ecocash']); // Check if the transaction was successful if ($status_result['status'] === 'Paid') { $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 not confirmed. Please try again.', 'error' ); return; } } else { wc_add_notice( 'Transaction status URL not provided.', 'error' ); $logger->error('Transaction status URL not provided.', ['source' => 'ecocash']); return; } } else { wc_add_notice( 'Payment was not accepted. Please check your Ecocash details and try again.', 'error' ); $logger->warning('Payment rejected: ' . json_encode($payment_result), ['source' => 'ecocash']); return; } } /** * Generate a checksum for the payment request. * * @param array $data The data to include in the checksum. * @param string $secret_key The secret key to use for hashing. * @return string The generated checksum. */ private function generate_checksum($data, $secret_key) { ksort($data); // Sort data by key $query_string = http_build_query($data); return hash_hmac('sha256', $query_string, $secret_key); }