diff options
Diffstat (limited to 'src/GNU-taler-payment/class-wc-gnutaler-gateway.php')
-rw-r--r-- | src/GNU-taler-payment/class-wc-gnutaler-gateway.php | 107 |
1 files changed, 78 insertions, 29 deletions
diff --git a/src/GNU-taler-payment/class-wc-gnutaler-gateway.php b/src/GNU-taler-payment/class-wc-gnutaler-gateway.php index 6eec471..3160b46 100644 --- a/src/GNU-taler-payment/class-wc-gnutaler-gateway.php +++ b/src/GNU-taler-payment/class-wc-gnutaler-gateway.php @@ -48,7 +48,7 @@ if ( ! defined( 'ABSPATH' ) ) { * * @param $gateways - Array of all the payment gateways. * @return array - * @since 1.0 + * @since 0.6.0 */ @@ -79,7 +79,7 @@ function gnutaler_init_gateway_class() * * Handles the payments from the Woocommerce Webshop and sends the transactions to the GNU Taler Backend and the GNU Taler Wallet. * - * @since 1.0 + * @since 0.6.0 */ class WC_GNUTaler_Gateway extends WC_Payment_Gateway { @@ -109,6 +109,7 @@ function gnutaler_init_gateway_class() $this->GNU_Taler_Backend_URL = $this->get_option( 'GNU_Taler_Backend_URL' ); $this->GNU_Taler_Backend_API_Key = $this->get_option( 'GNU_Taler_Backend_API_Key' ); $this->Fulfillment_url = $this->get_option( 'Fulfillment_url' ); + $this->Payment_url = $this->get_option( 'Payment_url' ); $this->Order_text = $this->get_option( 'Order_text' ); $this->merchant_information = $this->get_option( 'merchant_information' ); $this->merchant_name = $this->get_option( 'merchant_name' ); @@ -169,6 +170,12 @@ function gnutaler_init_gateway_class() 'description' => 'Set the URL where the customer should return after finishing the payment process.', 'default' => get_home_url(), ), + 'Payment_url' => array( + 'title' => 'GNU Taler Payment url', + 'type' => 'text', + 'description' => 'Set the URL for your GNU Taler Wallet where the Coins of the costumer will be send to.', + 'default' => '', + ), 'Order_text' => array( 'title' => 'Summarytext of the order', 'type' => 'text', @@ -202,7 +209,7 @@ function gnutaler_init_gateway_class() * @param $body - The content of the request. * @param $purpose - What return value is to be expected. * @return array The return array will either have the successful return value or a detailed error message. - * @since 1.0 + * @since 0.6.0 * */ public function call_api( $method, $backend_url, $body, $purpose ): array @@ -231,7 +238,7 @@ function gnutaler_init_gateway_class() * @param $curl - Created curl request for error handling. * @param $result - The response from the backend, that will be returned if the request was successful * @return array - Array with a boolean, a http code and a message will be returned - * @since 1.0 + * @since 0.6.0 * */ public function curl_error_handling( $curl, $result ): array @@ -350,7 +357,7 @@ function gnutaler_init_gateway_class() * @param $body - Content of the request. * @param $url - URL where the request will be send * @return false|resource - Either the configured curl request will be returned or false if an error appears. - * @since 1.0 + * @since 0.6.0 */ public function curl_init( $method, $body, $url ) { @@ -397,7 +404,7 @@ function gnutaler_init_gateway_class() * @param $purpose - What will be added to the url. * @param $body - Content of the request. * @return string - return the final url. - * @since 1.0 + * @since 0.6.0 */ public function create_api_url ($url, $purpose, $body ): string { @@ -418,7 +425,7 @@ function gnutaler_init_gateway_class() * * @param $url - URL to the backend * @return bool - Returns if valid or not. - * @since 1.0 + * @since 0.6.0 */ public function verify_backend_url( $url ): bool { @@ -437,7 +444,7 @@ function gnutaler_init_gateway_class() * * @param $order_id - ID of the order to get the Order from the WooCommerce Webshop * @return array|void - Array with result => success and redirection url otherwise it returns void. - * @since 1.0 + * @since 0.6.0 */ public function process_payment( $order_id ) { @@ -477,10 +484,13 @@ function gnutaler_init_gateway_class() //Empties the shopping cart WC()->cart->empty_cart(); + wc_add_notice($wc_order->get_currency()); + //Returns that the payment process finished successfully and redirects the costumer to confirm the payment via GNU Taler $this->add_log_entry( 'transaction', 'Userid: ' . $user_id . ' - Orderid: ' . $order_id . ' - Customer is being redirected to the payment confirmation site' ); return array( 'result' => 'success', + // 'redirect' => '', 'redirect' => $order_request['url'], ); } @@ -501,7 +511,7 @@ function gnutaler_init_gateway_class() * @param $user_id - User id for logging. * @param $order_id - Order id for logging. * @return array|void - Array with boolean true|false, url or error message with http status code. - * @since 1.0 + * @since 0.6.0 */ public function send_order_request( $backend_url, $json, $user_id, $order_id ): array { @@ -549,12 +559,11 @@ function gnutaler_init_gateway_class() * * * The amount of the refund request can, at the moment, only be refunded in the currency 'KUDOS', which is the currency that the GNU Taler Payment system uses. - * This will change in the future and therefore the following code must be replaced by the code below: 'amount' => 'KUDOS:2', - * 'amount' => $wc_order_currency . ':' . $wc_order_total_amount, - * * + * This will change in the future. + * * @param $order_id - To get the order from the WooCommerce Webshop * @return array - return the JSON Format. - * @since 1.0 + * @since 0.6.0 */ public function convert_to_checkout_json( $order_id ): array { @@ -565,16 +574,16 @@ function gnutaler_init_gateway_class() $wc_order_id = $wc_order->get_order_key() . '_' . $wc_order->get_order_number(); $merchant_option = $this->get_option( 'merchant_information' ); - $wc_order_products_array = $this->mutate_products_to_json_format( $wc_cart, $wc_order_currency ); + $wc_order_products_array = $this->mutate_products_to_json_format( $wc_cart, $wc_order_currency, $wc_order); $wc_order_merchant = $this->mutate_merchant_information_to_json_format( $merchant_option ); $order_json = array( 'order' => array( - //'amount' => $wc_order_currency . ':' . $wc_order_total_amount, - 'amount' => 'KUDOS:5', - 'summary' => 'Order of the following items:', + 'amount' => $wc_order_currency . ':' . $wc_order_total_amount, + 'summary' => 'Order from the merchant ' . $this->get_option('merchant_name') . ': ', 'fulfillment_url' => $this->get_option( 'Fulfillment_url', get_home_url() ), + //'payment_url' => $this->get_option( 'Payment_url' ), 'order_id' => $wc_order_id, 'merchant' => $wc_order_merchant, 'products' => $wc_order_products_array, @@ -589,9 +598,9 @@ function gnutaler_init_gateway_class() * @param $wc_cart - The content of the WooCommerce Cart. * @param $wc_order_currency - The currency the WooCommerce Webshop uses. * @return array - Returns an array of products. - * @since 1.0 + * @since 0.6.0 */ - public function mutate_products_to_json_format( $wc_cart, $wc_order_currency ): array + public function mutate_products_to_json_format( $wc_cart, $wc_order_currency, $wc_order ): array { $wc_order_products_array = array(); foreach ( $wc_cart as $product ) { @@ -600,6 +609,7 @@ function gnutaler_init_gateway_class() 'quantity' => $product['quantity'], 'price' => $wc_order_currency . ':' . $product['data']->get_price(), 'product_id' => $product['data']->get_id(), + 'delivery_location' => $this->mutate_shipping_information_to_json_format($wc_order), ); } return $wc_order_products_array; @@ -610,7 +620,7 @@ function gnutaler_init_gateway_class() * * @param $merchant_option - If the webshop owner allows to send the backend their information * @return array - Returns an array of merchant information's. - * @since 1.0 + * @since 0.6.0 */ public function mutate_merchant_information_to_json_format( $merchant_option ): array { @@ -635,7 +645,7 @@ function gnutaler_init_gateway_class() //Split the address into street and street number foreach ( $store_address_array as $char ) { - if ( is_numeric( $char ) && ! $whitechar_encounter ) { + if ( ! $whitechar_encounter ) { $store_address_streetNr .= $char; } elseif ( ctype_space( $char ) ) { $whitechar_encounter = true; @@ -665,11 +675,52 @@ function gnutaler_init_gateway_class() * If the refund request is finished successfully it returns an refund url, which can be send to the customer to finish the refund transaction. * If an error it will throw a WP_Error message and inform the system administrator. * + * @param $wc_order + * @return array + * @since 0.6.0 + */ + public function mutate_shipping_information_to_json_format($wc_order): array + { + $whitechar_encounter = false; + $shipping_address_street = ''; + $shipping_address_streetNr = ''; + + $store_address = $wc_order->get_shipping_address_1(); + $store_address_inverted = strrev( $store_address ); + $store_address_array = str_split( $store_address_inverted ); + + //Split the address into street and street number + foreach ( $store_address_array as $char ) { + if ( ! $whitechar_encounter ) { + $shipping_address_street .= $char; + } elseif ( ctype_space( $char ) ) { + $whitechar_encounter = true; + } else { + $shipping_address_street .= $char; + } + } + + return array( + 'country' => $wc_order->get_shipping_country(), + 'state' => $wc_order->get_shipping_state(), + 'city' => $wc_order->get_shipping_city(), + 'ZIP code' => $wc_order->get_shipping_postcode(), + 'street' => $shipping_address_street, + 'street number' => $shipping_address_streetNr, + ); + } + + /** + * Processes the refund transaction if requested by the system administrator of the webshop + * + * If the refund request is finished successfully it returns an refund url, which can be send to the customer to finish the refund transaction. + * If an error it will throw a WP_Error message and inform the system administrator. + * * @param $order_id - Order id for logging. * @param null $amount - Amount that is requested to be refunded. * @param string $reason - Reason for the refund request. * @return bool|WP_Error - Returns true or throws an WP_Error message in case of error. - * @since 1.0 + * @since 0.6.0 */ public function process_refund( $order_id, $amount = null, $reason = '' ) { @@ -719,7 +770,7 @@ function gnutaler_init_gateway_class() * @param $backend_url - URL where the request will be sent. * @param $json - JSON array with the data of the refund request for the backend. * @return array|void - Array with boolean true|false, url or error message with http status code. - * @since 1.0 + * @since 0.6.0 */ public function send_refund_request( $backend_url, $json ): array { @@ -756,21 +807,19 @@ function gnutaler_init_gateway_class() * Converts the information of the refund request into a JSON format that can be send to the GNU Taler Backend. * * The amount of the refund request can, at the moment, only be refunded in the currency 'KUDOS', which is the currency that the GNU Taler Payment system uses. - * This will change in the future and therefore the following code must be replaced by the code below: 'refund' => 'KUDOS:1' - * 'refund' => $order->get_currency() . ':' . $amount, + * This will change in the future. * * @param $order - Order where the refund request originated from. * @param $amount - Amount to be refunded. * @param $reason - Reason of refund. * @return array - returns the JSON Format. - * @since 1.0 + * @since 0.6.0 */ public function convert_refund_to_json( $order, $amount, $reason ): array { return array( 'order_id' => $order->get_order_key() . '_' . $order->get_order_number(), - 'refund' => 'KUDOS:4', - //'refund' => $order->get_currency() . ':' . $amount, + 'refund' => $order->get_currency() . ':' . $amount, 'instance' => 'default', 'reason' => $reason, ); @@ -783,7 +832,7 @@ function gnutaler_init_gateway_class() * @param $type - What kind of log it is. * @param $message - What the message of the log entry is. * @return void - Returns void. - * @since 1.0 + * @since 0.6.0 */ public function add_log_entry( $type, $message ): void { |