Ana içeriğe geç

Python SDK

Kurulum​

Python 3.10 ve üzeri gerekir. Yalnızca standart kütüphaneyi kullanır.

pip install odemehub

İstemci​

import os

from odemehub import Client, Options

client = Client(Options(
base_url="https://odeme.gurmehub.com",
team=os.environ["ODEMEHUB_TEAM"], # Çalışma Alanı Kimliği
channel_token=os.environ["ODEMEHUB_CHANNEL_TOKEN"],
api_key=os.environ["ODEMEHUB_API_KEY"],
api_secret=os.environ["ODEMEHUB_API_SECRET"],
# timeout=60.0, # saniye
))

İstekler urllib ile gönderilir. Başka bir HTTP istemcisi için post(url, headers, body, timeout) metodu olan bir nesne Client(options, transport=...) ile verilebilir.

İstekler odemehub.request, yanıtlar odemehub.response modülündedir. İstek alanları yalnızca adla verilir.

Müşteri ve kart​

from odemehub.request import Card, Customer, TaxDetails

customer = Customer(
channel_reference="musteri-88",
firstname="Ahmet",
lastname="Yılmaz",
phone="05551112233",
address="Kızılırmak Mah. Dumlupınar Blv. No:3",
district="Çankaya",
province="Ankara",
country="Türkiye",
# tax=TaxDetails(company_title="...", tax_number="...", tax_office="..."),
)

card = Card(
holder_name="AHMET YILMAZ",
number="5400360000000003",
security_code="000",
expiry_month="12",
expiry_year="2030",
# should_save=True,
)

Regular ödeme​

from odemehub.request import RegularPayment

payment = client.regular_payment(RegularPayment(
channel_reference="SIP-10231",
amount="450.00",
installment_number=1,
ip=request.remote_addr,
customer=customer,
card=card,
))

payment.result.successful # bool
payment.result.message # str | None
payment.transaction_token # str
payment.conversion # Conversion | None
payment.saved_card # SavedCard | None

3D Secure ödeme​

from odemehub.request import SecurePayment

payment = client.secure_payment(SecurePayment(
channel_reference="SIP-10232",
amount="450.00",
installment_number=1,
ip=request.remote_addr,
callback_url="https://magazam.com/odeme/donus",
customer=customer,
card=card,
))

if payment.result.successful:
return redirect(payment.redirect_url)

callback_url işleyicisi:

from odemehub.request import RetrievePayment

outcome = client.retrieve_payment(RetrievePayment(
transaction_token=request.form["transaction_token"],
))

if outcome.result.successful:
... # ödendi

Akış: Secure (3D) Ödeme.

Ödeme sorgusu​

from odemehub.request import RetrievePayment

payment = client.retrieve_payment(RetrievePayment(transaction_token=transaction_token))

İade ve iptal​

from odemehub.request import CancelPayment, RefundPayment

cancel = client.cancel_payment(CancelPayment(transaction_token=transaction_token))
refund = client.refund_payment(RefundPayment(transaction_token=transaction_token, amount="100.00"))

refund.type # "refund" | "cancel"
refund.amount # geri verilen tutar

Diğer uçlar​

Metotİstek sınıfı
order_paymentOrderPayment(channel_reference, success_url, customer, items: list[OrderItem], cancel_url=, description=, currency=, payment_provider_token=)
subscription_paymentSubscriptionPayment(channel_reference, items: list[SubscriptionItem], success_url, customer, cancel_url=, webhook_url=, payment_provider_token=)
retrieve_subscription / cancel_subscriptionRetrieveSubscription(subscription_token=) / CancelSubscription(subscription_token=)
retrieve_binRetrieveBin(bin, amount, payment_provider_token=, currency=)
save_productSaveProduct(channel_reference, name, type, amount, tax_rate, period=, currency=, is_active=)
save_cardSaveCard(customer, card, payment_provider_token=)
saved_cardsSavedCards(customer=NamedCustomer(channel_reference=))
default_saved_card / delete_saved_cardDefaultSavedCard(customer=NamedCustomer(...), saved_card_token=) / DeleteSavedCard(...)

Kanal alanı olan isteklerde channel_token= verilerek istemcinin kanalı istek bazında değiştirilebilir. Örnekler her uç sayfasının SDK örnekleri bölümündedir.

Hata yönetimi​

from odemehub import (
AuthenticationError, OdemehubError, SignatureError,
TransportError, UnexpectedResponseError, ValidationError,
)

try:
client.regular_payment(payment_request)
except ValidationError as error:
error.errors # {"transaction.amount": ["..."]}
except AuthenticationError:
... # 401
except UnexpectedResponseError as error:
error.status # ör. 403
except SignatureError:
... # yanıtın imzası tutmadı
except TransportError:
... # gateway'e ulaşılamadı; sonuç bilinmiyor

Abonelik webhook'u​

from odemehub import SignatureError

@app.post("/odemehub/abonelik")
def abonelik_bildirimi():
try:
webhook = client.subscription_webhook(
request.get_data(), # Django: request.body
request.headers.get("X-Signature"),
)
except SignatureError:
return "", 400

if webhook.is_active():
... # webhook.subscription.ends_at
elif webhook.is_past_due():
... # webhook.subscription.checkout_url
elif webhook.is_cancelled():
...
elif webhook.is_ended():
...

return "", 200

Ayrıntılar: Abonelik Webhook'u.