将 oGone 支付服务与 Java 集成 [英] integration oGone Payment service with Java

查看:43
本文介绍了将 oGone 支付服务与 Java 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to integrate "oGone Payment service" with Java. For example, User need to buy some items through "oGone Payment service" ( like PayPal). How to pass all my parameters to "oGone Payment service".If anybody could throw some torch on this, it will be helpful

谢谢

推荐答案

只需按照 ogone 文档进行操作.如果你想使用直接链接,我建议使用 spring RestTemplate.

just follow the ogone documentation. If you want to use direct link I suggest to use the 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>

然后用类似下面的代码来post到ogone

Then use code similar to the following to post to 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.

I'm not yet finished with the implementation but at least the "post to ogone" part works already. I'll try to post the complete DirectLinkTemplate in my blog once it is finished and cleaned up.

这篇关于将 oGone 支付服务与 Java 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆