美国跨境支付stripe
package com.ourslook.qcure.api.pay;
import com.ourslook.qcure.utils.RRException;
import com.ourslook.qcure.utils.annotation.IgnoreAuth;
import com.ourslook.qcure.utils.pay.stripepay.StripePayUtil;
import com.ourslook.qcure.utils.result.XaResult;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import com.stripe.model.Customer;
import com.stripe.model.Refund;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author dazr
* @version V1.0
*
* 美国跨境支付stripe 支付相关接口
* @date 2019年6月10日 下午1:00:00
*
* 卡支付:https://stripe.com/docs/sources/cards
* api首页:https://stripe.com/docs/api
*
* mvn jar 搜索:com.stripe stripe-java 即可
*
* [没有体验过stripe同学,可以使用modao体验](https://free.modao.cc/me/settings)
*/
@Api(value = "pay_stripe", description = "美国跨境支付strpe", position = 10)
@Controller
@CrossOrigin
@RequestMapping("/api/stripe")
public class ApiStripeController {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 步骤一:客户端初始化代码 创建Source对象
* 文档路径 https://stripe.com/docs/sources/cards#create-source
* 代码片段 https://stripe.com/docs/stripe-js/elements/quickstart
*
*
* 步骤二 Charge the Source 【为Source支付】
* 2.1:Attaching the Source to a Customer 根据客户端source获取客户的详细信息Customer
* 文档路径/代码片段:https://stripe.com/docs/sources/cards#charge-request
* 2.2:Making a charge request to finalize the payment 提出收费要求以完成付款
* 文档路径/代码片段:https://stripe.com/docs/sources/cards#making-a-charge-request-to-finalize-the-payment
*
*
*
* 步骤三 Step 3: Confirm that the charge has succeeded 可以通过自定义回调webhook事件确定是否支付成功
* https://stripe.com/docs/sources/cards#charge-confirmation
* EVENT:charge.succeeded
* EVENT:charge.failed
*
*
* http://127.0.0.1:8001/qcure/api/stripe/creditCardPay
*/
@SuppressWarnings("all")
@ApiOperation(value = "信用卡支付", notes = "Charge the Source【为Source支付】 ,具体分为两步【1: 根据客户端创建的source获取顾客信息 Customer 2:支付成功】;针对:Visa、Mastercard和American Express 支付")
@ResponseBody
@IgnoreAuth
@RequestMapping(value = "creditCardCharges", method = RequestMethod.GET)
public XaResult creditCardPay(
@ApiParam(value = "客户端输入了信用卡账号之后创建的source对象/token对象,详见 文档 【Step 1: Create a Source object】,字段名:stripeSourceId;这里是sourceId或者token都可以; token:tok_KPte7942xySKBKyrBu11yEpf", defaultValue = "src_18eYalAHEMiOZZp1l9ZTjSU0") @RequestParam(value = "stripeSourceId", required = true) String stripeSourceId,
@ApiParam(value = "email,字段名:email,请填写自己的email", defaultValue = "[email protected]") @RequestParam(value = "email", required = false) String email,
@ApiParam(value = "订单号,字段名:orderNo", defaultValue = "QC1234567890") @RequestParam(value = "orderNo", required = false) String orderNo,
HttpServletRequest request
) throws Exception {
XaResult xr = new XaResult<>();
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = StripePayUtil.API_KEY;
//1: 根据客户端source获取到customer对象
Map customerParams = new HashMap();
customerParams.put("email", email);
customerParams.put("source", stripeSourceId);//soureZZZId: 类似 src_18eYalAHEMiOZZp1l9ZTjSU0 ,必须是客户端输入信用卡账号之后客户端获取的原对象的id
Customer customer = null;
try {
customer = Customer.create(customerParams);
} catch (StripeException e) {
e.printStackTrace();
throw new RRException("stripe获取customer对象调用失败,详看日志:" + e.getMessage(), e);
}
//2: 根据customer对象进行支付
Map chargeParams = new HashMap();
// 港币:Amount must be at least 400 cents 美分 4港币; 港币目前至少是400
// 人民币:Amount must convert to at least 400 cents. ¥0.01 converts to approximately $0.01.; 人民币:355分
// 美元:最少 58
// 最终都是依港币进行结算的
chargeParams.put("amount", 58);//必须是整数
chargeParams.put("currency", "usd");//USD 美元、CNY 人民币、HKD港币
chargeParams.put("customer", customer.getId());//类似: cus_AFGbOSiITuJVDs
chargeParams.put("source", stripeSourceId);//类似:src_18eYalAHEMiOZZp1l9ZTjSU0
//下面是可选参数,这里可以添加可选自定义参数,如自定义:order_no
Map metadata = new HashMap<>();
metadata.put("order_id", "dazer:99998888");
chargeParams.put("metadata", metadata);
try {
Charge charge = Charge.create(chargeParams);
logger.info("charge对象获取成功,但不一定表示支付成功,虽然大部分情况是支付成功的...");
xr.setObject(charge);
//https://stripe.com/docs/api/charges/object#charge_object-status
if ("succeeded".equalsIgnoreCase(charge.getStatus())) {
//succeeded 支付成功
xr.setMsg("succeeded 支付成功");
} else if ("pending".equalsIgnoreCase(charge.getStatus())) {
//pending 支付结果要继续进行等待
xr.setMsg("pending 支付结果要继续进行等待");
} else {
//failed 支付失败
xr.setMsg("failed 支付失败");
}
} catch (StripeException e) {
e.printStackTrace();
if (StripePayUtil.CODE_AMOUNT_TOO_SMALL.equalsIgnoreCase(e.getCode())) {
throw new RRException("stripe支付金额太小,至少1美分,注意实时汇率;详:" + e.getMessage(), e);
} else {
throw new RRException("stripe调用Charge失败,详看日志:" + e.getMessage(), e);
}
}
return xr;
}
/**
* 退款
* 代码片段/文档:https://stripe.com/docs/refunds#refunds-charges
*
* http://127.0.0.1:8001/qcure/api/stripe/refunds?chargeId=ch_1Elv4YEEsRhJ9o6lPckOzcoc
*/
@ApiIgnore
@SuppressWarnings("all")
@ApiOperation(value = "refunds退款", notes = "refunds退款")
@ResponseBody
@IgnoreAuth
@RequestMapping(value = "refunds", method = RequestMethod.GET)
public XaResult refunds(
@ApiParam(value = "charge对象id,字段名:chargeId,信用卡支付成功获取到的charge对象", defaultValue = "ch_1Elrq4EEsRhJ9o6ldDCqaS36") @RequestParam(value = "chargeId", required = false) String chargeId,
HttpServletRequest request
) throws Exception {
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = StripePayUtil.API_KEY;
XaResult xr = new XaResult<>();
Map params = new HashMap<>();
params.put("charge", chargeId);//eg: ch_IGZpZZVn6H47dNUC2VOU
//params.put("amount", 1000);//要退还部分费用,请提供一个amount参数,以整数美分(或收费货币的最小货币单位)
Refund refund = null;
try {
refund = Refund.create(params);
xr.setObject(refund);
if ("succeeded".equalsIgnoreCase(refund.getStatus())) {
xr.setMsg("退款成功");
} else {
xr.setMsg("退款失败");
}
} catch (StripeException e) {
if (StripePayUtil.CODE_CHARGE_ALREADY_REFUNDED.equalsIgnoreCase(e.getCode())) {
xr.setMsg("退款成功, 您已经退款过了,请不要重复退款!");
} else {
throw new RRException("退款申请失败:" + e.getMessage(), e);
}
e.printStackTrace();
}
return xr;
}
}
stripe 信用卡支付demo - csdn下载地址