一:打開支付寶開發平臺,登錄,然後點擊控制台 https://open.alipay.com/ 二:滾動到底部,選著沙箱服務 三:獲取到對接要用的appId和公鑰私鑰 四:打開IDEA導入所需的xml文件 <!-- 支付寶--> <!-- https://mvnrepository.com/arti ...
一:打開支付寶開發平臺,登錄,然後點擊控制台
二:滾動到底部,選著沙箱服務
三:獲取到對接要用的appId和公鑰私鑰
四:打開IDEA導入所需的xml文件
<!-- 支付寶-->
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.0</version>
</dependency>
五:編寫實體類:
import lombok.Data;
@Data
public class Alipay {
private String traceNo;
private String totalAmount;
private String subject;
private String alipayTraceNo;
}
六:編寫yaml文件(這裡的回調要用網路https服務,這裡建議使用花生殼可以完成功能變數名稱訪問!)
#支付寶配置
alipay:
appId: ?
#私鑰
appPrivateKey: ?
#公鑰
alipayPublicKey: ?
#回調地址
notifyUrl: ?
七:編寫服務類:
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AplipayConfig {
private String appId;
private String appPrivateKey;
private String alipayPublicKey;
private String notifyUrl;
@PostConstruct
public void init() {
// 設置參數(全局只需設置一次)
Config config = new Config();
config.protocol = "https";
config.gatewayHost = "openapi.alipaydev.com";
config.signType = "RSA2";
config.appId = this.appId;
config.merchantPrivateKey = this.appPrivateKey;
config.alipayPublicKey = this.alipayPublicKey;
config.notifyUrl = this.notifyUrl;
Factory.setOptions(config);
System.out.println("=======支付寶SDK初始化成功=======");
}
}
八:編寫控制器(寫完控制器建議直接測試訪問看看可不可以訪問的到,鏈接在第九步)
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.rookie.sixthbackstage.entity.Alipay;
import com.rookie.sixthbackstage.entity.OrderItem;
import com.rookie.sixthbackstage.mapper.OrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/alipay")
public class AliPayController {
@Autowired
OrderItemMapper orderItemMapper;
@GetMapping("/alipay") // &subject=xxx&traceNo=xxx&totalAmount=xxx
public String pay(Alipay aliPay) {
AlipayTradePagePayResponse response;
try {
// 發起API調用(以創建當面付收款二維碼為例)
response = Factory.Payment.Page()
.pay(URLEncoder.encode(aliPay.getSubject(), "UTF-8"), aliPay.getTraceNo(), aliPay.getTotalAmount(), "《這裡放支付成功的地址!》");
} catch (Exception e) {
System.err.println("調用遭遇異常,原因:" + e.getMessage());
throw new RuntimeException(e.getMessage(), e);
}
return response.getBody();
}
@PostMapping("/notify") // 註意這裡必須是POST介面
public String payNotify(HttpServletRequest request) throws Exception {
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
System.out.println("=========支付寶非同步回調========");
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
for (String name : requestParams.keySet()) {
params.put(name, request.getParameter(name));
// System.out.println(name + " = " + request.getParameter(name));
}
int tradeNo = Integer.parseInt(params.get("out_trade_no"));
String gmtPayment = params.get("gmt_payment");
String alipayTradeNo = params.get("trade_no");
double pay_price= Double.parseDouble(params.get("buyer_pay_amount"));
// 支付寶驗簽
if (Factory.Payment.Common().verifyNotify(params)) {
// 驗簽通過
System.out.println("交易名稱: " + params.get("subject"));
System.out.println("交易狀態: " + params.get("trade_status"));
System.out.println("支付寶交易憑證號: " + params.get("trade_no"));
System.out.println("商戶訂單號: " + params.get("out_trade_no"));
System.out.println("交易金額: " + params.get("total_amount"));
System.out.println("買家在支付寶唯一id: " + params.get("buyer_id"));
System.out.println("買家付款時間: " + params.get("gmt_payment"));
System.out.println("買家付款金額: " + params.get("buyer_pay_amount"));
// 支付成功後,修改自己的訂單狀態,這裡的方法是我自己寫的,根據自己實際情況來做更改
OrderItem orderItem=new OrderItem(0,tradeNo,0,pay_price,gmtPayment,"","已支付");
orderItemMapper.updateOrderItemById(orderItem);
}
}
return "success";
}
}
九:前端測試
//http://localhost:8089/alipay/alipay?subject=asdflas&traceNo=123123&totalAmount=29999//鏈接,複製到瀏覽器試試看可不可以打開支付寶的頁面
//http://localhost:8089/alipay/alipay/asdflas/123123/29999//傳數據,名稱,訂單號,價格,更多參數請參考官網的文檔!
//結賬按鈕綁定的方法
gopay() { var url = localhost + "/alipay/alipay?subject=" + subject + "&traceNo=" + traceNo + "&totalAmount=" + totalAmount; location.href = url; },
十:結果
十一:想要掃碼支付的要下載沙箱的支付寶,賬號密碼的話在沙箱賬號里,也是沙箱支付寶的登錄賬號!