最近在做二开给国外的Web应用系统对接官方支付宝支付收款渠道,找到了这个Omnipay,这个库对国内外的支付网关进行了高层的封装,给每个支付网关实现提供了抽象接口,由具体的支付网关来实现这些接口。如果想实现国内国外多支付接口,Omnipay是个不错选择。
目录 隐藏 1 为什么使用 Omnipay 而不是网关的官方 PHP 包/示例代码? 2 用法示例(调用Stripe支付网关) 3 已实现的支付网关 4 支付宝下单代码示例
Omnipay是一个用PHP编写的开源支付处理库。它允许开发人员在不同的支付网关之间创建统一的API,从而简化了接入多种支付方式的过程。使用Omnipay,开发人员可以编写与不同支付网关兼容的代码,而无需担心具体的实现细节。这使得在项目中轻松地切换支付网关成为可能,而无需重写整个支付处理逻辑。
为什么使用 Omnipay 而不是网关的官方 PHP 包/示例代码?
- 因为您可以学习一个 API 并在使用不同支付网关的多个项目中使用它
- 因为如果您需要更改支付网关,则无需重写代码
- 因为大多数官方的PHP支付网关库都是一团糟
- 因为大多数支付网关的文档都非常差
- 因为你写的是购物车系统,需要支持多个网关
用法示例(调用Stripe支付网关)
use Omnipay\Omnipay;
$gateway = Omnipay::create('Stripe');
$gateway->setApiKey('abc123');
$formData = array('number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2030', 'cvv' => '123');
$response = $gateway->purchase(array('amount' => '10.00', 'currency' => 'USD', 'card' => $formData))->send();
if ($response->isRedirect()) {
// redirect to offsite payment gateway
$response->redirect();
} elseif ($response->isSuccessful()) {
// payment was successful: update database
print_r($response);
} else {
// payment failed: display message to customer
echo $response->getMessage();
}
正如您所看到的,Omnipay 拥有一致且经过深思熟虑的 API,尝试尽可能多地抽象出各种支付网关之间的差异。
Omnipay 是一个软件包的集合,这些软件包都依赖于omnipay/common 软件包来提供一致的接口。不依赖于官方支付网关 PHP 包 – 我们更喜欢直接使用 HTTP API。在底层,Omnipay 使用流行且强大的PHP-HTTP 库来发出 HTTP 请求。使用 league/omnipay 时,默认需要 Guzzle 适配器。
可以通过克隆现有包的布局来创建新网关。为您的包选择名称时,请不要使用omnipay作为vendor前缀,因为这意味着它受到官方支持。您应该使用您自己的用户名作为vendor前缀,并在程序包名称前添加omnipay-,以明确您的程序包可与 Omnipay 配合使用。例如,如果您的 GitHub 用户名是 santa,并且您正在实现giftpay 支付库,那么您的 Composer 包的好名称应该是 santa/omnipay-giftpay。
Github开源地址:https://github.com/thephpleague/omnipay
已实现的支付网关
Gateway | 2.x | 3.x | Composer Package | Maintainer |
---|---|---|---|---|
2c2p | ✓ | ✓ | dilab/omnipay-2c2p | Xu Ding |
2Checkout | ✓ | – | omnipay/2checkout | Omnipay |
2Checkout Improved | ✓ | – | collizo4sky/omnipay-2checkout | Agbonghama Collins |
99Bill | – | ✓ | x-class/omnipay-99bill | Laraveler |
Acapture (PayVision) | ✓ | – | qup/omnipay-acapture | Niels de Vries |
Adyen | – | ✓ | academe/omnipay-adyen | Jason Judge |
Affirm | ✓ | ✓ | eduardlleshi/omnipay-affirm | Eduard Lleshi |
Agms | ✓ | – | agmscode/omnipay-agms | Maanas Royy |
Alipay(Global) | ✓ | ✓ | lokielse/omnipay-global-alipay | Loki Else |
Alipay | ✓ | ✓ | lokielse/omnipay-alipay | Loki Else |
Allied Wallet | ✓ | – | delatbabel/omnipay-alliedwallet | Del |
Arca | – | ✓ | k3rnel/omnipay-arca | Poghos Boyajyan |
Authorize.Net | ✓ | ✓ | omnipay/authorizenet | Jason Judge |
Authorize.Net API | – | ✓ | academe/omnipay-authorizenetapi | Jason Judge |
Authorize.Net Recurring Billing | – | ✓ | cimpleo/omnipay-authorizenetrecurring | CimpleO |
Bankart | ✓ | ✓ | ampeco/omnipay-bankart | Ampeco |
Barclays ePDQ | ✓ | – | digitickets/omnipay-barclays-epdq | DigiTickets |
BlueOrange bank | – | ✓ | deh4eg/omnipay-blueorange | Denis Smolakov |
Beanstream | ✓ | – | lemonstand/omnipay-beanstream | LemonStand |
BitPay | ✓ | ✓ | hiqdev/omnipay-bitpay | HiQDev |
BKM Express | ✓ | – | yasinkuyu/omnipay-bkm | Yasin Kuyu |
BlueSnap | ✓ | – | vimeo/omnipay-bluesnap | Vimeo |
Braintree | ✓ | ✓ | omnipay/braintree | Omnipay |
Buckaroo | ✓ | – | omnipay/buckaroo | Omnipay |
CardGate | ✓ | – | cardgate/omnipay-cardgate | CardGate |
CardSave | ✓ | – | omnipay/cardsave | Omnipay |
CashBaBa | ✓ | ✓ | omnipay/cashbaba | Recursion Technologies Ltd |
Checkout.com | ✓ | – | fotografde/checkoutcom | fotograf.de |
CloudBanking | ✓ | – | cloudbanking/omnipay-cloudbanking | Cloudbanking |
Coinbase | ✓ | – | omnipay/coinbase | Omnipay |
CoinGate | ✓ | – | coingate/omnipay-coingate | CoinGate |
CoinPayments | ✓ | ✓ | InkedCurtis/omnipay-coinpayments | InkedCurtis |
Creditcall | ✓ | – | meebio/omnipay-creditcall | John Jablonski |
CSOB (GP WebPay) | ✓ | – | bileto/omnipay-csob | |
Cybersource | ✓ | ✓ | dioscouri/omnipay-cybersource | Dioscouri Design |
Cybersource SOAP | ✓ | – | dabsquared/omnipay-cybersource-soap | DABSquared |
DataCash | ✓ | – | digitickets/omnipay-datacash | DigiTickets |
Datatrans | ✓ | – | w-vision/datatrans | Dominik Pfaffenbauer |
Datatrans | ✓ | ✓ | academe/omnipay-datatrans | Jason Judge |
Docdata Payments | ✓ | ✓ | uskur/omnipay-docdata-payments | Uskur |
Dummy | ✓ | ✓ | omnipay/dummy | Del |
Ebanx | – | ✓ | descubraomundo/omnipay-ebanx | Descubra o Mundo |
eGHL | – | ✓ | e-ghl/omnipay | Jawad Humayun |
eGHL | ✓ | ✓ | dilab/omnipay-eghl | Xu Ding |
eCoin | ✓ | ✓ | hiqdev/omnipay-ecoin | HiQDev |
ecoPayz | ✓ | – | dercoder/omnipay-ecopayz | Alexander Fedra |
eSewa | – | ✓ | sudiptpa/omnipay-esewa | Sujip Thapa |
EgopayRu | ✓ | – | pinguinjkeke/omnipay-egopaymentru | Alexander Avakov |
Elavon | ✓ | ✓ | lxrco/omnipay-elavon | Korri |
ePayments | ✓ | ✓ | hiqdev/omnipay-epayments | HiQDev |
ePayService | ✓ | ✓ | hiqdev/omnipay-epayservice | HiQDev |
eWAY | ✓ | ✓ | omnipay/eway | Del |
Fasapay | ✓ | – | andreas22/omnipay-fasapay | Andreas Christodoulou |
Faspay | ✓ | ✓ | David-Kurniawan/omnipay-faspay | David |
Fat Zebra | ✓ | – | delatbabel/omnipay-fatzebra | Del |
FreeKassa | ✓ | ✓ | hiqdev/omnipay-freekassa | HiQDev |
Fibank | – | ✓ | ampeco/omnipay-fibank | Ampeco |
First Data | ✓ | – | omnipay/firstdata | OmniPay |
Flo2cash | ✓ | – | guisea/omnipay-flo2cash | Aaron Guise |
Free / Zero Amount | ✓ | – | colinodell/omnipay-zero | Colin O’Dell |
GiroCheckout | ✓ | ✓ | academe/omnipay-girocheckout | Jason Judge |
Globalcloudpay | ✓ | – | dercoder/omnipay-globalcloudpay | Alexander Fedra |
GoCardless | ✓ | – | omnipay/gocardless | Del |
GoPay | ✓ | – | bileto/omnipay-gopay | |
GovPayNet | ✓ | – | omnipay/omnipay-govpaynet | FlexCoders |
GVP (Garanti) | ✓ | – | yasinkuyu/omnipay-gvp | Yasin Kuyu |
GVP (Garanti) | – | ✓ | emr/omnipay-gvp | Emre Akinci |
Helcim | ✓ | – | academe/omnipay-helcim | Jason Judge |
Icepay Payments | – | ✓ | superbrave/omnipay-icepay-payments | SuperBrave |
iDram | – | ✓ | ptuchik/omnipay-idram | Avik Aghajanyan |
iDeal | – | ✓ | deniztezcan/omnipay-ideal | Deniz Tezcan |
Ingenico ePayments | – | ✓ | deniztezcan/omnipay-ingenico-epayments | Deniz Tezcan |
iPay88 | ✓ | ✓ | dilab/omnipay-ipay88 | Xu Ding |
IfthenPay | ✓ | – | ifthenpay/omnipay-ifthenpay | Rafael Almeida |
Ikajo | ✓ | ✓ | hiqdev/omnipay-ikajo | HiQDev |
InterKassa | ✓ | ✓ | hiqdev/omnipay-interkassa | HiQDev |
InovioPay | ✓ | ✓ | mvestil/omnipay-inoviopay | Mark Vestil |
Iyzico | ✓ | – | yasinkuyu/omnipay-iyzico | Yasin Kuyu |
Judo Pay | ✓ | – | transportersio/omnipay-judopay | Transporters.io |
Klarna Checkout | ✓ | ✓ | myonlinestore/omnipay-klarna-checkout | MyOnlineStore |
Laybuy | ✓ | – | mediabeastnz/omnipay-laybuy | Myles Derham |
Luminor Gateway | – | ✓ | deh4eg/omnipay-luminor | Denis Smolakov |
Komerci (Rede, former RedeCard) | ✓ | – | byjg/omnipay-komerci | João Gilberto Magalhães |
Komoju | ✓ | – | vink/omnipay-komoju | Danny Vink |
Midtrans | ✓ | ✓ | dilab/omnipay-midtrans | Xu Ding |
MercadoPago | – | ✓ | lucassmacedo/omnipay-mercadopago | Lucas Macedo |
Magnius | – | ✓ | fruitcake/omnipay-magnius | Fruitcake |
Manual | ✓ | – | omnipay/manual | Del |
Migs | ✓ | – | omnipay/migs | Omnipay |
Mpesa | ✓ | – | wasksofts/omnipay-mpesa | wasksofts |
MTNCAM Mobile Money | ✓ | ✓ | larrytech7/omnipay-momocm | Akah Harvey |
Mollie | ✓ | ✓ | omnipay/mollie | Barry vd. Heuvel |
MOLPay | ✓ | – | leesiongchan/molpay | Lee Siong Chan |
MoMo | – | ✓ | phpviet/omnipay-momo | PHPViet |
Moneris | – | ✓ | unoapp-dev/omnipay-moneris | UNOapp Dev |
MultiCards | ✓ | – | incube8/omnipay-multicards | Del |
MultiSafepay | ✓ | – | omnipay/multisafepay | Alexander Deruwe |
MyCard | ✓ | – | xxtime/omnipay-mycard | Joe Chu |
MyFatoorah | – | ✓ | myfatoorah/omnipay | MyFatoorah Plugins Team |
National Australia Bank (NAB) Transact | ✓ | ✓ | sudiptpa/omnipay-nabtransact | Sujip Thapa |
NestPay (EST) | ✓ | – | yasinkuyu/omnipay-nestpay | Yasin Kuyu |
NestPay (EST) | – | ✓ | uskur/omnipay-nestpay | Uskur |
Netaxept (BBS) | ✓ | – | omnipay/netaxept | Omnipay |
Netbanx | ✓ | – | omnipay/netbanx | Maks Rafalko |
Neteller | ✓ | – | dercoder/omnipay-neteller | Alexander Fedra |
NetPay | ✓ | – | netpay/omnipay-netpay | NetPay |
Network Merchants Inc. (NMI) | ✓ | – | mfauveau/omnipay-nmi | Matthieu Fauveau |
Nocks | ✓ | ✓ | nocksapp/omnipay-nocks | Nocks |
Nuvei | – | ✓ | nmc9/omnipay-nuvei | DiversifiedTech |
OkPay | ✓ | ✓ | hiqdev/omnipay-okpay | HiQDev |
OnePay | ✓ | ✓ | dilab/omnipay-onepay | Xu Ding |
Openpay Australia | ✓ | ✓ | sudiptpa/omnipay-openpay | Sujip Thapa |
Oppwa | ✓ | ✓ | vdbelt/omnipay-oppwa | Martin van de Belt |
PAY. (Pay.nl & Pay.be) | ✓ | ✓ | paynl/omnipay-paynl | Andy Pieters |
PayMongo | – | ✓ | oozman/omnipay-paymongo | Oozman |
Payoo | ✓ | ✓ | dilab/omnipay-payoo | Xu Ding |
Pacnet | ✓ | – | mfauveau/omnipay-pacnet | Matthieu Fauveau |
Pagar.me | ✓ | – | descubraomundo/omnipay-pagarme | Descubra o Mundo |
Paratika (Asseco) | ✓ | – | yasinkuyu/omnipay-paratika | Yasin Kuyu |
PayFast | ✓ | – | omnipay/payfast | Omnipay |
PayGate | – | ✓ | mvnrsa/paygate | Marnus van Niekerk |
Payflow | ✓ | – | omnipay/payflow | Del |
PaymentExpress (DPS) | ✓ | ✓ | omnipay/paymentexpress | Del |
PaymentExpress / DPS (A2A) | ✓ | – | onlinesid/omnipay-paymentexpress-a2a | Sid |
PaymentgateRu | ✓ | ✓ | pinguinjkeke/omnipay-paymentgateru | Alexander Avakov |
PaymentSense | ✓ | – | digitickets/omnipay-paymentsense | DigiTickets |
PaymentWall | ✓ | – | incube8/omnipay-paymentwall | Del |
Paynow | – | ✓ | pay-now/omnipay-paynow | Paynow |
PayPal | ✓ | ✓ | omnipay/paypal | Del |
PayPro | ✓ | – | paypronl/omnipay-paypro | Fruitcake |
PAYONE | ✓ | ✓ | academe/omnipay-payone | Jason Judge |
Paysafecard | ✓ | – | dercoder/omnipay-paysafecard | Alexander Fedra |
Paysafecard | – | ✓ | worldstream-labs/omnipay-paysafecard | Worldstream |
Paysafe Payment Hub (Neteller) | – | ✓ | worldstream-labs/omnipay-paysafe-payment-hub | Worldstream |
Paysera | ✓ | – | povils/omnipay-paysera | Povils |
Paysera | – | ✓ | semyonchetvertnyh/omnipay-paysera | Semyon Chetvertnyh |
PaySimple | ✓ | – | dranes/omnipay-paysimple | Dranes |
PaySsion | ✓ | – | inkedcurtis/omnipay-payssion | Curtis |
PayTrace | ✓ | – | softcommerce/omnipay-paytrace | Oleg Ilyushyn |
PayU | ✓ | – | bileto/omnipay-payu | |
PayZen | ✓ | – | ubitransports/omnipay-payzen | Ubitransport |
Paxum | ✓ | ✓ | hiqdev/omnipay-paxum | HiQDev |
Pelecard | ✓ | ✓ | uskur/omnipay-pelecard | Uskur |
Pin Payments | ✓ | – | omnipay/pin | Del |
Ping++ | ✓ | – | phoenixg/omnipay-pingpp | Huang Feng |
POLi | ✓ | – | burnbright/omnipay-poli | Sid |
Portmanat | ✓ | – | dercoder/omnipay-portmanat | Alexander Fedra |
Posnet | ✓ | – | yasinkuyu/omnipay-posnet | Yasin Kuyu |
Postfinance | ✓ | – | bummzack/omnipay-postfinance | Roman Schmid |
Qiwi | ✓ | ✓ | hiqdev/omnipay-qiwi | HiQDev |
QQ Wallet(QPay) | – | ✓ | kuangjy/omnipay-qpay | Kuang Jiaye |
Quickpay | ✓ | – | nobrainerweb/omnipay-quickpay | Nobrainer Web |
Rabobank | ✓ | – | omnipay/rabobank | Barry vd. Heuvel |
Razorpay | ✓ | – | razorpay/omnipay-razorpay | razorpay |
Realex | ✓ | – | digitickets/omnipay-realex | DigiTickets |
RedSys | ✓ | – | nazka/sermepa-omnipay | Javier Sampedro |
RentMoola | ✓ | – | rentmoola/omnipay-rentmoola | Geoff Shaw |
RoboKassa | ✓ | ✓ | hiqdev/omnipay-robokassa | HiQDev |
RocketGate | ✓ | ✓ | mvestil/omnipay-rocketgate | Mark Vestil |
Sage Pay | ✓ | ✓ | omnipay/sagepay | Jason Judge |
Sberbank | – | ✓ | andrewnovikof/omnipay-sberbank | Andrew Novikov |
SecPay | ✓ | – | justinbusschau/omnipay-secpay | Justin Busschau |
SecurePay | ✓ | ✓ | omnipay/securepay | Omnipay |
Secure Trading | ✓ | – | meebio/omnipay-secure-trading | John Jablonski |
Sisow | ✓ | ✓ | fruitcakestudio/omnipay-sisow | Fruitcake |
Skrill | ✓ | – | alfaproject/omnipay-skrill | João Dias |
Sofort | ✓ | – | aimeoscom/omnipay-sofort | Aimeos GmbH |
Spreedly | ✓ | – | gregoriohc/omnipay-spreedly | Gregorio Hernández Caso |
Square | ✓ | ✓ | transportersio/omnipay-square | Transporters.io |
Starkpay | ✓ | ✓ | starkpay/omnipay | Starkpay |
Stripe | ✓ | ✓ | omnipay/stripe | Del |
TargetPay | ✓ | – | omnipay/targetpay | Alexander Deruwe |
TatraBank | ✓ | – | omnipay-tatrabank | |
ToyyibPay | – | ✓ | sitehandy/omnipay-toyyibpay | Amirol Zolkifli |
Tpay | ✓ | – | omnipay/tpay | Tpay.com |
UnionPay | ✓ | ✓ | lokielse/omnipay-unionpay | Loki Else |
Vantiv | ✓ | – | lemonstand/omnipay-vantiv | LemonStand |
Veritrans | ✓ | – | andylibrian/omnipay-veritrans | Andy Librian |
Vindicia | ✓ | – | vimeo/omnipay-vindicia | Vimeo |
VivaPayments | ✓ | – | delatbabel/omnipay-vivapayments | Del |
VR Payment | – | ✓ | antibodies-online/omnipay-vr-payment | antibodies-online |
WebMoney | ✓ | ✓ | dercoder/omnipay-webmoney | Alexander Fedra |
✓ | – | labs7in0/omnipay-wechat | 7IN0’s Labs | |
WechatPay | ✓ | ✓ | lokielse/omnipay-wechatpay | Loki Else |
WePay | ✓ | – | collizo4sky/omnipay-wepay | Agbonghama Collins |
Wirecard | ✓ | ✓ | igaponov/omnipay-wirecard | Igor Gaponov |
Wirecard | ✓ | – | academe/omnipay-wirecard | Jason Judge |
Worldpay XML Direct Corporate Gateway | ✓ | – | teaandcode/omnipay-worldpay-xml | Dave Nash |
Worldpay XML Hosted Corporate Gateway | ✓ | ✓ | catharsisjelly/omnipay-worldpay-cg-hosted | Chris Lock |
Worldpay Business Gateway | ✓ | ✓ | omnipay/worldpay | Omnipay |
Yandex.Kassa | ✓ | ✓ | hiqdev/omnipay-yandex-kassa | HiQDev |
Yandex.Money | ✓ | – | yandexmoney/omnipay | Roman Ananyev |
Yandex.Money for P2P payments | ✓ | ✓ | hiqdev/omnipay-yandexmoney | HiQDev |
Yekpay | – | ✓ | nekofar/omnipay-yekpay | Milad Nekofar |
ZarinPal | – | ✓ | nekofar/omnipay-zarinpal | Milad Nekofar |
支付宝下单代码示例
需安装omnipay-alipay包,方法是:composer require lokielse/omnipay-alipay
/**
* @var AopAppGateway $gateway
*/
$gateway = Omnipay::create('Alipay_AopPage');
$gateway->setSignType('RSA2'); // RSA/RSA2/MD5. Use certificate mode must set RSA2
$gateway->setAppId('the_app_id');
$gateway->setPrivateKey('the_app_private_key');
$gateway->setAlipayPublicKey('the_alipay_public_key'); // Need not set this when used certificate mode
$gateway->setReturnUrl('https://www.example.com/return');
$gateway->setNotifyUrl('https://www.example.com/notify');
// Must set cert path if you used certificate mode
//$gateway->setAlipayRootCert('the_alipay_root_cert'); // alipayRootCert.crt
//$gateway->setAlipayPublicCert('the_alipay_public_cert'); // alipayCertPublicKey_RSA2.crt
//$gateway->setAppCert('the_app_public_cert'); // appCertPublicKey.crt
//$gateway->setCheckAlipayPublicCert(true);
/**
* @var AopTradePagePayResponse $response
*/
$response = $gateway->purchase()->setBizContent([
'subject' => 'test',
'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
'total_amount' => '0.01',
'product_code' => 'FAST_INSTANT_TRADE_PAY',
])->send();
$url = $response->getRedirectUrl();