lokielse/omnipay-alipay

<?php

namespace app\common\util;

use Omnipay\Omnipay;
use app\common\model\OrderInfo as OrderInfoModel;
use app\common\model\OrderAction as OrderActionModel;

class AliPay {

    protected $appId;
    protected $signType;//加密方式
    protected $privateKey;
    protected $publicKey;
    protected $notifyUrl;//异步通知

    public function __construct() {
        $this->appId = config('alipay.alipay_appid');
        $this->signType = config('alipay.alipay_encrypt');//RSA/RSA2
        $this->privateKey = config('alipay.alipay_private_key');
        $this->publicKey = config('alipay.alipay_public_key');
        $this->notifyUrl = config('alipay.alipay_callback');
    }

    /**
     * app支付
     * @param array $content
     * @return mixed
     * @throws \Exception
     */
    public function App($content){
        $gateway = $this->getGateway();

        /* 下订单 */
        $request = $gateway->purchase($content);
        /* AopTradeAppPayResponse $response */
        $response = $request->send();
        if(!$response->isSuccessful()){
            throw new \Exception($response->getOrderString());
        }
        $orderString = $response->getOrderString();
        return $orderString;
    }

    /**
     * 异步通知
     */
    public function notify(){
        $gateway = $this->getGateway();

        $arr = request()->post();
        file_put_contents(__DIR__.'alipay.txt', var_export($arr, true), FILE_APPEND);
        $request = $gateway->completePurchase($arr);

        $where = [];
        $where['code'] = $arr['out_trade_no'];
        $orderInfoModel = new OrderInfoModel();
        $orderInfo = $orderInfoModel->verify($where);
        if($orderInfo['pay_status'] == 2){
            return ;
        }

        $orderActionModel = new OrderActionModel();
        $actionData = [];
        $actionData['order_id'] = $orderInfo['id'];
        $actionData['order_code'] = $orderInfo['code'];
        $actionData['order_status'] = $orderInfo['order_status'];
        $actionData['shipping_status'] = $orderInfo['shipping_status'];
        $actionData['log_time'] = time();
        /**
         * @var AopCompletePurchaseResponse $response
         */
        try {
            $response = $request->send();

            if($response->isPaid()){
                /**
                 * Payment is successful
                 */
                $data = [];
                $data['pay_status'] = 2;//已付款
                $data['composition_status'] = 3;//待发货
                $data['online_money'] = $arr['total_amount'];//线上付款金额
                $data['pay_time'] = time();//付款时间
                $res = $orderInfoModel::update($data, ['id' => $orderInfo['id']]);

                $actionData['pay_status'] = 2;//已付款
                if($res === false){
                    $actionData['action_note'] = '支付宝支付成功,更改订单状态失败';
                }else{
                    $actionData['action_note'] = '支付宝支付成功,更改订单状态成功';
                }
            }else{
                /**
                 * Payment is not successful
                 */
                $actionData['pay_status'] = 1;//付款中
                $actionData['action_note'] = '支付宝支付失败';
            }
            $orderActionModel->create($actionData);
        } catch (\Exception $e) {
            /**
             * Payment is not successful
             */
            $actionData['pay_status'] = 1;//付款中
            $actionData['action_note'] = $e->getMessage();
            $orderActionModel->create($actionData);
        }
    }

    private function getGateway(){
        /* AopAppGateway $gateway */
        $gateway = Omnipay::create('Alipay_AopApp');
        $gateway->setSignType($this->signType);
        $gateway->setAppId($this->appId);
        $gateway->setPrivateKey($this->privateKey);
        $gateway->setAlipayPublicKey($this->publicKey);
        $gateway->setNotifyUrl($this->notifyUrl);
        return $gateway;
    }
}

 

转载于:https://my.oschina.net/u/1422143/blog/1608562

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值