From ab1900ef0b8c03e299ae9c75a490948358cd7162 Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 16 Mar 2022 11:05:13 +0530 Subject: [PATCH] Initial commit --- lib/AbstractSslCommerz.php | 132 ++++++++++++++ lib/SslCommerzInterface.php | 23 +++ lib/SslCommerzNotification.php | 313 +++++++++++++++++++++++++++++++++ sslcommerz.php | 143 +++++++++++++++ sslcommerz.png | Bin 0 -> 4791 bytes utils.php | 54 ++++++ vikbooking/sslcommerz.php | 97 ++++++++++ viksslcommerz.php | 290 ++++++++++++++++++++++++++++++ 8 files changed, 1052 insertions(+) create mode 100644 lib/AbstractSslCommerz.php create mode 100644 lib/SslCommerzInterface.php create mode 100644 lib/SslCommerzNotification.php create mode 100644 sslcommerz.php create mode 100644 sslcommerz.png create mode 100644 utils.php create mode 100644 vikbooking/sslcommerz.php create mode 100644 viksslcommerz.php diff --git a/lib/AbstractSslCommerz.php b/lib/AbstractSslCommerz.php new file mode 100644 index 0000000..3e0df53 --- /dev/null +++ b/lib/AbstractSslCommerz.php @@ -0,0 +1,132 @@ +storeId = $storeID; + } + + protected function getStoreId() + { + return $this->storeId; + } + + protected function setStorePassword($storePassword) + { + $this->storePassword = $storePassword; + } + + protected function getStorePassword() + { + return $this->storePassword; + } + + protected function setApiUrl($url) + { + $this->apiUrl = $url; + } + + protected function getApiUrl() + { + return $this->apiUrl; + } + + + protected function setApiDomain($url) + { + $this->apiDomain = $url; + } + + protected function getApiDomain() + { + return $this->apiDomain; + } + + /** + * @param $data + * @param array $header + * @param bool $setLocalhost + * @return bool|string + */ + public function callToApi($data, $header = [], $setLocalhost = false) + { + $curl = curl_init(); + + if (!$setLocalhost) { + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // The default value for this option is 2. It means, it has to have the same name in the certificate as is in the URL you operate against. + } else { + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // When the verify value is 0, the connection succeeds regardless of the names in the certificate. + } + + curl_setopt($curl, CURLOPT_URL, $this->getApiUrl()); + curl_setopt($curl, CURLOPT_HEADER, 0); + curl_setopt($curl, CURLOPT_HTTPHEADER, $header); + curl_setopt($curl, CURLOPT_TIMEOUT, 60); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + + $response = curl_exec($curl); + $err = curl_error($curl); + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $curlErrorNo = curl_errno($curl); + curl_close($curl); + + if ($code == 200 & !($curlErrorNo)) { + return $response; + } else { + return "FAILED TO CONNECT WITH SSLCOMMERZ API"; + //return "cURL Error #:" . $err; + } + } + + /** + * @param $response + * @param string $type + * @param string $pattern + * @return false|mixed|string + */ + public function formatResponse($response, $type = 'checkout', $pattern = 'json') + { + $sslcz = json_decode($response, true); + + if ($type != 'checkout') { + return $sslcz; + } else { + if (isset($sslcz['GatewayPageURL']) && $sslcz['GatewayPageURL'] != "") { + // this is important to show the popup, return or echo to send json response back + $response = json_encode(['status' => 'success', 'data' => $sslcz['GatewayPageURL'], 'logo' => $sslcz['storeLogo']]); + } else { + $response = json_encode(['status' => 'fail', 'data' => null, 'message' => "JSON Data parsing error!"]); + } + + if ($pattern == 'json') { + return $response; + } else { + echo $response; + } + } + } + + /** + * @param $url + * @param bool $permanent + */ + public function redirect($url, $permanent = false) + { + header('Location: ' . $url, true, $permanent ? 301 : 302); + + exit(); + } +} diff --git a/lib/SslCommerzInterface.php b/lib/SslCommerzInterface.php new file mode 100644 index 0000000..f874ddf --- /dev/null +++ b/lib/SslCommerzInterface.php @@ -0,0 +1,23 @@ +setStoreId( $store_id ); + $this->setStorePassword( $store_password ); + $this->setApiDomain( $api_domain ); + } + + public function orderValidate( $trx_id = '', $amount = 0, $currency = "BDT", $post_data ): bool|string { + if ( $post_data == '' && $trx_id == '' && ! is_array( $post_data ) ) { + $this->error = "Please provide valid transaction ID and post request data"; + + return $this->error; + } + $validation = $this->validate( $trx_id, $amount, $currency, $post_data ); + if ( $validation ) { + return true; + } + + return false; + } + + + # VALIDATE SSLCOMMERZ TRANSACTION + protected function validate( $merchant_trans_id, $merchant_trans_amount, $merchant_trans_currency, $post_data ): bool { + if ( $merchant_trans_id != "" && $merchant_trans_amount != 0 ) { + $post_data['store_id'] = $this->getStoreId(); + $post_data['store_pass'] = $this->getStorePassword(); + if ( $this->SSLCOMMERZ_hash_varify( $this->getStorePassword(), $post_data ) ) { + $val_id = urlencode( $post_data['val_id'] ); + $store_id = urlencode( $this->getStoreId() ); + $store_passwd = urlencode( $this->getStorePassword() ); + $requested_url = ( $this->getApiDomain() . '/validator/api/validationserverAPI.php' . "?val_id=" . $val_id . "&store_id=" . $store_id . "&store_passwd=" . $store_passwd . "&v=1&format=json" ); + $handle = curl_init(); + curl_setopt( $handle, CURLOPT_URL, $requested_url ); + curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); + if ( $post_data['connect_from_localhost'] ) { + curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, false ); + curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false ); + } else { + curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, true ); + curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, true ); + } + + $result = curl_exec( $handle ); + $code = curl_getinfo( $handle, CURLINFO_HTTP_CODE ); + if ( $code == 200 && ! ( curl_errno( $handle ) ) ) { + $result = json_decode( $result ); + $this->sslc_data = $result; + # TRANSACTION INFO + $status = $result->status; + $tran_date = $result->tran_date; + $tran_id = $result->tran_id; + $val_id = $result->val_id; + $amount = $result->amount; + $store_amount = $result->store_amount; + $bank_tran_id = $result->bank_tran_id; + $card_type = $result->card_type; + $currency_type = $result->currency_type; + $currency_amount = $result->currency_amount; + + # ISSUER INFO + $card_no = $result->card_no; + $card_issuer = $result->card_issuer; + $card_brand = $result->card_brand; + $card_issuer_country = $result->card_issuer_country; + $card_issuer_country_code = $result->card_issuer_country_code; + + # API AUTHENTICATION + $APIConnect = $result->APIConnect; + $validated_on = $result->validated_on; + $gw_version = $result->gw_version; + + # GIVE SERVICE + if ( $status == "VALID" || $status == "VALIDATED" ) { + if ( $merchant_trans_currency == "BDT" ) { + if ( trim( $merchant_trans_id ) == trim( $tran_id ) && ( abs( $merchant_trans_amount - $amount ) < 1 ) && trim( $merchant_trans_currency ) == trim( 'BDT' ) ) { + return true; + } + + $this->error = "Data has been tempered"; + + return false; + } + + if ( trim( $merchant_trans_id ) == trim( $tran_id ) && ( abs( $merchant_trans_amount - $currency_amount ) < 1 ) && trim( $merchant_trans_currency ) == trim( $currency_type ) ) { + return true; + } + $this->error = "Data has been tempered"; + + return false; + } + + $this->error = "Failed Transaction"; + + return false; + } + + $this->error = "Failed to connect with SSLCOMMERZ"; + + return false; + } + + $this->error = "Hash validation failed"; + + return false; + } + + $this->error = "Invalid data"; + + return false; + } + + protected function SSLCOMMERZ_hash_varify( $store_passwd = "", $post_data ) { + + if ( isset( $post_data, $post_data['verify_sign'], $post_data['verify_key'] ) ) { + $pre_define_key = explode( ',', $post_data['verify_key'] ); + $new_data = array(); + if ( ! empty( $pre_define_key ) ) { + foreach ( $pre_define_key as $value ) { + if ( isset( $post_data[ $value ] ) ) { + $new_data[ $value ] = ( $post_data[ $value ] ); + } + } + } + $new_data['store_passwd'] = md5( $store_passwd ); + ksort( $new_data ); + $hash_string = ""; + foreach ( $new_data as $key => $value ) { + $hash_string .= $key . '=' . ( $value ) . '&'; + } + $hash_string = rtrim( $hash_string, '&' ); + if ( md5( $hash_string ) == $post_data['verify_sign'] ) { + return true; + } + $this->error = "Verification signature not matched"; + + return false; + } + $this->error = 'Required data mission. ex: verify_key, verify_sign'; + + return false; + } + + public function makePayment( array $data, $type = 'checkout', $pattern = 'json' ) { + if ( empty( $data ) ) { + return "Please provide a valid information list about transaction with transaction id, amount, success url, fail url, cancel url, store id and pass at least"; + } + $header = []; + $this->setApiUrl( $this->getApiDomain() . '/gwprocess/v4/api.php' ); + $this->setParams( $data ); + $this->setAuthenticationInfo(); + $response = $this->callToApi( $this->data, $header, true ); + $formattedResponse = $this->formatResponse( $response, $type, $pattern ); + if ( $type === 'hosted' ) { + if ( isset( $formattedResponse['GatewayPageURL'] ) && $formattedResponse['GatewayPageURL'] != '' ) { + $this->redirect( $formattedResponse['GatewayPageURL'] ); + } else { + return $formattedResponse['failedreason']; + } + } else { + return $formattedResponse; + } + } + + public function setParams( $data ) { + $this->setRequiredInfo( $data ); + $this->setCustomerInfo( $data ); + $this->setShipmentInfo( $data ); + $this->setProductInfo( $data ); + $this->setAdditionalInfo( $data ); + } + + public function setRequiredInfo( array $data ): array { + $this->data['total_amount'] = $data['total_amount']; + $this->data['currency'] = $data['currency']; + $this->data['tran_id'] = $data['tran_id']; + $this->data['product_category'] = $data['product_category']; + $this->setSuccessUrl( $data['success_url'] ); + $this->setFailedUrl( $data['fail_url'] ); + $this->setCancelUrl( $data['fail_url'] ); + if ( isset( $data['ipn_url'] ) ) { + $this->setIpnUrl( $data['cancel_url'] ); + } + $this->data['success_url'] = $this->getSuccessUrl(); + $this->data['fail_url'] = $this->getFailedUrl(); + $this->data['cancel_url'] = $this->getCancelUrl(); + $this->data['ipn_url'] = $this->getIpnUrl(); + $this->data['multi_card_name'] = $data['multi_card_name'] ?? null; + $this->data['allowed_bin'] = $data['allowed_bin'] ?? null; + $this->data['emi_option'] = $data['emi_option'] ?? null; + $this->data['emi_max_inst_option'] = $data['emi_max_inst_option'] ?? null; + $this->data['emi_selected_inst'] = $data['emi_selected_inst'] ?? null; + + return $this->data; + } + + protected function getSuccessUrl() { + return $this->successUrl; + } + + protected function setSuccessUrl( $url ) { + $this->successUrl = $url; + } + + protected function getFailedUrl() { + return $this->failedUrl; + } + + protected function setFailedUrl( $url ) { + $this->failedUrl = $url; + } + + protected function getCancelUrl() { + return $this->cancelUrl; + } + + protected function setCancelUrl( $url ) { + $this->cancelUrl = $url; + } + + protected function getIpnUrl() { + return $this->ipnUrl; + } + + protected function setIpnUrl( $url ) { + $this->ipnUrl = $url; + } + + public function setCustomerInfo( array $data ): array { + $this->data['cus_name'] = $data['cus_name']; + $this->data['cus_email'] = $data['cus_email']; + $this->data['cus_add1'] = $data['cus_add1']; + $this->data['cus_add2'] = $data['cus_add2'] ?? null; + $this->data['cus_city'] = $data['cus_city'] ?? null; + $this->data['cus_state'] = $data['cus_state'] ?? null; + $this->data['cus_postcode'] = $data['cus_postcode'] ?? ''; + $this->data['cus_country'] = $data['cus_country']; + $this->data['cus_phone'] = $data['cus_phone'] ?? ''; + $this->data['cus_fax'] = $data['cus_fax'] ?? null; + + return $this->data; + } + + public function setShipmentInfo( array $data ): array { + $this->data['shipping_method'] = $data['shipping_method']; + $this->data['num_of_item'] = $data['num_of_item']; + $this->data['ship_name'] = $data['ship_name'] ?? null; + $this->data['ship_add1'] = $data['ship_add1'] ?? null; + $this->data['ship_add2'] = $data['ship_add2'] ?? null; + $this->data['ship_city'] = $data['ship_city'] ?? null; + $this->data['ship_state'] = $data['ship_state'] ?? null; + $this->data['ship_postcode'] = $data['ship_postcode'] ?? null; + $this->data['ship_country'] = $data['ship_country'] ?? null; + + return $this->data; + } + + public function setProductInfo( array $data ): array { + $this->data['product_name'] = $data['product_name'] ?? ''; + $this->data['product_category'] = $data['product_category'] ?? ''; + $this->data['product_profile'] = $data['product_profile'] ?? ''; + $this->data['hours_till_departure'] = $data['hours_till_departure'] ?? null; + $this->data['flight_type'] = $data['flight_type'] ?? null; + $this->data['pnr'] = $data['pnr'] ?? null; + $this->data['journey_from_to'] = $data['journey_from_to'] ?? null; + $this->data['third_party_booking'] = $data['third_party_booking'] ?? null; + $this->data['hotel_name'] = $data['hotel_name'] ?? null; + $this->data['length_of_stay'] = $data['length_of_stay'] ?? null; + $this->data['check_in_time'] = $data['check_in_time'] ?? null; + $this->data['hotel_city'] = $data['hotel_city'] ?? null; + $this->data['product_type'] = $data['product_type'] ?? null; + $this->data['topup_number'] = $data['topup_number'] ?? null; + $this->data['country_topup'] = $data['country_topup'] ?? null; + $this->data['cart'] = $data['cart'] ?? null; + $this->data['product_amount'] = $data['product_amount'] ?? null; + $this->data['vat'] = $data['vat'] ?? null; + $this->data['discount_amount'] = $data['discount_amount'] ?? null; + $this->data['convenience_fee'] = $data['convenience_fee'] ?? null; + + return $this->data; + } + + public function setAdditionalInfo( array $data ): array { + $this->data['value_a'] = $data['value_a'] ?? null; + $this->data['value_b'] = $data['value_b'] ?? null; + $this->data['value_c'] = $data['value_c'] ?? null; + $this->data['value_d'] = $data['value_d'] ?? null; + + return $this->data; + } + + public function setAuthenticationInfo(): array { + $this->data['store_id'] = $this->getStoreId(); + $this->data['store_passwd'] = $this->getStorePassword(); + + return $this->data; + } +} diff --git a/sslcommerz.php b/sslcommerz.php new file mode 100644 index 0000000..90ef3aa --- /dev/null +++ b/sslcommerz.php @@ -0,0 +1,143 @@ + [ + 'label' => __( '', 'vikbooking' ), + 'type' => 'custom', + 'html' => '' + ], + 'sandbox' => [ + 'label' => __( 'Test Mode', 'vikbooking' ), + 'type' => 'select', + 'options' => [ 'Yes', 'No' ], + ], + 'store_id' => [ + 'label' => __( 'Store ID', 'vikbooking' ), + 'type' => 'text', + ], + 'store_passwd' => [ + 'label' => __( 'Store Password', 'vikbooking' ), + 'type' => 'text', + ], + ]; + } + + protected function doRefund( JPaymentStatus &$status ) { + parent::doRefund( $status ); // TODO: Change the autogenerated stub + } + + protected function beginTransaction() { + + $sslcommerz_url = $this->getParam( 'sandbox' ) === 'Yes' ? "https://sandbox.sslcommerz.com" : "https://securepay.sslcommerz.com"; + $details = $this->get( 'details' ); + $customer_values = $this->prepareCustomerData( $details['custdata'] ); + $post_data = []; +//Integration Required Parameters + $post_data['store_id'] = $this->getParam( 'store_id' ); + $post_data['store_passwd'] = $this->getParam( 'store_passwd' ); + $post_data['total_amount'] = $this->get( 'total_to_pay' ); + $post_data['currency'] = $this->get( 'transaction_currency' ); + $post_data['tran_id'] = $this->get( 'sid' ) . "-" . $this->get( 'ts' ); + $post_data['success_url'] = JUri::getInstance( $this->get( 'notify_url' ) ); + $post_data['fail_url'] = JUri::getInstance( $this->get( 'notify_url' ) ); + $post_data['cancel_url'] = JUri::getInstance( $this->get( 'notify_url' ) ); +//Customer Information + $post_data['cus_name'] = $customer_values['Name'] . ' ' . $customer_values['Last Name']; + $post_data['cus_email'] = $this->get( 'customer_email' ); + $post_data['cus_add1'] = $customer_values['Address']; + $post_data['cus_city'] = $customer_values['City']; + $post_data['cus_country'] = $customer_values['Country']; + $post_data['cus_phone'] = $customer_values['Phone'] ?? ''; + $post_data['cus_postcode'] = $customer_values['Zip Code'] ?? ''; +//Shipment Information + $post_data['shipping_method'] = 'No'; + $post_data['num_of_item'] = $details['roomsnum']; +//Product Information + $post_data['product_name'] = $this->order->rooms_name; + $post_data['product_category'] = 'ecommerce'; + $post_data['product_profile'] = 'general'; + $sslCommerzN = new SslCommerzNotification( $post_data['store_id'], $post_data['store_passwd'], $sslcommerz_url ); + $sslCommerzN->makePayment( $post_data, 'hosted' ); + } + + private function prepareCustomerData( $customer_data ): array { + $customer_data_parts = explode( "\n", $customer_data ); + $customer_values = array(); + if ( str_contains( $customer_data_parts[0], ':' ) && str_contains( $customer_data_parts[1], ':' ) ) { + foreach ( $customer_data_parts as $custdet ) { + if ( $custdet === '' ) { + continue; + } + $customer_det_parts = explode( ':', $custdet ); + if ( count( $customer_det_parts ) >= 2 ) { + $key = $customer_det_parts[0]; + unset( $customer_det_parts[0] ); + $customer_values[ $key ] = trim( implode( ':', $customer_det_parts ) ); + } + } + } + + return $customer_values; + } + + protected function validateTransaction( JPaymentStatus &$status ): bool { + $sslcommerz_url = $this->getParam( 'sandbox' ) === 'Yes' ? "https://sandbox.sslcommerz.com" : "https://securepay.sslcommerz.com"; + $sslv = new SslCommerzNotification( $this->getParam( 'store_id' ), $this->getParam( 'store_passwd' ), $sslcommerz_url ); + $tran_id = $_POST['tran_id']; + $bank_tran_id = $_POST['bank_tran_id']; + $amount = $_POST['amount']; + $currency = $_POST['currency']; + $_POST['connect_from_localhost'] = false; + if ($_POST['status']=='VALID'){ + $validated = $sslv->orderValidate( $tran_id, $amount, $currency, $_POST ); + if ( $validated ) { + $status->verified(); + $status->paid( $amount ); + $status->setData( 'TransactionId', $tran_id ); + $status->setData( 'merchantOrderId', $bank_tran_id ); + $status->appendLog( "Successful payment" ); + } else { + $status->setData( 'TransactionId', $tran_id ); + $status->setData( 'merchantOrderId', $bank_tran_id ); + $status->appendLog( "Failure payment" ); + $status->appendLog( ( "Failure Reason " . ( isset( $_POST['failedreason'] ) ) ) ? $_POST['failedreason'] : $_POST['error'] ); + } + } + $status->appendLog( 'STATUS:- ' . $_POST['status'] ); + $status->appendLog( "Transaction ID:- " . $tran_id ); + $status->appendLog( "Transaction Time:- " . $_POST['tran_date'] ); + $status->appendLog( "Payment Method:- " . $_POST['card_issuer'] ); + $status->appendLog( "Bank Transaction ID:- " . $_POST['bank_tran_id'] ); + $status->appendLog( "Amount:- " . $_POST['amount'] . ' ' . $_POST['currency'] ); + + return true; + } + + protected function complete( $res = 0 ) { + $app = JFactory::getApplication(); + if ( $res ) { + $url = $this->get( 'return_url' ); + $app->enqueueMessage( __( 'Thank you! Payment successfully received.', 'viksslcommerz' ) ); + } else { + $url = $this->get( 'error_url' ); + $app->enqueueMessage( __( 'It was not possible to verify the payment. Please, try again.', 'viksslcommerz' ) ); + } + JFactory::getApplication()->redirect( $url ); + exit; + } +} + + diff --git a/sslcommerz.png b/sslcommerz.png new file mode 100644 index 0000000000000000000000000000000000000000..00931d80ea2e33e361064b3e46cd00fab21d1a93 GIT binary patch literal 4791 zcmbVQ2{@E%-+zWIsW23dql{5UVpjVg+bGJ&iL%QuGYn%!Gh^Q-Qlk<&g|dZ;vX-(I zS|m#+F(=uTB|Bx!Qr|<%b>8oLzxVyVcdl#Zxu5&~-T&?W|9|)Yniwm~gWE)8MF0TU zW^RT*3ILE=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 +1,54 @@ +isDriver( 'sslcommerz' ) ) { + return; + } + if ( $payment->get( 'leave_deposit' ) ) { + $html = '

+ ' . JText::_( 'VBLEAVEDEPOSIT' ) . '' . + $payment->get( 'currency_symb' ) . ' ' . number_format( $payment->get( 'total_to_pay' ), 2 ) . + '


' . $html; + } + $was_using_cache = wp_using_ext_object_cache( false ); + $transient = set_transient( 'viksslcommerz_vikbooking_' . $payment->get( 'oid' ) . '_' . $payment->get( 'sid' ), $payment->get( 'total_to_pay' ), 10 * MINUTE_IN_SECONDS ); + wp_using_ext_object_cache( $was_using_cache ); + if ( ! $transient ) { + $txname = $payment->get( 'sid' ) . '-' . $payment->get( 'oid' ) . '.tx'; + $fp = fopen( VIKSSLCOMMERZ_DIR . DIRECTORY_SEPARATOR . 'VikSslcommerz' . DIRECTORY_SEPARATOR . $txname, 'w+' ); + fwrite( $fp, $payment->get( 'total_to_pay' ) ); + fclose( $fp ); + } +}, 10, 2 ); + +add_action( 'payment_before_validate_transaction_vikbooking', function ( $payment ) { + if ( ! $payment->isDriver( 'sslcommerz' ) ) { + return; + } + $txname = $payment->get( 'sid' ) . '-' . $payment->get( 'oid' ) . '.tx'; + $txdata = ''; + $path = VIKSSLCOMMERZ_DIR . DIRECTORY_SEPARATOR . 'Sslcommerz' . DIRECTORY_SEPARATOR . $txname; + $was_using_cache = wp_using_ext_object_cache( false ); + $transient = 'viksslcommerz_vikbooking_' . $payment->get( 'oid' ) . '_' . $payment->get( 'sid' ); + $data = get_transient( $transient ); + if ( $data ) { + $payment->set( 'total_to_pay', $data ); + delete_transient( $transient ); + wp_using_ext_object_cache( $was_using_cache ); + } else if ( is_file( $path ) ) { + $fp = fopen( $path, 'rb' ); + $txdata = fread( $fp, filesize( $path ) ); + fclose( $fp ); + if ( ! empty( $txdata ) ) { + $payment->set( 'total_to_pay', $txdata ); + } else { + $payment->set( 'total_to_pay', $payment->get( 'total_to_pay', 0 ) ); + } + unlink( $path ); + } +} ); + +add_action( 'payment_on_after_validation_vikbooking', function ( &$payment, $res ) { + if ( ! $payment->isDriver( 'sslcommerz' ) ) { + return; + } + $url = 'index.php?option=com_vikbooking&task=vieworder&sid=' . $payment->get( 'sid' ) . '&ts=' . $payment->get( 'ts' ); + $model = JModel::getInstance( 'vikbooking', 'shortcodes', 'admin' ); + $item_id = $model->all( 'post_id' ); + if ( count( $item_id ) ) { + $url = JRoute::_( $url . '&Itemid=' . $item_id[0]->post_id, false ); + } + JFactory::getApplication()->redirect( $url ); + exit; +}, 10, 2 ); + +class VikBookingSslCommerzPayment extends AbstractSslCommerzPayment { + public function __construct( $alias, $order, $params = array() ) { + parent::__construct( $alias, $order, $params ); + $details = $this->get( 'details', array() ); + $this->set( 'oid', $this->get( 'id', null ) ); + if ( ! $this->get( 'oid' ) ) { + $this->set( 'oid', $details['id'] ?? 0 ); + } + + if ( ! $this->get( 'sid' ) ) { + $this->set( 'sid', $details['sid'] ?? 0 ); + } + + if ( ! $this->get( 'ts' ) ) { + $this->set( 'ts', $details['ts'] ?? 0 ); + } + + if ( ! $this->get( 'custmail' ) ) { + $this->set( 'custmail', $details['custmail'] ?? '' ); + } + } +} diff --git a/viksslcommerz.php b/viksslcommerz.php new file mode 100644 index 0000000..58c34ae --- /dev/null +++ b/viksslcommerz.php @@ -0,0 +1,290 @@ +load( 'viksslcommerz', VIKSSLCOMMERZ_LANG ); +} ); + +/** + * Returns the current plugin version for being used by + * VikUpdater, in order to check for new updates. + * + * @return string The plugin version. + */ +add_filter( 'vikwp_vikupdater_sslcommerz_version', function () { + return VIKSSLCOMMERZVERSION; +} ); + +/** + * Returns the base path of this plugin for being used by + * VikUpdater, in order to move the files of a newer version. + * + * @return string The plugin base path. + */ +add_filter( 'vikwp_vikupdater_sslcommerz_path', function () { + return plugin_dir_path( __FILE__ ) . ".."; +} ); + +/** + * VIKRESTAURANTS HOOKS + */ + +/** + * Pushes the sslcommerz gateway within the supported payments of VikRentCar plugin. + * + * @param array $drivers The current list of supported drivers. + * + * @return array The updated drivers list. + */ +add_filter( 'get_supported_payments_vikrestaurants', function ( $drivers ) { + $driver = viksslcommerz_get_payment_path( 'vikrestaurants' ); + + // make sure the driver exists + if ( $driver ) { + $drivers[] = $driver; + } + + return $drivers; +} ); + +/** + * Loads sslcommerz payment handler when dispatched by Vikrestaurants. + * + * @param array &$drivers A list of payment classnames. + * @param string $payment The name of the invoked payment. + * + * @return void + * + * @see JPayment + */ +add_action( 'load_payment_gateway_vikrestaurants', function ( &$drivers, $payment ) { + // make sure the classname hasn't been generated yet by a different hook + // and the request payment matches 'kashier' string + if ( $payment == 'sslcommerz' ) { + $classname = viksslcommerz_load_payment( 'vikrestaurants' ); + + if ( $classname ) { + $drivers[] = $classname; + } + } +}, 10, 2 ); + +/** + * VIKRENTITEMS HOOKS + */ + +/** + * Pushes the sslcommerz gateway within the supported payments of VikRentCar plugin. + * + * @param array $drivers The current list of supported drivers. + * + * @return array The updated drivers list. + */ +add_filter( 'get_supported_payments_vikrentitems', function ( $drivers ) { + $driver = viksslcommerz_get_payment_path( 'vikrentitems' ); + + // make sure the driver exists + if ( $driver ) { + $drivers[] = $driver; + } + + return $drivers; +} ); + +/** + * Loads sslcommerz payment handler when dispatched by vikrentitems. + * + * @param array &$drivers A list of payment classnames. + * @param string $payment The name of the invoked payment. + * + * @return void + * + * @see JPayment + */ +add_action( 'load_payment_gateway_vikrentitems', function ( &$drivers, $payment ) { + // make sure the classname hasn't been generated yet by a different hook + // and the request payment matches 'sslcommerz' string + if ( $payment == 'sslcommerz' ) { + $classname = viksslcommerz_load_payment( 'vikrentitems' ); + + if ( $classname ) { + $drivers[] = $classname; + } + } +}, 10, 2 ); + +/** + * VIKRENTCAR HOOKS + */ + +/** + * Pushes the sslcommerz gateway within the supported payments of VikRentCar plugin. + * + * @param array $drivers The current list of supported drivers. + * + * @return array The updated drivers list. + */ +add_filter( 'get_supported_payments_vikrentcar', function ( $drivers ) { + $driver = viksslcommerz_get_payment_path( 'vikrentcar' ); + + // make sure the driver exists + if ( $driver ) { + $drivers[] = $driver; + } + + return $drivers; +} ); + +/** + * Loads sslcommerz payment handler when dispatched by Vikrentcar. + * + * @param array &$drivers A list of payment classnames. + * @param string $payment The name of the invoked payment. + * + * @return void + * + * @see JPayment + */ +add_action( 'load_payment_gateway_vikrentcar', function ( &$drivers, $payment ) { + // make sure the classname hasn't been generated yet by a different hook + // and the request payment matches 'sslcommerz' string + if ( $payment == 'sslcommerz' ) { + $classname = viksslcommerz_load_payment( 'vikrentcar' ); + + if ( $classname ) { + $drivers[] = $classname; + } + } +}, 10, 2 ); + +/** + * VIKAPPOINTMENTS HOOKS + */ + +/** + * Pushes the sslcommerz gateway within the supported payments of VikAppointments plugin. + * + * @param array $drivers The current list of supported drivers. + * + * @return array The updated drivers list. + */ +add_filter( 'get_supported_payments_vikappointments', function ( $drivers ) { + $driver = viksslcommerz_get_payment_path( 'vikappointments' ); + + // make sure the driver exists + if ( $driver ) { + $drivers[] = $driver; + } + + return $drivers; +} ); + +/** + * Loads sslcommerz payment handler when dispatched by VikAppointments. + * + * @param array &$drivers A list of payment classnames. + * @param string $payment The name of the invoked payment. + * + * @return void + * + * @see JPayment + */ +add_action( 'load_payment_gateway_vikappointments', function ( &$drivers, $payment ) { + // make sure the classname hasn't been generated yet by a different hook + // and the request payment matches 'sslcommerz' string + if ( $payment == 'sslcommerz' ) { + $classname = viksslcommerz_load_payment( 'vikappointments' ); + + if ( $classname ) { + $drivers[] = $classname; + } + } +}, 10, 2 ); + +/** + * VIKBOOKING HOOKS + */ + +/** + * Pushes the sslcommerz gateway within the supported payments of VikBooking plugin. + * + * @param array $drivers The current list of supported drivers. + * + * @return array The updated drivers list. + */ +add_filter( 'get_supported_payments_vikbooking', function ( $drivers ) { + $driver = viksslcommerz_get_payment_path( 'vikbooking' ); + + // make sure the driver exists + if ( $driver ) { + $drivers[] = $driver; + } + + return $drivers; +} ); + +/** + * Loads sslcommerz payment handler when dispatched by VikBooking. + * + * @param array &$drivers A list of payment instances. + * @param string $payment The name of the invoked payment. + * + * @return void + * + * @see JPayment + */ +add_action( 'load_payment_gateway_vikbooking', function ( &$drivers, $payment ) { + // make sure the classname hasn't been generated yet by a different hook + // and the request payment matches 'sslcommerz' string + if ( $payment == 'sslcommerz' ) { + $classname = viksslcommerz_load_payment( 'vikbooking' ); + + if ( $classname ) { + $drivers[] = $classname; + } + } +}, 10, 2 ); + +/** + * Filters the array containing the logo details to let VikBooking + * retrieves the correct image. + * + * In order to change the image logo, it is needed to inject the + * image path and URI within the $logo argument. + * + * @param array $logo An array containing the following information: + * - name The payment name; + * - path The payment logo base path; + * - uri The payment logo base URI. + * + * @return array The updated logo information. + */ +add_filter( 'vikbooking_oconfirm_payment_logo', function ( $logo ) { + if ( $logo['name'] == 'sslcommerz' ) { + $logo['path'] = VIKKASHIER_DIR . DIRECTORY_SEPARATOR . 'vikbooking' . DIRECTORY_SEPARATOR . 'sslcommerz.png'; + $logo['uri'] = VIKKASHIER_URI . 'vikbooking/sslcommerz.png'; + } + + return $logo; +} );