<?php

namespace SRVX\Api\Controllers\Alipay;

use SRVX\Api\Controllers\ControllerBase;
use SRVX\Msg;
use SRVX\Service\Pay\AlipayServiceToTransfer;


class TransferController extends ControllerBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function indexAction()
    {
        try {
            $data = $this->request->getJsonRawBody(true);

            if ($data["config"]["pay_ali_use_type"] == "1") {//新接口
                $data["config"]["pay_ali_new_app_cert_sn"]          = base64_decode($data["config"]["pay_ali_new_app_cert_sn"]);
                $data["config"]["pay_ali_new_alipayrsa_public_key"] = base64_decode($data["config"]["pay_ali_new_alipayrsa_public_key"]);
            } else {
                $data["config"]["pay_ali_public_key"] = base64_decode($data["config"]["pay_ali_public_key"]);
            }

            $AlipayServiceToTransfer = new AlipayServiceToTransfer($data["config"]);
            $result                  = $AlipayServiceToTransfer->transfer($data["args"]);
            logger($result, 'alipay_Transfer_index', true);
            $this->api($result['data']);
        } catch (\Exception $exception) {
            $this->api(Msg::NewError(500, $exception->getMessage()));
        }
    }
}
