前言:

芝麻 GO 是支付宝给商家提供的一款助力商家进行拉新,促进下单的营销工具。对于用户是先享权益,后承诺任务,只需要签约一份协议就能开始一个任务。

说实话,很久没有对接支付宝的产品,在查看文档时,我一度以为芝麻 GO 的先享权益发出去的钱也只不过先冻结一笔金额,等待任务结束金额才会真正到用户钱包。

可实际却是用户先享权益的业务功能是由商家自行实现,比如发现金,发业务券包等,最后,商家根据任务的进度可以对发出给用户的金额进行扣回 (会从用户支付宝的余额或银行卡中扣钱)。

说到这里,我一开始也很不能理解,就比如发现金,我们是通过企业付款到用户的方式打款。如果用户一收到金额就把现金花完了,那后面又怎么扣回呢,问过阿里工作人员说,如果用户支付宝都没有钱,就会一直等待用户卡里或余额里有金额再扣除,以下就从简单的几个例子介绍芝麻 GO 的对接。

 

流程:

1. 前端配置活动入口,芝麻 GO 签约。
2. 签约成功回调,优惠数据单独回传,业务操作入库。
3. 监听协议到期或退出的回调。
4. 任务数据回传,位于业务某个节点,比如打款回调位置。
5. 协议到期或退出,对协议进行结算,比如扣回等操作。


数据库设计:

CREATE TABLE `zmgo_agreement` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `out_request_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 接口请求号 ',
  `redpack_id` int (11) DEFAULT '0' COMMENT ' 现金红包 ID',
  `user_id` int (11) DEFAULT '0' COMMENT ' 用户 ID',
  `open_id` varchar (200) CHARACTER SET utf8 DEFAULT '' COMMENT ' 支付宝授权 ID',
  `real_name` varchar (30) CHARACTER SET utf8 DEFAULT '' COMMENT ' 支付宝用户实名 ',
  `user_mobile` varchar (20) CHARACTER SET utf8 DEFAULT '' COMMENT ' 用户手机号 ',
  `template_id` varchar (150) CHARACTER SET utf8 DEFAULT '' COMMENT ' 模板 ID',
  `partner_id` varchar (80) CHARACTER SET utf8 DEFAULT '' COMMENT ' 协议签约商户 ID',
  `agreement_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 签约协议号 ID',
  `biz_type` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 业务类型 ',
  `alipay_user_id` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 阿里用户 ID',
  `freeze_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 冻结金额 ',
  `type` varchar (50) CHARACTER SET utf8 DEFAULT '' COMMENT ' 玩法类型: NORMAL: 普通版  CYCLE_WITHHOLD: 周期扣版 ',
  `exp_invalid_time` varchar (120) CHARACTER SET utf8 DEFAULT '' COMMENT ' 预期失效时间 ',
  `sign_time` varchar (120) CHARACTER SET utf8 DEFAULT '' COMMENT ' 签约时间 ',
  `start_time` varchar (120) CHARACTER SET utf8 DEFAULT '' COMMENT ' 协议开始时间 ',
  `agreement_status` int (2) DEFAULT '0' COMMENT ' 协议状态: 0. 未签约 1. 协议生效中 2. 协议已到期未结算 3. 协议已失效 ',
  `pre_withdraw_status` int (2) DEFAULT '0' COMMENT ' 企业打款用户状态: 0: 提现中;1: 提现成功;2: 提现失败 ',
  `withhold_plan_no` varchar (150) CHARACTER SET utf8 DEFAULT '' COMMENT ' 用户协议还款计划单据号 ',
  `changed_type` int (2) DEFAULT '0' COMMENT ' 变更类型: 0. 未签约 1. 协议退出  2. 协议失效时间延期 ',
  `gmt_changed` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 协议变更时间 ',
  `update_time` int (11) DEFAULT '0' COMMENT ' 更新时间 ',
  `create_time` int (11) DEFAULT '0' COMMENT ' 创建时间 ',
  `delete_time` int (11) DEFAULT '0' COMMENT ' 删除时间 ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8mb4 COMMENT=' 签约表 ';
CREATE TABLE `zmgo_agreement_changed` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `agreement_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 芝麻 GO 协议号 ',
  `agreement_status` int (2) DEFAULT '0' COMMENT ' 协议状态:  0. 未签约 1. 协议生效中 2. 协议已到期未结算 3. 协议已失效 ',
  `changed_type` int (2) DEFAULT '0' COMMENT ' 变更类型: 0. 未签约 1. 协议退出  2. 协议失效时间延期 ',
  `quit_type` int (2) DEFAULT '0' COMMENT ' 协议退出类型: 0. 未签约 1. 到期取消退出 2. 用户取消退出 3. 结算申请退出 4. 扣款超时退出 ',
  `quit_point` int (2) DEFAULT '0' COMMENT ' 退出节点: 0. 未签约 1. 任务期退出 2. 续订期退出 3. 到期退出 ',
  `create_time` int (11) DEFAULT '0' COMMENT ' 变更时间 ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COMMENT=' 协议变更表 ';
CREATE TABLE `zmgo_cumulate` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `biz_action` int (2) DEFAULT '1' COMMENT '1. 正向回传 2. 逆向回传 ',
  `agreement_no` varchar (200) CHARACTER SET utf8 DEFAULT '' COMMENT ' 协议号 ',
  `ali_user_id` varchar (120) CHARACTER SET utf8 DEFAULT '' COMMENT ' 支付宝用户 ID',
  `user_id` int (11) DEFAULT '0' COMMENT ' 用户 ID',
  `out_biz_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 业务单号 ',
  `refer_out_biz_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 逆向对应的正向外部业务号 ',
  `sub_biz_action` int (2) DEFAULT '1' COMMENT '1. 新增 2. 修改 ',
  `data_type` int (2) DEFAULT '1' COMMENT ' 回传数据类型: 1. 进度型 2. 优惠型 ',
  `task_desc` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 任务描述 ',
  `task_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 用户与商户交易金额 ',
  `trade_no` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 交易单号 ',
  `discount_desc` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 优惠信息描述 ',
  `discount_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 优惠金额 ',
  `remark` varchar (255) CHARACTER SET utf8 DEFAULT '' COMMENT ' 备注 ',
  `create_time` int (11) DEFAULT '0' COMMENT ' 创建时间 ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=' 数据回传表 ';
CREATE TABLE `zmgo_finance_preorder` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `agreement_no` varchar (150) NOT NULL DEFAULT '' COMMENT ' 芝麻 GO 协议 ID',
  `title` varchar (50) DEFAULT NULL COMMENT ' 标题 ',
  `finance_id` varchar (50) NOT NULL DEFAULT '0' COMMENT ' 唯一流水号 ',
  `pay_no` varchar (50) NOT NULL DEFAULT '0' COMMENT ' 支付宝或微信流水号 ',
  `user_id` int (11) NOT NULL DEFAULT '0' COMMENT ' 用户的 id',
  `real_name` varchar (100) DEFAULT NULL COMMENT ' 用户真实姓名 ',
  `mobile` varchar (50) DEFAULT '' COMMENT ' 手机号 ',
  `open_id` varchar (100) DEFAULT '' COMMENT ' 支付宝 openID',
  `type` tinyint (3) DEFAULT '2' COMMENT ' 提现类型  1 微信 2 支付宝 ',
  `amount` decimal (10,2) NOT NULL DEFAULT '0.00' COMMENT ' 金额 ',
  `state` tinyint (4) DEFAULT '0' COMMENT ' 提现状态: 0: 提现中;1: 提现成功;2: 提现失败 ',
  `pay_time` int (11) DEFAULT '0' COMMENT ' 提现时间 ',
  `remark` varchar (255) DEFAULT NULL COMMENT ' 备注说明 ',
  `create_time` int (11) DEFAULT '0' COMMENT ' 创建时间 ',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `user_id` (`user_id`) USING BTREE,
  KEY `finance_id` (`finance_id`) USING BTREE,
  KEY `user_activity_id` (`agreement_no`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COMMENT=' 签约打款记录表 ';
CREATE TABLE `zmgo_settle` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `out_request_no` varchar (200) CHARACTER SET utf8 DEFAULT '' COMMENT ' 业务单号 ',
  `agreement_no` varchar (200) CHARACTER SET utf8 DEFAULT '' COMMENT ' 协议单号 ',
  `alipay_user_id` varchar (100) CHARACTER SET utf8 DEFAULT '' COMMENT ' 支付宝用户 ID',
  `partner_id` varchar (100) CHARACTER SET utf8 DEFAULT '' COMMENT ' 商户 ID',
  `withhold_plan_no` varchar (120) CHARACTER SET utf8 DEFAULT '' COMMENT ' 用户协议还款计划单据号 ',
  `cumulate_json` json DEFAULT NULL COMMENT ' 回传数据查询结果 ',
  `activity_rule` json DEFAULT NULL COMMENT ' 现金礼包配置源数据 ',
  `pay_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 扣除用户金额: 0. 不扣 >0. 扣款金额 ',
  `total_real_pay_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 用户与商家交易金额 ',
  `total_discount_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 任务优惠金额 ',
  `withhold_biz_type` int (2) DEFAULT '0' COMMENT '0. 未结算 1. 会员费结算 2. 周期扣 3. 续订担保期扣款 4. 续定期扣款 5. 扣款超时,转为还款计划 ',
  `biz_type` varchar (50) CHARACTER SET utf8 DEFAULT '' COMMENT ' 当前业务订单归属的协议类型 ',
  `rest_freeze_amount` decimal (10,2) DEFAULT '0.00' COMMENT ' 剩余冻结金额 ',
  `withhold_task_period` varchar (100) CHARACTER SET utf8 DEFAULT '' COMMENT ' 任务阶段:0. 未知 1. 任务期 2. 续订期 ',
  `trans_status` int (2) DEFAULT '0' COMMENT ' 支付结算状态: 0. 未知 1. 成功 2. 失败 ',
  `update_time` int (11) DEFAULT '0' COMMENT ' 更新时间 ',
  `create_time` int (11) DEFAULT '0' COMMENT ' 创建时间 ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=' 结算记录表 ';

四个请求:

1. 数据回传返回结果:
接口:ZhimaMerchantZmgoCumulateSyncRequest

{
    "zhima_merchant_zmgo_cumulate_sync_response": {
        "code": "10000",
        "msg": "Success",
        "agreement_id": "ZMGO_AGR2022111010020604080046634028",
        "out_biz_no": "P2022111016116529"
    },
    "alipay_cert_sn": "3cba789ec6cb878bb3c09c936ddf177c",
    "sign": "WP+CBTWAkCj/t0xawBEhYzXYuW4YVwlstuipry2K3fPM+ZFi9vdmZtoR/3MkawCXjHjv/VttJ/H1DSARucf8/vV+5UXEdWnLaNLY551eKou2Hum1xxmlMohCDFp7SsmlhXeyosEjc3AzPIP2Dh7d/M2A2lIx/aoVeuw4277OpBNNI+6jLavn7aRhQHRZZTdJVd0J0emZUonA8FSj6iaCnOtHvs3vag6eltJ3M42K5YOlQVT0CHxK/vXtTWvsE2a5Us0n6DCH6s7UMpgmCmmJhiqec1Cuz4P22oHNauelmtqawFA+8ZfoXgc0zxaCj1x3J+1XbT7zc4enXjVrbZza9g=="
}

2. 数据回传查询返回结果:
接口:ZhimaMerchantZmgoCumulateQueryRequest

{
    "aggr_amount": "600.00",
    "aggr_discount_amount": "30.00",
    "aggr_times": 0,
    "agreement_id": "ZMGO_AGR2022111010020604080046634028",
    "detail_list": [
        {
            "action_type": "POSITIVE",
            "biz_time": "2022-11-10 16:11:35",
            "data_type": "TASK",
            "discount_amount": "58.00",
            "discount_desc": "58 元现金红包",
            "out_biz_no": "P2022111016116529",
            "sub_data_type": "AMOUNT",
            "task_amount": "600.00",
            "task_desc": "完成了一笔交易",
            "task_times": 0
        }
    ]
}

3. 商家主动解约协议返回结果 (会触发回调):
接口:ZhimaCreditPeZmgoAgreementUnsignRequest

{
    "zhima_credit_pe_zmgo_agreement_unsign_response": {
        "code": "10000",
        "msg": "Success",
        "agreement_id": "ZMGO_AGR2022111010020604080046634028",
        "withhold_plan_no": "ZMGO_WHD2022111010020603080063837101"
    },
    "alipay_cert_sn": "3cba789ec6cb878bb3c09c936ddf177c",
    "sign": "nqX578EJ42LwYo04gfNf8sfPGzwp1T4aBtMgDFRyo1fHpMykLEEq/pmR6LseLWjWtC76mEwnJn3rq4u7dQ3lBpvtkFQC2EsCrmAqA9InBZ6CpU7gwzbD3mtc8h2nVoJg61TI8E6uR2VEGYMBB3b3359J5DFn86jeyO+7PspoKDtqXCfQ31wnO3QE4KOjELQEAAEerVfP86Rna4j/+pZ20pJwmFRIdgBZ5C8bKvAAdmuFdj607tgDV+qu+0iekGhs+H96j38qjdz98MFCztkeyP/q2ulDjM9gYhYFM8Cd/nFlKRMyG5cQMeWhR6Pfa5Ah7XEmSrsRuNqk+ENqnQmp7w=="
}

4. 结算申请返回结果 (会触发结算回调):
接口:ZhimaCreditPeZmgoAgreementUnsignRequest

{
    "zhima_credit_pe_zmgo_settle_apply_response": {
        "code": "10000",
        "msg": "Success",
        "agreement_id": "ZMGO_AGR2022111010020604080046634028",
        "out_request_no": "2022111019333259",
        "settle_status": "P",
        "withhold_plan_no": "ZMGO_WHD2022111010020603080063837101"
    },
    "alipay_cert_sn": "3cba789ec6cb878bb3c09c936ddf177c",
    "sign": "XDnOpAq4qFF3ErmMyIHl4pphVpy2RWGeb7QGDXUToy0SKjK4YsL50njYqQU7CGb6Z5hHDjpaP8bVMjEZHF8UXvG5YxRhDxfD8QUpbSnEvSLImv8zCaSUi3/qfXlPliraybXMO2G3LHl6dIaer34MD2SykXH1CRskD39DEZzNDr1eb0Z+VaWpgTuL+Tb8C/ur9t5RZxnbSFFv8fvCeAQ3j4am91sfXsnQ/PmM3AqNtI3cG04u+Bpndpsb8f5BtKoN6jGNS0H6Ka6fneoBNfz0ibA4Inz4wBjovMpM6Qk/BTWnQPSYL/cPNOFVQdRphnfgT0yihGFjnGWtJXHhWZvNrQ=="
}


三个回调:

1. 芝麻 GO_签约变更回调:

array (
  'charset' => 'UTF-8',
   
  'biz_content' => '{
        "freeze_amount":"30.00",
        "product_version":"zhimGoV2",
        "total_periods":"0",
        "biz_type":"huanhuanhuishou",
        "alipay_user_id":"2088112611324801",
        "cycle_withhold":"N",
        "type":"NORMAL",
        "exp_invalid_time":"2022-11-16 23:59:59",
        "sign_time":"2022-11-10 11:28:50",
        "start_time":"2022-11-10 11:28:50",
        "partner_id":"16546461211",
        "agreement_id":"ZMGO_AGR2022111010020604800022190623",
        "zmgo_opt_no":"ZMGO_OPT2022111010020607800033971182",
        "agreement_status":"Y",
        "template_id":"ZMGO_TPL202211091002000131300",
        "out_request_no":"ZMG2022111011251265",
        "trans_status":"Y"
    }',
   
  'utc_timestamp' => '1668050931661',
  'app_id' => '156461456156',
  'version' => '1.1',
  'sign_type' => 'RSA2',
  'notify_id' => '2022111000262112851056307524605182',
  'msg_method' => 'zhima.credit.pe.zmgo.sign.notify',
)

2. 芝麻 GO_协议变更回调:

array (
  'charset' => 'UTF-8',
  'biz_content' => '{
        "changed_content":"{}",
        "product_version":"zhimGoV2",
        "agreement_id":"ZMGO_AGR2022111010020604800022190623",
        "gmt_changed":"2022-11-10 11:40:13",
        "biz_type":"huanhuanhuishou",
        "alipay_user_id":"2088112611324801",
        "agreement_status":"P",
        "quit_type":"SETTLE_APPLY_QUIT",
        "quit_point":"EXPIRE_PERIOD",
        "withhold_plan_no":"ZMGO_WHD2022111010020603800038996544",
        "changed_type":"quit"
    }',
  'utc_timestamp' => '1668052343953',
  'app_id' => '125656644511',
  'version' => '1.1',
  'sign_type' => 'RSA2',
  'notify_id' => '2022111000262114013056310722707218',
  'msg_method' => 'zhima.credit.pe.zmgo.agreement.changed',
)

3. 芝麻 GO_结算变更回调:

array (
  'charset' => 'UTF-8',
  'biz_content' => '{
    "product_version":"zhimGoV2",
    "withhold_biz_type":"ZMGO_MEMBERSHIP_FEE",
    "pay_amount":"0.00","biz_type":"huanhuanhuishou",
    "alipay_user_id":"2088712299951081",
    "rest_freeze_amount":"0.00",
    "withhold_plan_no":"ZMGO_WHD2022111010020603080063837101",
    "total_period":"1",
    "withhold_task_period":"TASK_PERIOD",
    "current_period":"1",
    "partner_id":"16546461211",
    "agreement_id":"ZMGO_AGR2022111010020604080046634028",
    "agreement_status":"N",
    "biz_time":"2022-11-10 19:33:19",
    "out_request_no":"2022111019333259",
    "trans_status":"Y"
  }',
  'utc_timestamp' => '1668080120303',
  'sign' => 'oeqBdg7v+JFAVyb6L5+0flyQtx8b+iyNIzeiU/sc0DL5LTODveLKSuDdIrC2RXd9UpsUXY7IwZLPqGHmWYz/0dXua0r+/WLYBh2Bq1oGtOXYpzzNmIdlMUEE/NfFs+QS3ZASoJrOyvCf9n35Kug7UnwaupO4d80+6VI+sfoWHb4LzBZ2rB4DmbcIlK3brAVvc5sgXPK9wcZ9z0KM7W3ARelCGtrAVIMrqiyKUecVav8P4Swl6apviN5DYKKCRQ27fn3rPqOB7nx1Xvx2Vo0F1yZHC4QGUeHeNtAXokPNwTrqSo6Gvx8ZsMHaCcIBtEZo2Znldp+OKk4nsPziffcvcg==',
  'app_id' => '156461456156',
  'version' => '1.1',
  'sign_type' => 'RSA2',
  'notify_id' => '2022111000262193320018695146401913',
  'msg_method' => 'zhima.credit.pe.zmgo.settle.notify',
)

特别注意:

协议状态
agreement_status: Y: 协议生效中 P: 协议已到期未结算 N: 协议已失效

业务操作状态
trans_status: Y 表示成功结束;N 表示失败结束。

关于对接芝麻 GO 的几点问题_芝麻GO