- خانه
- مهاجرت بین درگاهها
راهنماها
مهاجرت بین درگاهها
قرارداد GatewayAdapter دو متد پهنا دارد، پس جابهجایی بین درگاهها عمدتاً یک تغییر کانفیگ است. عمدتاً.
آنچه بدون تغییر منتقل میشود
- فراخوانی
createPaymentشما، بهجز رشته provider. - فراخوانی
verifyCallbackو شاخهisSuccessfulآن. - همه کلاسهای خطا و کل ساختار
catchرویPaymentError— کلاینت مقادیر غیر PaymentError را برای هر آداپتور یکسان پیچ میکند. - مدیریت واحد پول.
toRialsمستقل از درگاه است و از هر آداپتوری انتظار میرود در رفت آن را صدا بزند. - لاگ چرخه حیات شما. پنج رویداد از کلاینت میآیند، نه از آداپتورها.
آنچه باید برای هر درگاه بازنویسی شود
| جنبه | چگونه تغییر میکند |
|---|---|
| Callback transport | ZarinPal sends a GET query string; most bank PSPs POST an HTML form. You parse it before building CallbackRequest. |
| Config fields | merchantId for ZarinPal, X-API-KEY for IDPay, WSDL endpoint plus terminal id for Behpardakht. |
| Protocol | REST JSON for the facilitators; SOAP for Mellat; token-then-form for Saman SEP. A SOAP adapter needs an XML serialiser the SDK does not ship. |
| Success codes | 100/101 for ZarinPal; 100/101 plus 1 and 10 states for IDPay; a comma-separated string for Mellat. |
| Identifier shape | 36-character authority, 32-character hex id, numeric RefId, or a bare token. |
| Signature | ZarinPal has none — it authenticates by lookup. Signed gateways are what InvalidSignatureError exists for. |
الگوی دو ارائهدهنده
چون کلید provider اتحاد هر چیزی است که پیکربندی کنید، اجرای همزمان دو درگاه همان مقدار کد را میخواهد که اجرای یکی.
import { UnoPay, ZarinpalAdapter, type GatewayAdapter } from 'unopay';
const unopay = new UnoPay({
zarinpal: new ZarinpalAdapter({ merchantId: process.env.ZARINPAL_MERCHANT_ID! }),
idpay: myIdpayAdapter, // a GatewayAdapter you wrote yourself
});
// 'zarinpal' | 'idpay' — anything else fails to compile.
type Provider = 'zarinpal' | 'idpay';
app.post('/checkout/:provider', async (req, res) => {
const provider = req.params.provider as Provider;
const { amount, currency } = req.body;
const payment = await unopay.createPayment(provider, {
amount: Number(amount),
currency,
callbackUrl: 'https://yoursite.com/api/payment/callback',
metadata: { description: 'Order ' + req.body.orderId },
});
await saveAuthority(req.body.orderId, provider, payment.transactionId);
res.redirect(payment.redirectUrl);
});
نام ارائهدهنده را کنار آتوریتی ذخیره کنید. در زمان کالبک باید بدانید کدام آداپتور را صدا بزنید، و خود کالبک به شما نمیگوید.
سه شکست خاموش که باید بررسی کنید
پارس کالبک. هندلر GET در برابر فرم POST بیصدا {} تولید میکند. برچسب واحد. درگاهی که در واحد پایه متفاوتی تسویه میکند مبلغ شما را میشکند هرچند بررسی تایپ رد شود. عرض شناسه. طول ستونی که برای آتوریتی ۳۶ کاراکتری اندازه گرفته شده، RefId عددی را برش میدهد یا برعکس.