<?php

namespace common\helpers;

class PayStack
{
    /**
     * 根据运行环境加载secretKey
     */
    public static function getSecretKey(): string
    {
         return YII_ENV == YII_ENV_PROD ? \Yii::$app->Paystack->liveSecretKey : \Yii::$app->Paystack->testSecretKey;
//       return \Yii::$app->Paystack->testSecretKey;
    }

    /**
     * 支付.
     */
    public static function transactionInit($currency, $amount, $email)
    {
        $secretKey = self::getSecretKey();

        LoggerTool::info('env: ' . YII_ENV);

        if (YII_ENV != YII_ENV_PROD) {
            return [
                'status' => true,
                'data' => [
                    'amount' => $amount
                ]
            ];
        }

        try {
            $url = "https://api.paystack.co/transaction/initialize";
            $fields = [
                'email' => $email,
                'amount' => $amount * 100
            ];
            $fields_string = http_build_query($fields);
            //open connection
            $curl = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($curl,CURLOPT_URL, $url);
            curl_setopt($curl,CURLOPT_POST, true);
            curl_setopt($curl,CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                "Authorization: Bearer {$secretKey}",
                "Cache-Control: no-cache",
            ));
            //So that curl_exec returns the contents of the cURL; rather than echoing it
            curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
            //execute post
            $result = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            if ($err) {
                return [
                    'status'    => false,
                    'message'   => $err,
                ];
            }

            return json_decode($result, true);
        } catch (\Exception $e) {
            return [
                'status'    => false,
                'message'   => $e->getMessage(),
            ];
        }
    }
    /**
     * 交易结果校验.
     * @param $ref
     * @return array
     */
    public static function transactionVerify($ref): array
    {
        $secretKey = self::getSecretKey();

        try {
            $curl = curl_init();
            curl_setopt_array($curl, [
                CURLOPT_URL => "https://api.paystack.co/transaction/verify/{$ref}",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Bearer {$secretKey}",
                    "Cache-Control: no-cache",
                ),
            ]);

            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);

            if ($err) {
                return [
                    'status'    => false,
                    'message'   => $err,
                ];
            }

            return json_decode($response, true);
        } catch (\Exception $e) {
            return [
                'status'    => false,
                'message'   => $e->getMessage(),
            ];
        }
    }

    /**
     * 交易退款.
     * @param string $reference
     * @param int $amount
     * @return array
     */
    public static function transactionRefund(string $reference, int $amount)
    {
        $secretKey = self::getSecretKey();

        try {
            $url = "https://api.paystack.co/refund";
            $fields = [
                'transaction' => $reference,
                'amount'      => $amount,
            ];
            $fields_string = http_build_query($fields);
            //open connection
            $curl = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                "Authorization: Bearer {$secretKey}",
                "Cache-Control: no-cache",
            ));

            //So that curl_exec returns the contents of the cURL; rather than echoing it
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            //execute post
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);

            if ($err) {
                return [
                    'status'    => false,
                    'message'   => $err,
                ];
            }

            return json_decode($response, true);
        } catch (\Exception $e) {
            return [
                'status'    => false,
                'message'   => $e->getMessage(),
            ];
        }
    }
}