PayPal支付接口方式(checkout)集成

1.简述

  PayPal是倍受全球亿万用户追捧的国际贸易支付工具,即时支付,即时到账,全中文操作界面,能通过中国的本地银行轻松提现,解决外贸收款难题,助您成功开展海外业务,决胜全球。注册PayPal后就可立即开始接受信用卡付款。作为在线付款服务商,PayPal是您向全世界近2.54亿的用户敞开大门的最快捷的方式。 最大的好处是,注册完全免费!集国际流行的信用卡,借记卡,电子支票等支付方式于一身。帮助买卖双方解决各种交易过程中的支付难题。PayPal是名副其实的全球化支付平台, 服务范围超过200个市场, 支持的币种超过100个。在跨国交易中, 将近70%的在线跨境买家更喜欢用PayPal支付海外购物款项。

  PayPal提供了多种支付方式

  • 标准支付
  • 快速支付
  • 其中标准支付誉为最佳实践。

  注意:paypal支付国内账号不能付款给国内账号

2.PayPal的相关URL

  正式网址https://www.paypal.com/ 

  沙箱(开发者)网址https://developer.paypal.com/

  沙箱(测试用户)登录地址https://www.sandbox.paypal.com/

  demo地址https://demo.paypal.com/c2/demo/code_samples

  webHooks验证(调用API)https://developer.paypal.com/docs/api/webhooks/v1#verify-webhook-signature

官方文档

3.PayPal Checkout集成步骤

  1、整合Smart Payment Buttons(PayPal智能付款按钮)到页面

  2、用户点击支付按钮

  3、按钮调用PayPal Orders API来创建交易

  4、进入PayPal支付页面

  5、用户登录后确认支付

  6、按钮调用PayPal Orders API来完成交易

  7、显示支付成功信息

4.PayPal Checkout集成步骤实现

(1)注册账号

  在PayPal正式网站https://www.paypal.com中注册一个账号,如果公司没有给你相关信息的话,先注册一个个人账号也是一样的。

(2)进入开发者界面创建相关信息

  1、在开发者平台https://developer.paypal.com/,登录刚创建的账号

 

  2、登录成功后,选择:SANBOX下的Accounts标签

 

  3、创建个人账号和商家账号用于测试沙箱环境

 

  4、创建APP获取client id和secret

 

  点击Create app创建

 

 

 

(3)示例代码

  控制器代码如下

@Controller
@RequestMapping("/paypalC")
public class PaypalC {

    @Autowired
    private PaypalS paypalS;

    @RequestMapping(method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    
    /**创建订单
     */
    @RequestMapping(method = RequestMethod.POST, value = "createOrder")
    public void createOrder(HttpServletRequest req, HttpServletResponse resp){
        paypalS.createOrder(req, resp);
    }

    /**执行付款
     */
    @RequestMapping(method = RequestMethod.POST, value = "executePayment")
    public void executePayment(HttpServletRequest req, HttpServletResponse resp){
        paypalS.executePayment(req, resp);
    }

    /**交易取消
     */
    @RequestMapping(method = RequestMethod.GET, value = "cancel")
    public String cancel(){
        return "cancel";
    }

    /**交易成功返回页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "success")
    public String success(HttpServletRequest req, HttpServletResponse resp, String orderId){
        req.setAttribute("orderId", orderId);
        return "success";
    }
    
    /**查询交易详情
     */
    @RequestMapping(method = RequestMethod.POST, value = "paymentDetails")
    public void paymentDetails(HttpServletRequest req, HttpServletResponse resp, String orderId){
        paypalS.paymentDetails(req, resp, orderId);
    }
}
View Code

  业务层代码如下

/**Paypal支付service类
 */
@Service
public class PaypalS {
    /**创建订单
     */
    public void createOrder(HttpServletRequest req, HttpServletResponse resp){
        //1.获取paypal的token
        String accessToken = PaypalUtils.getAccessToken();
        //2.提交交易到paypal
        String BaseUrl = req.getScheme() + "://"+ req.getServerName() + ((req.getServerPort() == 80) ? "" : ":" + req.getServerPort()) + req.getContextPath();
        String result = PaypalUtils.createOrder(req, accessToken, BaseUrl);
        resp.setContentType("application/json");
        try {
            PrintWriter out = resp.getWriter();
            out.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**执行付款
     */
    public void executePayment(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("application/json");
        try {
            String jsonFromHtml = readInputStreamForData(req); // holding the json from request
            JSONObject json = JSONObject.parseObject(jsonFromHtml);
            String orderId = json.getString("orderId");//付款id
            String accessToken = PaypalUtils.getAccessToken();
            String result = PaypalUtils.executePayment(req, accessToken, orderId);
            PrintWriter out = resp.getWriter();
            out.print(result);
        }catch(Exception e) {
            e.printStackTrace();
            try {
                PrintWriter out = resp.getWriter();
                out.print(new AjaxDto(0, "支付失败,请联系管理员...", req).toJson());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    
    /**查询交易详情
     */
    public void paymentDetails(HttpServletRequest req, HttpServletResponse resp,String orderId){
        try {
            String accessToken = PaypalUtils.getAccessToken();
            JSONObject dataFromGetPaymentsAPI = PaypalUtils.getPaymentDetails(req, accessToken, orderId);
            resp.setContentType("application/json");
            resp.setStatus(200);
            PrintWriter out = resp.getWriter();
            out.print(dataFromGetPaymentsAPI);
        } catch (Exception e) {
            resp.setStatus(500);
            resp.setContentType("application/json");
            try {
                PrintWriter out = resp.getWriter();
                Map<String, String> error = new HashMap<String, String>();
                error.put("error", "获取交易详情失败,请联系管理员!!!");
                out.print(error);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    
    /**获取req传递参数
     */
    private String readInputStreamForData(HttpServletRequest req) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
        String json = "";
        if(br != null) {
            json = br.readLine();
        }
        return json;
    }
}
View Code

  PayPal工具类如下

public class PaypalUtils {
    private static Log log = LogFactory.getLog(PaypalUtils.class);
    /**true表示测试环境(沙盒),false:正式环境
     */
    private static boolean IS_APPLICATION_IN_SANDBOX = true;
    //TODO 测试环境使用
    /**沙盒帐户详细信息-卖方
     */
    private static String SELLAR_SANDBOX_EMAIL = "";//邮箱
    private static String SELLAR_SANDBOX_PASSWORD = "";//密码
    private static String SELLAR_SANDBOX_SIGNATURE = "";//标签
    /**App Client ID 和 SECRET
     */
    private static String CLIENT_ID = "";//Client ID
    private static String SECRET = "";//SECRET
    
    private static String ACCESS_TOKEN_URL = "https://api.sandbox.paypal.com/v1/oauth2/token";//获取token接口
    private static String CREATE_PAYMENT_URL = "https://api.sandbox.paypal.com/v2/checkout/orders";
    private static String EXECUTE_PAYMENT_URL = "https://api.sandbox.paypal.com/v2/checkout/orders/{id}/capture";
    private static String GET_PAYMENT_DETAILS = "https://api.sandbox.paypal.com/v2/checkout/orders/{id}";
    
    
    //TODO 正式环境使用
    /**真实帐户详细信息-卖方
     */
    private static String SELLAR_LIVE_EMAIL = "";//邮箱
    private static String SELLAR_LIVE_PASSWORD = "";//密码
    private static String SELLAR_LIVE_SIGNATURE = "";//标签
    /**App Client ID 和 SECRET
     */
    private static String CLIENT_ID_LIVE = "";//Client ID
    private static String SECRET_LIVE = "";//SECRET
    
    private static String ACCESS_TOKEN_URL_LIVE = "https://api.paypal.com/v1/oauth2/token";//获取token接口
    private static String CREATE_PAYMENT_URL_LIVE = "https://api.paypal.com/v2/checkout/orders";
    private static String EXECUTE_PAYMENT_URL_LIVE = "https://api.paypal.com/v2/checkout/orders/{id}/capture";
    private static String GET_PAYMENT_DETAILS_LIVE = "https://api.paypal.com/v2/checkout/orders/{id}";
    
    private static String CANCEL_URL= "paypalC/cancel";//交易失败页面
    private static String RETURN_URL= "paypalC/success";//交易成功页面
    
    /**初始化配置实体
     */
    public static PaypalConfigBean paypalConfigBean = new PaypalConfigBean();
    static{
        if(IS_APPLICATION_IN_SANDBOX) {
            // load all properties for sandbox
            paypalConfigBean.setAccessTokenUrl(ACCESS_TOKEN_URL);
            paypalConfigBean.setClientId(CLIENT_ID);
            paypalConfigBean.setCreatePaymentsUrl(CREATE_PAYMENT_URL);
            paypalConfigBean.setExecutePaymentsUrl(EXECUTE_PAYMENT_URL);
            paypalConfigBean.setGetPaymentsDetailsUrl(GET_PAYMENT_DETAILS);
            paypalConfigBean.setSecret(SECRET);
        }else {
            // load all properties for live
            paypalConfigBean.setAccessTokenUrl(ACCESS_TOKEN_URL_LIVE);
            paypalConfigBean.setClientId(CLIENT_ID_LIVE);
            paypalConfigBean.setCreatePaymentsUrl(CREATE_PAYMENT_URL_LIVE);
            paypalConfigBean.setExecutePaymentsUrl(EXECUTE_PAYMENT_URL_LIVE);
            paypalConfigBean.setGetPaymentsDetailsUrl(GET_PAYMENT_DETAILS_LIVE);
            paypalConfigBean.setSecret(SECRET_LIVE);
        }
        paypalConfigBean.setCancelUrl(CANCEL_URL);
        paypalConfigBean.setReturnUrl(RETURN_URL);
    }
    
    
    //TODO paypal请求处理
    /**获取paypal的Token
     */
    public static String getAccessToken() {
        Map<String, String> headers = new HashMap<String, String>();
        String token = getBasicBearerToken(paypalConfigBean.getClientId(), paypalConfigBean.getSecret());
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("accept-language", "en_US");
        headers.put("Authorization", token);
        String param = "grant_type=client_credentials";
        try {
            Map<String, String> data = HttpsUtils.doPost(headers, param, paypalConfigBean.getAccessTokenUrl());
            if(!OUtils.isEmpty(data.get("statusCode")) && data.get("statusCode").startsWith("2")){
                JSONObject jsonObj = JSONObject.parseObject(data.get("data"));
                return jsonObj.getString("access_token");
            }else{
                log.error("paypal的token获取失败,参数:"+token+",返回结果:"+data);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return "";
    }
    
    /**创建订单
     */
    public static String createOrder(HttpServletRequest req, String accessToken, String BaseUrl){
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("accept-language", "en_US");
        headers.put("authorization", "Bearer "+accessToken);
        
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//订单信息
        Map<String, Object> order = new HashMap<String, Object>();
        Map<String, String> orderInfo = new HashMap<String, String>();
        orderInfo.put("currency_code", "USD");//支付货币
        orderInfo.put("value", "100.00");//支付金额
        order.put("reference_id","PUHF");//订单编号,多个订单时使用
        order.put("amount", orderInfo);
        list.add(order);
        
        Map<String, String> redirects = new HashMap<String, String>();
        redirects.put("return_url", BaseUrl+"/"+paypalConfigBean.getReturnUrl());//付款成功返回地址
        redirects.put("cancel_url", BaseUrl+"/"+paypalConfigBean.getCancelUrl());//付款失败返回地址
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("intent", "CAPTURE");//创建付款
        params.put("purchase_units", list);//订单信息
        params.put("application_context", redirects);//返回地址,无效的地址
        
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String param = JSONObject.toJSONString(params);
            Map<String, String> data = HttpsUtils.doPost(headers, param, paypalConfigBean.getCreatePaymentsUrl());
            if(!OUtils.isEmpty(data.get("statusCode")) && data.get("statusCode").startsWith("2")){
                JSONObject jsonObj = JSONObject.parseObject(data.get("data"));
                result.put("id", jsonObj.getString("id"));
            }else{
                log.error("paypal创建订单失败,token:"+accessToken+",参数:"+param+",返回结果:"+data);
                return new AjaxDto(0, "创建订单失败,请联系管理员...", req).toJson();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return new AjaxDto(1, null, result).toJson();
    }
    
    /**执行paypal付款
     */
    public static String executePayment(HttpServletRequest req, String accessToken,  String orderId) throws Exception {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer "+accessToken);
        String url = paypalConfigBean.getExecutePaymentsUrl().trim();
        url = url.replace("{id}", orderId);
        
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Map<String, String> data = HttpsUtils.doPost(headers, null, url);
            if(!OUtils.isEmpty(data.get("statusCode")) && data.get("statusCode").startsWith("2")){
                JSONObject jsonObj = JSONObject.parseObject(data.get("data"));
                result.put("id", jsonObj.getString("id"));
            }else{
                log.error("paypal支付失败,token:"+accessToken+",返回结果:"+data);
                return new AjaxDto(0, "支付失败,请联系管理员...", req).toJson();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxDto(1, null, result).toJson();
    }
    
    /**获取付款详情
     */
    public static JSONObject getPaymentDetails(HttpServletRequest req, String accessToken, String orderId) throws Exception {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer "+accessToken);
        String url = paypalConfigBean.getGetPaymentsDetailsUrl().trim();
        url = url.replace("{id}", orderId);
        String data = HttpsUtils.doGet(headers, url);
        if(!OUtils.isEmpty(data)){
            JSONObject jsonObj = JSONObject.parseObject(data);
            return jsonObj;
        }
        return null;
    }
    
    /**把paypal的clientId、secret转为Base64
     */
    private static String getBasicBearerToken(String clientId, String secret) {
        String token = clientId.toString().trim() +":"+secret.toString().trim();
        token = token.replace("\"", "");
        Base64 b = new Base64();
        String accessToken = b.encodeAsString(new String(token).getBytes());
        return "Basic "+ accessToken;
    }
    
    /**paypal返回的错误
     */
    private static String getPaypalError(String statusCode,String errorCode){
        return "";
    }
}
View Code

  HttpUtils工具类(httpclient-4.5.jar、httpcore-4.4.1.jar)如下

public class HttpsUtils {
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static SSLConnectionSocketFactory sslsf = null;
    private static PoolingHttpClientConnectionManager cm = null;
    private static SSLContextBuilder builder = null;
    static {
        try {
            builder = new SSLContextBuilder();
            // 全部信任 不做身份鉴定
            builder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            });
            sslsf = new SSLConnectionSocketFactory(builder.build(), new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);// max connection
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * httpClient post请求
     * 
     * @param url
     *            请求url
     * @param header
     *            头部信息
     * @param param
     *            请求参数 form提交适用
     * @param entity
     *            请求实体 json/xml提交适用
     * @return 可能为空 需要处理
     * @throws Exception
     * 
     */
    public static String doGet(String url) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();// 设置请求和传输超时时间
            httpGet.setConfig(requestConfig);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity);
            } else {
                readHttpResponse(httpResponse);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }

    /**
     * httpClient post请求
     * 
     * @param url
     *            请求url
     * @param header
     *            头部信息
     * @param param
     *            请求参数 form提交适用
     * @param entity
     *            请求实体 json/xml提交适用
     * @return 可能为空 需要处理
     * @throws Exception
     * 
     */
    public static String doPost(String url, Map<String, String> header, Map<String, String> param, HttpEntity entity) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpPost httpPost = new HttpPost(url);
            // 设置头信息
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            // 设置请求参数
            if (MapUtils.isNotEmpty(param)) {
                List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> entry : param.entrySet()) {
                    // 给参数赋值
                    formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
                httpPost.setEntity(urlEncodedFormEntity);
            }
            // 设置实体 优先级高
            if (entity != null) {
                httpPost.setEntity(entity);
            }
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity);
            } else {
                readHttpResponse(httpResponse);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }
    
    public static String doGet(Map<String, String> header, String url) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpGet httpGet = new HttpGet(url);
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();// ��������ʹ��䳬ʱʱ��
            httpGet.setConfig(requestConfig);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity);
            } else {
                readHttpResponse(httpResponse);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }
    
    public static Map<String, String> doPost(Map<String, String> header, String param, String url) throws Exception {
        Map<String, String> data = new HashMap<String, String>();
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();
            HttpPost httpPost = new HttpPost(url);
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            if (!OUtils.isEmpty(param)) {
                httpPost.setEntity(new StringEntity(param));
            }
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity resEntity = httpResponse.getEntity();
            data.put("statusCode", String.valueOf(httpResponse.getStatusLine().getStatusCode()));
            data.put("data", EntityUtils.toString(resEntity));
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return data;
    }

    public static CloseableHttpClient getHttpClient() throws Exception {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm).setConnectionManagerShared(true).build();

        return httpClient;
    }

    public static String readHttpResponse(HttpResponse httpResponse) throws ParseException, IOException {
        StringBuilder builder = new StringBuilder();
        // 获取响应消息实体
        HttpEntity entity = httpResponse.getEntity();
        // 响应状态
        builder.append("status:" + httpResponse.getStatusLine());
        builder.append("headers:");
        HeaderIterator iterator = httpResponse.headerIterator();
        while (iterator.hasNext()) {
            builder.append("\t" + iterator.next());
        }
        // 判断响应实体是否为空
        if (entity != null) {
            String responseString = EntityUtils.toString(entity);
            builder.append("response length:" + responseString.length());
            builder.append("response content:" + responseString.replace("\r\n", ""));
        }
        return builder.toString();
    }
}
View Code

  cancel.html代码如下

<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <h1>用户取消支付...</h1>
</body>
</html>
View Code

  index.html代码如下:

<div style="width:20px;" id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&commit=false"></script><!-- client-id正式环境需要改,测试环境才用sb -->
 
<script>
 
paypal.Buttons({
 
    env: 'sandbox', /* sandbox | production */
    style: {
                layout: 'horizontal',   // 布局方式:vertical: 垂直,horizontal:水平,
                size:   'responsive',   /* medium | large | responsive*/
                shape:  'pill',         /* pill | rect*/
                color:  'gold',         /* gold | blue | silver | black*/
                label: 'paypal'
            }, 
     commit: false, // Show a 'Pay Now' button
      /* createOrder() is called when the button is clicked */
 
    createOrder: function() {
        /* Set up a url on your server to create the order */
        var CREATE_URL = '<%=request.getContextPath() %>/paypalC/createOrder';
        /* Make a call to your server to set up the payment */
        return fetch(CREATE_URL,{
            method: 'post'
         }).then(function(res) {
          return res.json();
         }).then(function(data) {
          if(data.result == 1)
              return data.obj.id;
          else{
              alert(data.msg);
          }
         });
 
    },
    /* onApprove() is called when the buyer approves the payment */
    onApprove: function(data, actions) {
        /* Set up a url on your server to execute the payment */
        var EXECUTE_URL = '<%=request.getContextPath() %>/paypalC/executePayment';
        /* Set up the data you need to pass to your server */
        /* Make a call to your server to execute the payment */
        return fetch(EXECUTE_URL, {
         method: 'post',
         body: JSON.stringify({
          orderId: data.orderID
         })
        }).then(function(res) {
         return res.json();
        }).then(function(data) {
            if(data.result == 1)
                window.location.href='<%=request.getContextPath() %>/paypalC/success?orderId='+data.obj.id;
            else{
                  alert(data.msg);
            }
        });
 
    },onCancel: function() {
        return window.location.href='<%=request.getContextPath() %>/paypalC/cancel';
    }
 
}).render('#paypal-button-container');
 
</script>
View Code

  success.html代码如下

<html>
  <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>PayPal Credit</title>
      <!--Including Bootstrap style files-->
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
      <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
      <style>
          /* http://angrytools.com/gradient/ */
          .bg-color {
              color: white;
              background: -moz-linear-gradient(0deg, #004094 0%, #0096D9 50%, #004094 100%); /* ff3.6+ */
              background: -webkit-gradient(linear, left top, right top, color-stop(0%, #004094), color-stop(50%, #0096D9), color-stop(100%, #004094)); /* safari4+,chrome */
              background: -webkit-linear-gradient(0deg, #004094 0%, #0096D9 50%, #004094 100%); /* safari5.1+,chrome10+ */
              background: -o-linear-gradient(0deg, #004094 0%, #0096D9 50%, #004094 100%); /* opera 11.10+ */
              background: -ms-linear-gradient(0deg, #004094 0%, #0096D9 50%, #004094 100%); /* ie10+ */
              background: linear-gradient(90deg, #004094 0%, #0096D9 50%, #004094 100%); /* w3c */
              filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#004094', endColorstr='#004094',GradientType=1 ); /* ie6-9 */
          }
          .mark{
              max-width: 45%;
          }
          .mark-input-field{
              height:30px !important;
          }
      </style>
  </head>
  <body>
      <div class="container-fluid">
          <div class="row">
                 <div class="col-md-4 col-md-offset-4">
                            <div class="loader" style="margin-top:10px; margin-left:80px">付款成功</div>
                            <!-- Display the Transaction Details-->
                            <div id="show_transactions">
                                <h4> 
                                    <div id="firstName"></div>
                                    <div id="lastName"></div>谢谢您的订单 
                                </h4>
                                <div><strong>订单编号:</strong> <span id="transactionId"></span> </div>
                                <div><strong>交易类型:</strong><span id="transactionType"></span> </div>
                                <div><strong>总金额:</strong><span id="amt"></span>  </div>
                                <div><strong>货币代码:</strong><span id="currencyCode"></span>  </div>
                                <div><strong>支付状态:</strong><span id="paymentStatus"></dispanv>  </div>
                            </div>
                        
                </div> 
            </div>
        </div>  <!--Container-Fluid ends here -->
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://code.jquery.com/jquery.js"></script>
        
        <script> 
            $( document ).ready(function() {
                // set back url 
                var url = location.pathname.split("/")[1]; url = '/'+url+'/'
                $('#go_back').attr('href',url);
                function qs(key) {
                    key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
                    var match = location.search.match(new RegExp("[?&]"+key+"=([^&]+)(&|$)"));
                    return match && decodeURIComponent(match[1].replace(/\+/g, " "));
                }
                var orderId = qs("orderId");
                $.post('<%=request.getContextPath() %>/paypalC/paymentDetails?orderId='+orderId, function( data ) {
                        $("#transactionId").html(data.id);
                        $("#transactionType").html(data.intent);
                        $("#amt").html(data.purchase_units[0].amount.value);
                        $("#currencyCode").html(data.purchase_units[0].amount.currency_code);
                        $("#paymentStatus").html("COMPLETED" == data.status ? "交易完成" : "交易失败");
                });
            });
        </script> 
    </body>
</html>
View Code

posted on 2020-10-24 09:53  码农记录  阅读(6497)  评论(0编辑  收藏  举报

导航