jacky-zeng/omnipay-alipay

There is no license information available for the latest version (v1.2) of this package.

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

v1.2 2017-12-14 01:24 UTC

This package is not auto-updated.

Last update: 2020-07-25 06:26:14 UTC


README

Donate

Installation

Omnipay is installed via Composer. 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

The following gateways are provided by this package:

Gateway Description 说明   Links
Alipay_AopPage Alipay Page Gateway 电脑网站支付 - new Usage Doc
Alipay_AopApp Alipay APP Gateway APP支付 - new Usage Doc
Alipay_AopF2F Alipay Face To Face Gateway 当面付 - new Usage Doc
Alipay_AopWap Alipay WAP Gateway 手机网站支付 - new Usage Doc
Alipay_LegacyApp Alipay Legacy APP Gateway APP支付 Usage Doc
Alipay_LegacyExpress Alipay Legacy Express Gateway 即时到账 Usage Doc
Alipay_LegacyWap Alipay Legacy WAP Gateway 手机网站支付 Usage Doc

Usage

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 repository.

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 (支付回调)

    /**
     * 支付宝支付回调(同步/异步) 验签  并返回回调数据
     * @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 (退款回调)

   /**
     * 支付宝退款回调异步 验签 并返回回调数据
     * @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

Support

If you believe you have found a bug, please email 1017798347@qq.com