引言:
同类文借鉴链接: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/
根据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。