Paypal REST API Java 版 PC端商城支付接口對接。

引言:

同類文借鑑鏈接:http://blog.csdn.net/change_on/article/details/73881791(對此博主萬分感謝)

Paypal賬號註冊網址:https://www.paypal.com

Paypal開發者網址:https://developer.paypal.com

Paypal支付演示網址:https://demo.paypal.com

一、註冊公司交易賬戶,也可以作爲開發者賬號使用。

國際註冊網址:https://www.paypal.com

外語不太好的,可以登錄這個網址,親切的漢語。https://www.paypal.com/c2/webapps/mpp/home

二、創建兩個測試sandbox賬號

這個具體可以參考引言中的同類文借鑑鏈接:http://blog.csdn.net/change_on/article/details/73881791

三、選擇您所需的API(本文爲REST-REST-API)

API頁面https://developer.paypal.com/reference/

四、安裝REST SDK配置環境

根據https://developer.paypal.com/docs/api/quickstart/中REST SDK 快速入門步驟操作即可。

創建應用鏈接https://developer.paypal.com/developer/applications,生成所需的Client ID和Secret

五、寫代碼嘍

https://developer.paypal.com/docs/api/payments/此鏈接有請求體和響應體詳情。
https://github.com/paypal/PayPal-Java-SDK此鏈接可以下載java源碼例碼哦。
https://developer.paypal.com/docs/api/quickstart/若不是java語言的可以在此鏈接附加信息中選擇對應語言。

六、本人代碼

1、pom.xml依賴

<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.14.0</version>
</dependency>

2、支付接口

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.PaymentExecution;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;


public class PaypalPayment {

public static final String clientID = "你的clientID";//sandbox
public static final String clientSecret = "你的clientSecret";//sandbox
public static final String mode = "sandbox";//sandbox 沙箱or live生產
public static final String cancelUrl = "https://網站域名/paypal/cancelUrl";//你的真實取消地址
public static final String returnUrl = "https://網站域名/paypal/returnUrl";//你的paypal返回調用地址
public static final String currency = "EUR";
public static final String description = "北極光供訂單";
public static final String method = "paypal";
public static final String intent = "sale";
public static APIContext apiContext = new APIContext(clientID, clientSecret, mode);

    /**Create Payment Object
     * @param total--金額
     * @throws PayPalRESTException 
     */
public static Payment createPayment(String total) throws PayPalRESTException {

Payer payer = new Payer();
payer.setPaymentMethod(method);
Amount amount = new Amount();
amount.setTotal(total);
amount.setCurrency(currency);

Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction.setDescription(description);
List<Transaction> transactions = new ArrayList<Transaction>();
transactions.add(transaction);

RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setCancelUrl(cancelUrl);
redirectUrls.setReturnUrl(returnUrl);


Payment payment = new Payment();
payment.setPayer(payer);
payment.setTransactions(transactions);
payment.setRedirectUrls(redirectUrls);
payment.setIntent(intent);
System.out.println(payment.create(apiContext).toString());
return payment.create(apiContext);
}

    public static Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
        Payment payment = new Payment();
        payment.setId(paymentId);
        PaymentExecution paymentExecute = new PaymentExecution();
        paymentExecute.setPayerId(payerId);
        return payment.execute(apiContext, paymentExecute);
    }


}

3、控制器調用

@Controller
@RequestMapping(value="/paypal")
public class PaypalCommonController extends BaseController {


/**Sofort 支付接口
* @param String orderID;
* @return String paypalPayUrl;
* @throws Exception
*/
@RequestMapping(value="/payment", produces="application/json;charset=UTF-8")
@ResponseBody
public String sofortPayment(String orderID)throws Exception{
String total = "0.01";
Payment payment = PaypalPayment.createPayment(total);
   for(Links links : payment.getLinks()){
      if(links.getRel().equals("approval_url")){
      return links.getHref();//客戶付款登陸地址
      }
   }
return "system/pay/paypal/failedUrl";
}

    @RequestMapping(value="/cancelUrl", produces="application/json;charset=UTF-8")
    public String cancelPay(){
    return "system/pay/paypal/cancelUrl";
    }


/**客戶登陸付款後paypal返回路徑參數示例
*http://域名/paypal/returnUrl?paymentId=PAY-339981922W118522HLJLQF3A&token=EC-9K664484GE997692K&PayerID=LEBMCXS5RQ7AU
*/
    @RequestMapping(value="/returnUrl", produces="application/json;charset=UTF-8")
    @ResponseBody
public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId){
   try {
       Payment payment = PaypalPayment.executePayment(paymentId, payerId);
       if(payment.getState().equals("approved")){
           return "success";
       }
   } catch (PayPalRESTException e) {
       logger.error(e.getMessage());
   }
   return "system/pay/paypal/failed";
}

七、測試、生產

先用創建的沙箱測試賬號測試,沒有問題後,把clientID 、clientSecret換成生產的,mode 由 sandbox換成 live。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章