PHPackages                             jacky-zeng/omnipay-alipay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. jacky-zeng/omnipay-alipay

ActiveLibrary[Payment Processing](/categories/payments)

jacky-zeng/omnipay-alipay
=========================

完善omnipay-alipay库 新增无密退款及详细代码示例说明

v1.2(8y ago)25431PHP

Since Dec 7Pushed 8y agoCompare

[ Source](https://github.com/jacky-zeng/omnipay-alipay)[ Packagist](https://packagist.org/packages/jacky-zeng/omnipay-alipay)[ RSS](/packages/jacky-zeng-omnipay-alipay/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (5)Used By (0)

Omnipay: Alipay
===============

[](#omnipay-alipay)

[![Donate](https://camo.githubusercontent.com/9484ebc029b006d8c4ab83f30b2375e86919af95101624f9864d1296493894df/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d2545372542412541322545352538432538352d7265642e7376673f6c6f676f3d64617461253341696d616765253246706e672533426261736536342532436956424f5277304b47676f414141414e53556845556741414142414141414151434159414141416638253246396841414141475852465748525462325a30643246795a5142425a4739695a53424a6257466e5a564a6c5957523563636c6c5041414141795270564668305745314d4f6d4e76625335685a4739695a5335346258414141414141414477253246654842685932746c644342695a576470626a3069373775253246496942705a443069567a564e4d4531775132566f61556836636d5654656b355559337072597a6c6b496a38253242494478344f6e68746347316c6447456765473173626e4d366544306959575276596d5536626e4d366257563059533869494867366547317764477339496b466b62324a6c4946684e5543424462334a6c494455754d79316a4d444578494459324c6a45304e5459324d5377674d6a41784d6938774d6938774e6930784e446f314e6a6f794e7941674943416749434167496a346750484a6b5a6a70535245596765473173626e4d36636d526d50534a6f644852774f693876643364334c6e637a4c6d39795a7938784f546b354c7a41794c7a49794c584a6b5a69317a6557353059586774626e4d6a496a346750484a6b5a6a70455a584e6a636d6c7764476c76626942795a47593659574a76645851394969496765473173626e4d366547317750534a6f644852774f693876626e4d7559575276596d5575593239744c336868634338784c6a41764969423462577875637a70346258424e545430696148523063446f764c32357a4c6d466b62324a6c4c6d4e7662533934595841764d5334774c3231744c79496765473173626e4d36633352535a575939496d6830644841364c793975637935685a4739695a53356a62323076654746774c7a45754d43397a56486c775a5339535a584e7664584a6a5a564a6c5a694d694948687463447044636d566864473979564739766244306951575276596d5567554768766447397a6147397749454e544e69416f5457466a6157353062334e6f4b53496765473177545530365357357a644746755932564a52443069654731774c6d6c705a446f774f5445304e4551334f546442524559784d555532516b5647526b51304f444d334d304d33525463774e43496765473177545530365247396a6457316c626e524a52443069654731774c6d52705a446f774f5445304e45513351546442524559784d555532516b5647526b51304f444d334d304d33525463774e4349253242494478346258424e545470455a584a70646d566b526e4a766253427a64464a6c5a6a7070626e4e305957356a5a556c4550534a346258417561576c6b4f6a41354d545130524463334e304645526a457852545a4352555a47524451344d7a637a517a64464e7a41304969427a64464a6c5a6a706b62324e316257567564456c4550534a34625841755a476c6b4f6a41354d545130524463344e304645526a457852545a4352555a47524451344d7a637a517a64464e7a413049693825324249447776636d526d4f6b526c63324e796158423061573975506941384c334a6b5a6a70535245592532424944777665447034625842745a585268506941385033687759574e725a5851675a57356b50534a79496a38253242586625324679355141414171704a52454655654e7030557a317645304551666274376e375a6a78253242416b566f5170454a25324269534957514b4f6d436f456b48346b38674370416f6b4a436f614f6b7045564c5338522532426743494a494a4369437942454a4a4c46444570253246744f3939353734625a4d344545784568375a383374764466767a56675145644b6c785a767877737648744c4e3169595a4a45566b475a436d513870763448495951674f4f45736a36393673376465617175584673512532427350696a656a3573336b4d49682532463848634d684441415a4545326a516b747772526a395a6b4c53664d64786825324636395237665677366d786557592532426c544d6d796568435368424b776d7055494b7365714b63356c33484f67444359556b79554b487a374f6d50682532423859466d576f6d4862564b47554657585069334770446a555a374c446959527664344164515935734f5275744f316766373135527432644b44394a4d7849326f356f6a4d774633396a5255505142556959384434515777616a556b792532467349307948615955512532467767454a6b4c4c47505538456363774a5a6d504a58734842325a6f573649644170636f41466844306b4a313030597837704a494d4664385439564b526d32475a4a64644278584e4678746f48513431654d6b51636446436f7373357746253242424c73435453494544444c7770767a47496559736b38505a5974387a4561312532466e743252596d584225324664747a3353747332463749765330457979253246365a4c6469627a753662344d43776343514e4350444b2532466d5972645633736b4c72707347452532467a3077434662524c4355666d646f33454d774951706945534b6c5863746361355a7857616e433845715a6870315a4838562532467764416f733047726755486945696a47796549307851427533253242314d63313279474e64253246414e67526d47327a6d632532464f6f50594441594f6a376650532532465a37472532464f31486a326b4a6b714f4177694550416e4e6d316430484c6957596c61427375664356583838734b58435a7444566b6c76386b69634e756868315547625844527550463570643979794c65722532426b6d4e6b37444c7252436642253242713757755a71756c316d54426d25324250316c427851374948684d4e72503130376b6e6152456f7578356d436f56306563253246322532464a4f473074624c514e25324258313076253242797538685a395a37325525324231573463532532425032314667526b6c757125324243365a5a545061566e6633384847377262653725324654556878514e752532424d565041515941714e56596c616f25324255553841414141415355564f524b3543594949253344)](https://www.zengyanqi.com/myhtml/my_pay.jpg)

Installation
------------

[](#installation)

Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply add it to your `composer.json` file:

```
"jacky-zeng/omnipay-alipay": "1.2",

```

And run composer to update your dependencies:

```
$ composer update

```

Basic Usage
-----------

[](#basic-usage)

The following gateways are provided by this package:

GatewayDescription说明 LinksAlipay\_AopPageAlipay Page Gateway电脑网站支付 - new[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Aop-Page-Gateway) [Doc](https://doc.open.alipay.com/doc2/detail.htm?treeId=270&articleId=105901&docType=1)Alipay\_AopAppAlipay APP GatewayAPP支付 - new[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Aop-APP-Gateway) [Doc](https://doc.open.alipay.com/docs/doc.htm?treeId=204&articleId=105051&docType=1)Alipay\_AopF2FAlipay Face To Face Gateway当面付 - new[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Aop-Face-To-Face-Gateway) [Doc](https://doc.open.alipay.com/docs/doc.htm?treeId=194&articleId=105072&docType=1)Alipay\_AopWapAlipay WAP Gateway手机网站支付 - new[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Aop-WAP-Gateway) [Doc](https://doc.open.alipay.com/docs/doc.htm?treeId=203&articleId=105288&docType=1)Alipay\_LegacyAppAlipay Legacy APP GatewayAPP支付[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Legacy-APP-Gateway) [Doc](https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1)Alipay\_LegacyExpressAlipay Legacy Express Gateway即时到账[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Legacy-Express-Gateway) [Doc](https://doc.open.alipay.com/docs/doc.htm?treeId=108&articleId=103950&docType=1)Alipay\_LegacyWapAlipay Legacy WAP Gateway手机网站支付[Usage](https://github.com/lokielse/omnipay-alipay/wiki/Legacy-WAP-Gateway) [Doc](https://doc.open.alipay.com/docs/doc.htm?treeId=60&articleId=103564&docType=1)Usage
-----

[](#usage)

### Purchase (购买)

[](#purchase-购买)

```
    protected function baseOption($gateway)
    {
        $publicKey = config('config.AliPay.alipay_public_key');  //获取支付宝公钥
        $privateKey = config('config.AliPay.merchant_private_key');  //获取支付宝私钥
        $gateway->setSignType('RSA2'); // RSA/RSA2/MD5
        $gateway->setAppId(config('config.AliPay.app_id'));
        $gateway->setPrivateKey($privateKey);
        $gateway->setAlipayPublicKey($publicKey);
        return $gateway;
    }

    // alipay - h5
    public function aliPay(Request $request)
    {
        $order_number = array_get($request->all(), 'order_number', date('YmdHis') . mt_rand(1000, 9999));
        $total_amount = array_get($request->all(), 'total_amount');
        $gateway = Omnipay::create('Alipay_AopPage');
        $gateway = $this->baseOption($gateway);
        $gateway->setReturnUrl(config('config.AliPay.return_url')); //同步通知地址
        $gateway->setNotifyUrl(config('config.AliPay.notify_url')); //异步通知地址

        $response = $gateway->purchase()->setBizContent([
            'out_trade_no' => $order_number,
            'total_amount' => $total_amount / 100,
            'subject'      => '支付宝支付测试',
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
        ])->send();
        $redirectUrl = $response->getRedirectUrl();
        if ( !$redirectUrl ) return '支付宝调用失败';
        return ['pay_url' => $redirectUrl];
    }

```

For general usage instructions, please see the main [Omnipay](https://github.com/omnipay/omnipay)repository.

### Refund (退款)

[](#refund-退款)

```
    /**
     * 支付宝退款 (有密)
     * @param Request $request
     * @return mixed
     */
    public function aliRefund(Request $request)
    {
        $params = $request->all();
        $order_number = array_get($params, 'order_number');
        $out_trade_no = array_get($params, 'out_trade_no');
        $amount = array_get($params, 'amount');
        $gateway = Omnipay::create('Alipay_LegacyExpress');
        $gateway->setSignType(config('config.AliPay.sign_type'));
        $gateway->setReturnUrl(config('config.AliPay.return_url')); //同步通知地址
        $gateway->setNotifyUrl(config('config.AliPay.notify_url')); //异步通知地址
        $gateway->setSellerEmail(config('config.AliWebPay.seller_email'));
        $gateway->setPartner(config('config.AliWebPay.partner'));
        $gateway->setKey(config('config.AliWebPay.key'));
        $data = [
            'refund_date' => date('Y-m-d H:i:s'),
            "seller_user_id" => trim(config('config.AliWebPay.seller_id')),
            'batch_no' => $order_number,
            'batch_num' => 1,//退款笔数与refund_items数组中保持一致
            '_input_charset' => 'UTF-8',
            'refund_items' => [
                [
                    'out_trade_no' => $out_trade_no,
                    'amount' => $amount / 100.0,
                    'reason' => 'User_refund'
                ]
            ],
        ];
        $request = $gateway->refund($data);
        $response = $request->send();
        $url = $response->getRedirectUrl();
        return ['url' => $url]; //在浏览器中打开该地址输入密码进行退款
    }

    /**
     * 支付宝退款 （无密）
     * @param Request $request
     * @return mixed
     */
    public function aliRefundNoPwd(Request $request)
    {
        $params = $request->all();
        $order_number = array_get($params, 'order_number');
        $out_trade_no = array_get($params, 'out_trade_no');
        $amount = array_get($params, 'amount');
        $gateway = Omnipay::create('Alipay_LegacyExpress');
        $gateway->setSignType(config('config.AliPay.sign_type'));
        $gateway->setReturnUrl(config('config.AliPay.return_url')); //同步通知地址
        $gateway->setNotifyUrl(config('config.AliPay.notify_url')); //异步通知地址
        $gateway->setSellerEmail(config('config.AliWebPay.seller_email'));
        $gateway->setPartner(config('config.AliWebPay.partner'));
        $gateway->setKey(config('config.AliWebPay.key'));
        $data = [
            'refund_date' => date('Y-m-d H:i:s'),
            "seller_user_id" => trim(config('config.AliWebPay.seller_id')),
            'batch_no' => $order_number,
            'batch_num' => 1,//退款笔数与refund_items数组中保持一致
            '_input_charset' => 'UTF-8',
            'refund_items' => [
                [
                    'out_trade_no' => $out_trade_no,
                    'amount' => $amount / 100.0,
                    'reason' => 'User_refund'
                ]
            ],
        ];
        $request = $gateway->refundNoPwd($data);
        $response = $request->send();
        $url = $response->getRedirectUrl();
        $html = file_get_contents($url);
        $rs = $this->xmlToArray($html);
        if ($rs && array_get($rs, 'is_success') == 'T') {
            return ['msg' => '申请退款成功'];
        } else {
            if(array_get($rs, 'error') == 'DUPLICATE_BATCH_NO'){
                return '请勿重复申请 '. array_get($rs, 'error');
            }else{
                return '申请退款失败 '. array_get($rs, 'error');
            }
        }
    }

```

Pay Notify （支付回调）
-----------------

[](#pay-notify-支付回调)

```
    /**
     * 支付宝支付回调(同步/异步) 验签  并返回回调数据
     * @param $params
     * @return bool|mixed
     */
    private function verifyAndGetAliDataForPay($params)
    {
        $gateway = Omnipay::create('Alipay_AopApp');
        $publicKey = config('config.AliPay.alipay_public_key');  //获取支付宝公钥
        $privateKey = config('config.AliPay.merchant_private_key');  //获取支付宝私钥
        $gateway->setSignType(config('config.AliPay.sign_type')); // RSA/RSA2/MD5
        $gateway->setAppId(config('config.AliPay.app_id'));
        $gateway->setPrivateKey($privateKey);
        $gateway->setAlipayPublicKey($publicKey);
        try {
            $response = $gateway->completePurchase(['params' => $params])->send(); //验签
            $data = $response->getData();
            return $data;
        } catch (InvalidRequestException $ex) {
            \Log::info('支付宝支付回调失败:' . $ex->getMessage());
            return false;
        }
    }

```

Refund Notify （退款回调）
--------------------

[](#refund-notify-退款回调)

```
   /**
     * 支付宝退款回调异步 验签 并返回回调数据
     * @param $params
     * @return bool|mixed
     */
    private function verifyAndGetAliDataForRefund($params)
    {
        $gateway = Omnipay::create('Alipay_LegacyExpress');
        $gateway->setSignType(config('config.AliWebPay.sign_type')); // MD5
        $gateway->setPartner(config('config.AliWebPay.partner'));
        $gateway->setKey(config('config.AliWebPay.key'));
        try {
            $response = $gateway->completePurchase(['params' => $params])->send(); //验签
            $data = $response->getData();
            return $data;
        } catch (InvalidRequestException $ex) {
            \Log::info('支付宝退款回调失败:' . $ex->getMessage());
            return false;
        }
    }

```

Related
-------

[](#related)

- [Laravel-Omnipay](https://github.com/ignited/laravel-omnipay)
- [Omnipay-GlobalAlipay](https://github.com/lokielse/omnipay-global-alipay)
- [Omnipay-WechatPay](https://github.com/lokielse/omnipay-wechatpay)
- [Omnipay-UnionPay](https://github.com/lokielse/omnipay-unionpay)

Support
-------

[](#support)

If you believe you have found a bug, please email

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

3

Last Release

3072d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/10e4d9c58a92a87ce99cce8cf20832820a97ebb16a3f490c0e72d517912ccba3?d=identicon)[jacky-zeng](/maintainers/jacky-zeng)

---

Top Contributors

[![jacky-zeng](https://avatars.githubusercontent.com/u/26794054?v=4)](https://github.com/jacky-zeng "jacky-zeng (13 commits)")

### Embed Badge

![Health badge](/badges/jacky-zeng-omnipay-alipay/health.svg)

```
[![Health](https://phpackages.com/badges/jacky-zeng-omnipay-alipay/health.svg)](https://phpackages.com/packages/jacky-zeng-omnipay-alipay)
```

###  Alternatives

[league/omnipay

Omnipay payment processing library

6.1k9.7M166](/packages/league-omnipay)[silverstripe/silverstripe-omnipay

SilverStripe Omnipay Payment Module

38106.0k15](/packages/silverstripe-silverstripe-omnipay)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
