2Checkout Integration

2Checkout 是国外一家做信用卡支付的公司,其主要做系统的解决方案,也提供了 API 的集成,本文将主要介绍 2CheckoutAPI 集成方案。

定位

之前我们有讲到 Stripe 也是国外一家做信用卡支付的公司, 那么两者之间和国外其他两家做支付的区别在哪呢

PayPal PayDollar 2Checkout Stripe
定位 自有支付平台 融合支付平台 信用卡支付平台 信用卡支付平台
集成方案 PayPal PayDollar 2Checkout Stripe
优点 类支付宝 可以对接一些第三方支付服务,支付宝/银联/微信 安全性高适用于国外信用卡 文档全
缺点 类支付宝 受政策和商务影响较多 不支持国内信用卡 安全性查, 无法防止盗刷

集成方案

2Checkout 集成方案可以分为两种

  • 一站式集成, 调用 2Checkout 支付界面完成支付, 通知我们
    • 优点 : 简单, 快捷
    • 缺点 : 无法获取用户的支付信息, 无法定制化支付界面
  • API 式集成, 前端收集支付信息, 由后端调用 2Checkout Rest API 完成支付, 通知我们
    • 优点 : 灵活, 可定制化前端支付界面以及获取用户信息
    • 缺点 : 需要对支付环节进行二次开发, 较复杂

接下来, 我们都说说这两种是怎么集成的

前提准备

首先 请确认你已经有了 2Checkout 的账号

一站式集成

Inline Checkout Guide

前端集成

Inline Checkout JS 参数

在前端引入 2Checkout JS 时, 注意 如果我们不使用 2Checkout 的产品概念, 请设置 app.mode 为 DYNAMIC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
(function (document, src, libName, config) {
var script = document.createElement('script');
script.src = src;
script.async = true;
var firstScriptElement = document.getElementsByTagName('script')[0];
script.onload = function () {
for (var namespace in config) {
if (config.hasOwnProperty(namespace)) {
window[libName].setup.setConfig(namespace, config[namespace]);
}
}
window[libName].register();
};

firstScriptElement.parentNode.insertBefore(script, firstScriptElement);
})(document, 'https://secure.avangate.com/checkout/client/twoCoInlineCart.js', 'TwoCoInlineCart',{"app":{"merchant":"XXXXXXX","mode":"DYNAMIC"},"cart":{"host":"https:\/\/secure.2checkout.com","customization":"inline"}});

在实际发起支付的地方, 我们用以下代码唤起支付页面, 而且 2Checkout 提供了事件, 供我们处理前端的跳转, 因此, 我们无需与后台通讯, 即可获取支付状态等.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
TwoCoInlineCart.cart.reloadCart();
// 注册购物车关闭的事件
TwoCoInlineCart.events.subscribe('cart:closed', () => {
console.log('cart:closed triggered');
// debugger;
this.$emit('closeLoading')
});

// 注册支付完成的事件
TwoCoInlineCart.events.subscribe('payment:finalized', () => {
console.log('payment:finalized triggered');
// debugger;
this.$router.push(this.payData.successUrl);
});

TwoCoInlineCart.products.removeAll();
// 设置 购买的产品
TwoCoInlineCart.products.add({
type: "PRODUCT",
name: "Product Name",
price: this.payData.amount,
tangible: "FALSE"
}); // add products to cart
if (localStorage.getItem("lang") == "zh-CN") {
TwoCoInlineCart.cart.setLanguage("ZH");
} else {
TwoCoInlineCart.cart.setLanguage("EN");
}
// 设置 购买的邮箱
//TwoCoInlineCart.billing.setEmail(localStorage.getItem("login_mail"));
// 设置 购买的用户名
//TwoCoInlineCart.billing.setName(localStorage.getItem("login_name"));
// 设置 返回的方法
TwoCoInlineCart.cart.setReturnMethod(this.payData.successUrl);
// 设置 购物车的币种
TwoCoInlineCart.cart.setCurrency('USD');
// 设置 是否为测试订单
TwoCoInlineCart.cart.setTest(!isProdENV());
// 设置 自己业务的订单
TwoCoInlineCart.cart.setOrderExternalRef(this.payData.orderRef);
// 设置 自己业务的数据
TwoCoInlineCart.cart.setExternalCustomerReference(this.payData.remark);
TwoCoInlineCart.cart.setCartLockedFlag(true);
// 唤起购物车界面
TwoCoInlineCart.cart.checkout();

2CO Cart1

2CO Cart2

后端集成

2Checkout 提供 系统回调通知的API URL, 会将返回结果通知给此 API

  • IPN)(Instant Payment Notification/及时付款通知): 此通知和 Paydollar/Paypal 类似, 当客户付款后, 会发起通知, 但当我们完成我们的业务行为后, 需要按照 2Checkout 的协议, 返回一个定义的格式字符串给到 2Checkout 告诉它我们已经做好了
    2CO IPN

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    {
    "COMPANY_D": "",
    "IPN_QTY[]": "1",
    "IPN_DISCOUNT[]": "0.00",
    "IPN_PRICE[]": "16.67",
    "IPADDRESS": "180.168.61.243",
    "FIRSTNAME": "John",
    "COUNTRY_D": "China",
    "IPN_VAT[]": "0.00",
    "COUNTRY": "China",
    "CUSTOMEREMAIL": "xxxx@mail.com",
    "LASTNAME": "Doe",
    "IPN_INFO[]": "",
    "PAYMETHOD": "Visa/MasterCard",
    "REFNOEXT": "15107",
    "IPN_PNAME[]": "XXXXXXX Order 15107",
    "FRAUD_STATUS": "APPROVED",
    "IPN_SHIPPING": "0.00",
    "CBANKACCOUNT": "",
    "PAYMETHOD_CODE": "CCVISAMC",
    "LASTNAME_D": "Doe",
    "CARD_EXPIRATION_DATE": "12/34",
    "IPCOUNTRY": "China",
    "COMPANY": "",
    "HASH": "325b43b1aad14bb78832f222e6c268dd",
    "ZIPCODE_D": "-",
    "ORDERNO": "4",
    "ORDERSTATUS": "COMPLETE",
    "REFUND_TYPE": "",
    "ADDRESS1_D": "-",
    "IPN_DELIVEREDCODES[]": "",
    "FISCALCODE": "",
    "CBANKNAME": "",
    "STATE_D": "-",
    "PHONE": "",
    "STATE": "-",
    "IPN_PROMONAME[]": "",
    "CARD_TYPE": "visa",
    "IPN_TOTALGENERAL": "16.67",
    "REGISTRATIONNUMBER": "",
    "REFNO": "95185621",
    "ADDRESS1": "-",
    "PHONE_D": "",
    "ADDRESS2": "",
    "EXTERNAL_CUSTOMER_REFERENCE": "XXXXXXXXXXXX",
    "CURRENCY": "USD",
    "IPN_TOTAL[]": "16.67",
    "FIRSTNAME_D": "John",
    "CARD_LAST_DIGITS": "1111",
    "SALEDATE": "2019-05-08 09:23:40",
    "ZIPCODE": "-",
    "IPN_COMMISSION": "0.9335",
    "IPN_VER[]": "1",
    "CITY_D": "-",
    "TEST_ORDER": "1",
    "IPN_PCODE[]": "",
    "CITY": "-",
    "IPN_PID[]": "22136385",
    "IPN_DATE": "20190508095004",
    "ADDRESS2_D": "",
    "FAX": "",
    "IPN_SHIPPING_TAX": "0.00"
    }
  • INS)(Instant Notification Service/及时通知服务): 它和 IPN 不太一样的是, 它会按照自己定义好的一些触发器, 例如 Order created/ Fraud status/ Invoice status 发送给预先定义好的通知路径, 他没有要求返回我们是否收到
    2CO INS

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    {
    "sale_id": "250152202687",
    "item_rec_status_1": "live",
    "ship_street_address2": "",
    "item_usd_amount_1": "16.67",
    "md5_hash": "30601638DEF96C18EA0FBFA29AE3C2A7",
    "item_name_1": "XXXXX Order 15107",
    "item_cust_amount_1": "16.67",
    "invoice_list_amount": "16.67",
    "bill_country": "CHN",
    "item_rec_list_amount_1": "",
    "customer_ip_country": "China",
    "invoice_id": "250152202686",
    "ship_name": "Doe John",
    "bill_postal_code": "-",
    "item_count": "1",
    "ship_city": "-",
    "ship_postal_code": "-",
    "recurring": "0",
    "customer_first_name": "John",
    "customer_phone": "",
    "key_count": "55",
    "ship_street_address": "-",
    "message_type": "FRAUD_STATUS_CHANGED",
    "bill_state": "-",
    "bill_street_address2": "",
    "item_type_1": "bill",
    "ship_status": "",
    "payment_type": "credit card",
    "vendor_id": "XXXXXXXXX",
    "customer_email": "xxx@mail.com",
    "invoice_usd_amount": "16.67",
    "invoice_status": "approved",
    "fraud_status": "pass",
    "bill_city": "-",
    "invoice_cust_amount": "16.67",
    "item_recurrence_1": "",
    "customer_ip": "180.168.61.243",
    "ship_state": "-",
    "list_currency": "USD",
    "customer_last_name": "Doe",
    "item_list_amount_1": "16.67",
    "bill_street_address": "-",
    "timestamp": "2019-05-08 09:23:52",
    "item_id_1": "",
    "auth_exp": "2019-05-15",
    "message_description": "Order fraud status changed",
    "vendor_order_id": "15107",
    "message_id": "14",
    "item_duration_1": "Forever",
    "item_rec_install_billed_1": "",
    "cust_currency": "USD",
    "item_rec_date_next_1": "",
    "ship_tracking_number": "",
    "customer_name": "John Doe",
    "sale_date_placed": "2019-05-08 09:23:40",
    "ship_country": "CHN"
    }

API 式集成

Order With Dynamic Products

API 式集成更多的是后端的集成, 这里有参考的链接 AvangateJavaApiSamples