<commit_before>package com.egzosn.pay.payoneer.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.api.BasePayService;
import com.egzosn.pay.common.api.Callback;
import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.bean.*;
import com.egzosn.pay.common.bean.outbuilder.PayTextOutMessage;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.http.HttpHeader;
import com.egzosn.pay.common.http.HttpStringEntity;
import com.egzosn.pay.payoneer.bean.PayoneerTransactionType;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * payoneer业务逻辑
 *
 * @author Actinia
 *         <pre>
 *         email <EMAIL>
 *         date 2018-01-19
 *         </pre>
 */
public class PayoneerPayService extends BasePayService implements AdvancedPayService {
    /**
     * 测试地址
     */
    public final static String SANDBOX_DOMAIN = "https://api.sandbox.payoneer.com/v2/programs/";
    /**
     * 正式地址
     */
    public final static String RELEASE_DOMAIN = "https://api.payoneer.com/v2/programs/";
    /**
     * 响应状态码
     */
    public final static String CODE = "code";
    /**
     * 响应状态码
     */
    private final static String OUT_TRADE_NO = "client_reference_id";


    public PayoneerPayService(PayConfigStorage payConfigStorage) {
        super(payConfigStorage);
    }

    public PayoneerPayService(PayConfigStorage payConfigStorage, HttpConfigStorage configStorage) {
        super(payConfigStorage, configStorage);
    }

    /**
     * 获取授权页面
     *
     * @param payeeId 收款id
     *
     * @return 返回请求结果
     */
    @Override
    public String getAuthorizationPage(String payeeId) {

        HttpStringEntity entity = new HttpStringEntity("{\"payee_id\":\"" + payeeId + "\"}", ContentType.APPLICATION_JSON);
        JSONObject response = getHttpRequestTemplate().postForObject(getReqUrl(PayoneerTransactionType.registration), entity, JSONObject.class);
        if (response != null && 0 == response.getIntValue(CODE)) {
            return response.getString("registration_link");
        }
        throw new PayErrorException(new PayException("fail", "Payoneer获取授权页面失败,原因:" + response.getString("hint"), response.toJSONString()));
    }

    /**
     * 回调校验
     *
     * @param params 回调回来的参数集
     *
     * @return 签名校验 true通过
     */
    @Override
    public boolean verify(Map<String, Object> params) {
        if (params != null && 0 == Integer.parseInt(params.get(CODE).toString())) {
            if (params.containsKey(OUT_TRADE_NO)){
                return verifySource((String) params.get(OUT_TRADE_NO));
            }
        }
        return true;
    }

    /**
     * 签名校验
     *
     * @param params 参数集
     * @param sign   签名原文
     *<commit_msg>增加订单付款后的校验<commit_after>package com.egzosn.pay.payoneer.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.api.BasePayService;
import com.egzosn.pay.common.api.Callback;
import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.bean.*;
import com.egzosn.pay.common.bean.outbuilder.PayTextOutMessage;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.http.HttpHeader;
import com.egzosn.pay.common.http.HttpStringEntity;
import com.egzosn.pay.payoneer.bean.PayoneerTransactionType;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * payoneer业务逻辑
 *
 * @author Actinia
 *         <pre>
 *         email <EMAIL>
 *         date 2018-01-19
 *         </pre>
 */
public class PayoneerPayService extends BasePayService implements AdvancedPayService {
    /**
     * 测试地址
     */
    public final static String SANDBOX_DOMAIN = "https://api.sandbox.payoneer.com/v2/programs/";
    /**
     * 正式地址
     */
    public final static String RELEASE_DOMAIN = "https://api.payoneer.com/v2/programs/";
    /**
     * 响应状态码
     */
    public final static String CODE = "code";
    /**
     * 响应状态码
     */
    private final static String OUT_TRADE_NO = "client_reference_id";


    public PayoneerPayService(PayConfigStorage payConfigStorage) {
        super(payConfigStorage);
    }

    public PayoneerPayService(PayConfigStorage payConfigStorage, HttpConfigStorage configStorage) {
        super(payConfigStorage, configStorage);
    }

    /**
     * 获取授权页面
     *
     * @param payeeId 收款id
     *
     * @return 返回请求结果
     */
    @Override
    public String getAuthorizationPage(String payeeId) {

        HttpStringEntity entity = new HttpStringEntity("{\"payee_id\":\"" + payeeId + "\"}", ContentType.APPLICATION_JSON);
        JSONObject response = getHttpRequestTemplate().postForObject(getReqUrl(PayoneerTransactionType.registration), entity, JSONObject.class);
        if (response != null && 0 == response.getIntValue(CODE)) {
            return response.getString("registration_link");
        }
        throw new PayErrorException(new PayException("fail", "Payoneer获取授权页面失败,原因:" + response.getString("hint"), response.toJSONString()));
    }

    /**
     * 回调校验
     *
     * @param params 回调回来的参数集
     *
     * @return 签名校验 true通过
     */
    @Override
    public boolean verify(Map<String, Object> params) {
        if (params != null && 0 == Integer.parseInt(params.get(CODE).toString())) {
            if (params.containsKey(OUT_TRADE_NO)){
                return verifySource((String) params.get(OUT_TRADE_NO));
            }
            return true;
        }
        return false;
    }

    /**
     * 签名校验
     *
     * @param params 参数集
     * @param sign   签名原文
     *
{'fraction_non_alphanumeric': 0.11928078343233264, 'fraction_numerical': 0.0038529458982180125, 'mean_word_length': 3.0064308681672025, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1377334', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2114, 'n_words': 336}