使用Java集成oGone支付服务

时间:2011-03-22 16:57:22

标签: payment-gateway

我必须将“oGone支付服务”与Java集成。例如,用户需要通过“oGone支付服务”(如PayPal)购买一些商品。如何将我的所有参数传递给“oGone支付服务”。如果有人可以对此投掷一些火炬,那将会很有帮助

由于

2 个答案:

答案 0 :(得分:2)

只需按照ogone文档即可。如果你想使用直接链接我建议使用spring RestTemplate。

<!-- rest template -->
<bean class="org.springframework.web.client.RestTemplate" id="restTemplate">
    <property name="messageConverters">
        <list>
            <ref bean="formMessageConverter" />
            <ref bean="stringMessageConverter" />
        </list>
    </property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"
    id="stringMessageConverter"></bean>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"
    id="formMessageConverter"></bean>
<bean class="DirectLinkTemplateImpl" id="directLinkTemplate">
    <property name="restTemplate" ref="restTemplate"></property>
    <property name="url"
        value="https://secure.ogone.com/ncol/test/orderdirect.asp"></property>
    <property name="pspId" value="YOR PSP ID"></property>
    <property name="userId" value="userID"></property>
    <property name="pswd" value="pass"></property>
    <property name="shaPassphrase" value="shaPassphrase"></property>
</bean>

然后使用类似于以下内容的代码发布到ogone

    /**
     * This class abstracts the functionality of the OGONE direct link payment api making use of Springs {@link RestTemplate}
     * 
     * 
     * @author Kai Grabfelder (nospam@kaigrabfelder.de)
     *
     */
    public class DirectLinkTemplateImpl implements DirectLinkTemplate {

    private RestOperations restTemplate;

    private String shaPassphrase; 

    private String url;
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * affiliation name in ogone.
     */
    private String pspId;
    /**
     * Name of ogone application (API) user.
     */
    private String userId;
    /**
     * Password of the API user (USERID).
     */
    private String pswd;

    public RestOperations getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestOperations restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }



    /* (non-Javadoc)
     * @see DirectLinkTemplate#executeDirectLinkRequest(int, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void executeDirectLinkRequest(int amount, String currency,
            String orderId, String alias) {


        Map<String, String> request = new HashMap<String, String>();
        request.put("PSPID", getPspId());
        request.put("USERID", getUserId());
        request.put("PSWD", getPswd());
        request.put("AMOUNT", String.valueOf(amount));
        request.put("CURRENCY", currency);
        request.put("ORDERID", orderId);
        request.put("ALIAS", alias);
        request.put("ECI", "9"); //set the ECI parameter to 9 (recurring payment) - necessary according to ogone support

        request = cleanupRequestParameters(request);

        String shaSign = composeSHASIGNParameter(request);
        request.put("SHASIGN", shaSign);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String,String>>(createMultiValueMap(request), headers );


        String response = restTemplate.postForObject(getUrl(), entity, String.class);

        //TODO validate response
        System.out.println(response);



    }

    private MultiValueMap<String, String> createMultiValueMap(Map<String, String> map){
        map = new TreeMap<String, String>(map);
        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<String, String>();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            multiValueMap.add(key, map.get(key));
        }
        return multiValueMap;
    }


    /**
     * cleanup the request parameters by removing all parameters with an empty value and converting all parameter names to upper case
     * 
     * @param request
     * @return 
     */
    protected Map<String, String> cleanupRequestParameters(Map<String, String> request){

        Map<String, String> map = new HashMap<String, String>();

        Set<String> keys = request.keySet();
        for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            String value = request.get(key);
            if (StringUtils.hasText(value)) map.put(key.toUpperCase(), value);
        }

        return map ;
    }



    /**
     * create the SHASign parameter according to the specification of ogone: sorts all keys alphabetically,
     * @param request
     * @return
     */
    protected String composeSHASIGNParameter(Map<String, String> request){

        //create a map with alphabetically sorted keys
        TreeMap<String, String> sortedMap = new TreeMap<String, String>(request);

        Set<String> keys = sortedMap.keySet();
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key).append("=").append(request.get(key));
            sb.append(getShaPassphrase());
        }

        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] digest = md.digest(sb.toString().getBytes());

        String encodedString = new String(Hex.encodeHex(digest));
        return encodedString;
    }

    public String getShaPassphrase() {
        return shaPassphrase;
    }

    public void setShaPassphrase(String shaPassphrase) {
        this.shaPassphrase = shaPassphrase;
    }

    public String getPspId() {
        return pspId;
    }

    public void setPspId(String pspId) {
        this.pspId = pspId;
    }

}

我还没有完成实现,但至少“post to ogone”部分已经完成了。完成并清理后,我会尝试在我的博客中发布完整的DirectLinkTemplate。

答案 1 :(得分:0)

我不确定。它可能工作与否jOgone。为Ogone编写的java库