İçeriğe geç
Tüm kodlar

ER3NSHOP

Kurulum sihirbazlı online mağaza sistemi

76 satır · 3,4 KBHamİndir
<?php

// PayTR iFrame API: token alınır, kart formu sitenin içinde iframe olarak açılır.
// Ödeme sonucu PayTR sunucusundan /odeme-bildirim adresine gelir (sitenin internetten erişilebilir olması gerekir).
function gateway_paytr_start($order, $payment)
{
    $merchant_id = setting('paytr_merchant_id');
    $key = setting('paytr_key');
    $salt = setting('paytr_salt');

    $basket = base64_encode(json_encode(array_map(
        fn($line) => [$line['name'], number_format($line['total'] / max(1, $line['qty']), 2, '.', ''), $line['qty']],
        payment_basket($order)
    ), JSON_UNESCAPED_UNICODE));

    $amount = (int) round($order['total'] * 100);
    $max = card_max_installment();
    $no_installment = $max > 1 ? 0 : 1;
    $test = card_test_mode() ? 1 : 0;
    $ip = client_ip() ?: '127.0.0.1';

    $hash = $merchant_id . $ip . $payment['reference'] . $order['email'] . $amount . $basket . $no_installment . ($max > 1 ? $max : 0) . 'TL' . $test;
    $token = base64_encode(hash_hmac('sha256', $hash . $salt, $key, true));

    $response = json_decode(http_request('https://www.paytr.com/odeme/api/get-token', [
        'merchant_id'       => $merchant_id,
        'user_ip'           => $ip,
        'merchant_oid'      => $payment['reference'],
        'email'             => $order['email'],
        'payment_amount'    => $amount,
        'paytr_token'       => $token,
        'user_basket'       => $basket,
        'debug_on'          => DEBUG ? 1 : 0,
        'no_installment'    => $no_installment,
        'max_installment'   => $max > 1 ? $max : 0,
        'user_name'         => $order['name'],
        'user_address'      => $order['address'] . ' ' . $order['district'] . '/' . $order['city'],
        'user_phone'        => $order['phone'],
        'merchant_ok_url'   => payment_result_url($order),
        'merchant_fail_url' => payment_result_url($order) . '&durum=hata',
        'timeout_limit'     => payment_timeout(),
        'currency'          => 'TL',
        'test_mode'         => $test,
        'lang'              => 'tr',
    ]), true);

    if (($response['status'] ?? '') !== 'success') {
        throw new RuntimeException('PayTR: ' . ($response['reason'] ?? 'ödeme başlatılamadı.'));
    }
    return ['type' => 'iframe', 'url' => 'https://www.paytr.com/odeme/guvenli/' . $response['token']];
}

// PayTR sunucusundan gelen bildirim. Cevap olarak düz "OK" beklenir.
function gateway_paytr_notify()
{
    $reference = post('merchant_oid', '');
    $status = post('status', '');
    $total = post('total_amount', '');
    $expected = base64_encode(hash_hmac('sha256', $reference . setting('paytr_salt') . $status . $total, setting('paytr_key'), true));

    if (!hash_equals($expected, (string) post('hash', ''))) {
        http_response_code(400);
        exit('PAYTR notification failed: bad hash');
    }

    $payment = payment_by_reference($reference);
    if ($payment) {
        $raw = ['status' => $status, 'total_amount' => $total, 'payment_type' => post('payment_type'), 'installment_count' => post('installment_count'), 'test_mode' => post('test_mode'), 'reason' => post('failed_reason_msg')];
        if ($status === 'success') {
            payment_paid($payment, $reference, (int) post('installment_count', 1), $raw);
        } else {
            payment_failed($payment, post('failed_reason_msg', 'Ödeme reddedildi.'), $raw);
        }
    }
    exit('OK');
}

Lisans bilgisi projenin LICENSE dosyasında.