Ve bu şekilde müşteri ödeme yaptığını zannediyor.
Bunun için ne tür bir call-back kodu ekleyebilirim ?
Eklenti kodları :
if (!defined('ABSPATH')) {
exit;
}
add_filter('woocommerce_payment_gateways', 'payme_add_gateway_class');
function payme_add_gateway_class($gateways) {
$gateways[] = 'WC_Gateway_payme';
return $gateways;
}
add_action('plugins_loaded', 'payme_init_gateway_class');
function payme_init_gateway_class() {
class WC_Gateway_payme extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'payme';
$this->has_fields = false;
$this->order_button_text = __('Pay with payme', 'woocommerce');
$this->method_title = __('payme', 'woocommerce');
$this->method_description = __('Pay with payme', 'woocommerce');
$this->supports = array(
'products',
);
$this->init_form_fields();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->apiKey = $this->get_option('apiKey');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_api_wc_gateway_payme', array($this, 'webhook'));
}
public function needs_setup() {
return $this->apiKey == '';
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable payme', 'woocommerce'),
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('payme', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),
'default' => __('Pay with payme', 'woocommerce'),
),
'apiKey' => array(
'title' => __('payme API Key', 'woocommerce'),
'type' => 'text',
'description' => __('Please enter your payme API key', 'woocommerce'),
'default' => '',
'desc_tip' => true,
'placeholder' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
),
);
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
$amount = $order->get_total();
$currency = $order->get_currency();
$total = $this->calculateUSD($amount, $currency);
$raw = wp_remote_post(
"https://payme.co.uk/api/merchant/create",
array(
"method" => "POST",
"body" => json_encode(array(
"apiKey" => $this->apiKey,
"email" => $order->get_billing_email(),
"amount" => $total,
"webhookURL" => WC()->api_request_url("wc_gateway_payme"),
"returnURL" => esc_url_raw($this->get_return_url($order)),
"metadata" => $order->get_id(),
)),
"timeout" => 70,
"user-agent" => "WooCommerce/" . WC()->version,
"httpversion" => "1.1",
"headers" => array(
"Content-Type" => "application/json; charset=utf-8"
),
)
);
$response = json_decode($raw['body']);
if (!isset($response->response) || $response->status !== "success" || $response->response === "") {
return false;
}
return array(
'result' => 'success',
'redirect' => 'https://payme.co.uk/checkout/' . $response->response,
);
}
public function webhook() {
global $woocommerce;
$response = json_decode(file_get_contents("php://input"));
if (!isset($response->metadata)) {
return false;
}
$order = wc_get_order($response->metadata);
if (!$order || $order === null) {
return false;
}
if ($response->apiKey !== $this->apiKey) {
return false;
}
if ($response->status !== "paid") {
return false;
}
$order->payment_complete();
$order->reduce_order_stock();
$woocommerce->cart->empty_cart();
}
private function calculateUSD($amount, $currency){
$amount = (float)$amount;
$currency = strtoupper($currency);
if ($amount <= 0) {
return $amount;
}
if (!$currency || $currency === null || $currency === "") {
return $amount;
}
$url = "https://payme.co.uk/api/merchant/rates/USD" . $currency;
$req = wp_remote_get($url);
$raw = wp_remote_retrieve_body($req);
$response = json_decode($raw);
if (!$raw || $raw === null) {
return $amount;
}
if (!isset($response->status) || $response->status !== "success") {
return $amount;
}
$multiplier = (float)$response->response;
if ($multiplier <= 0) {
return $amount;
}
return $amount * $multiplier;
}
}
}
?>