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;
    }
}
本站公眾號
   歡迎關注本站公眾號,獲取更多信息