package com.fdkankan.pay.paypal.sdk;

import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public abstract class PayPalConfig {

    abstract public String getClientId();

    abstract public String getSecret();

    abstract public String getMode();

    @Bean
    public Map<String, String> paypalSdkConfig(){
        Map<String, String> sdkConfig = new HashMap<>();
        sdkConfig.put("mode", getMode());
        return sdkConfig;
    }

    @Bean
    public OAuthTokenCredential authTokenCredential(){
        return new OAuthTokenCredential(getClientId(), getSecret(), paypalSdkConfig());
    }

    @Bean
    public APIContext apiContext() throws PayPalRESTException {
        String accessToken = authTokenCredential().getAccessToken();
        log.info("paypal新的accessToken：" + accessToken);
        APIContext apiContext = new APIContext(accessToken);
        apiContext.setConfigurationMap(paypalSdkConfig());
        return apiContext;
    }
}
