1.0.1 • Published 1 year ago

@activity-maker/payment-process v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

电商中台支付相关 SDK

公共类型

Env

运行环境

说明
test测试环境
uatUAT 环境
production生产环境

Goods

商品

参数说明类型必填默认值
itemId商品 IDstring-
quantity商品数量string-
extension商品扩展字段Extension-

Extension

商品扩展字段

参数说明类型必填默认值
sourceType商品来源类型string-
sourceValue商品来源值string-

Domain

下单用户来源

说明
1喜马拉雅
2开放平台
3知识宝

TradeType

交易类型

说明
1消费
2充值
4拼团
5买赠

Context

附带的业务参数,下单时会透传。object 对象,key 为 string,值为 any

支付

Author: haoran.zhang

基本用法

import React, { useEffect } from 'react';
import { Button, message } from 'antd';
import { checkWxAuth, getWxAuth, payment, PaymentProcessResponse } from '@activity-maker/payment-process'

export default () => {

  useEffect(() => {
    if (!checkWxAuth(17)) {
      getWxAuth(17).then(value => {
        // value: { ret: 0, msg: '微信授权成功' }
        // 静默授权成会自动跳转到授权链接
        if(value.ret === 0) {
          message.success(value.msg)
        }
      }).catch(reason => {
        // reason: new Error('微信授权失败')
        message.warn(reason.toString())
      })
    }
  }, [])

  const handlerClick = () => {
    payment.wechatAppPay({}, (res: PaymentProcessResponse) => {
      if(res.ret !== 0) {
        message.warn(res.msg)
      }
    })
    // ret为0,支付成功,自动跳转支付成功页
    // ret为1, 支付未成功,msg: "不在微信环境内" | "支付参数错误" | "支付取消"
    //  | "支付失败" | "微信支付参数错误" | "未知错误"
  }


  return (
    <Button type="primary" onClick={handlerClick}>
      点击微信app内支付
    </Button>
  )
};

thirdPartyId对照表

http://gitlab.ximalaya.com/x-fm/xpassport/wikis/thirdpartyid%E5%AF%B9%E7%85%A7%E8%A1%A8

API

const result: boolean = checkWxAuth(thirdPartyId: number);

const result: Promise = getWxAuth(thirdPartyId: number);

interface PaymentProcessResponse {
  ret: number;
  msg: string;
  data?: any;
}

payment.wechatAppPay(
	payInfo: any,  // 支付接口返回参数,对象类型
	callback: (res: PaymentProcessResponse) => void  // 支付回调
)

结算页

Author: Eleven

封装前往结算页

前往 H5 结算页

import React from 'react';
import { Button } from 'antd';
import { openSettlement, OpenSettlementOptions } from '@activity-maker/payment-process';

export default () => {
  const handlerClick = () => {
    const orderItems = [
      { itemId: '1010000100000452555', quantity: '1' },
      { itemId: '1011700100000120855', quantity: '1' },
      { itemId: '1010000100000551820', quantity: '1' },
      { itemId: '1011700100000222903', quantity: '1' },
      { itemId: '1034200100000226541', quantity: '1' },
      { itemId: '1010000100000550918', quantity: '1' },
    ];
    const params: OpenSettlementOptions = {
      orderItems,
      // 测试的应用标识
      appIdentifier: '9ruj3ol12h9t94kb89bjf0tobunjfurf',
    };

    openSettlement(params, 'h5', false, 'test');
  };

  return (
    <Button type="primary" onClick={handlerClick}>
      点击跳转 H5 结算页
    </Button>
  );
};

前往 RN 结算页

import React from 'react';
import { Button, message } from 'antd';
import { openSettlement } from '@activity-maker/payment-process';

export default () => {
  const handlerClick = () => {
    const isXmlyApp = /iting/i.test(window.navigator.userAgent);
    const orderItems = [
      { itemId: '1010000100000452555', quantity: '1' },
      { itemId: '1011700100000120855', quantity: '1' },
      { itemId: '1010000100000551820', quantity: '1' },
      { itemId: '1011700100000222903', quantity: '1' },
      { itemId: '1034200100000226541', quantity: '1' },
      { itemId: '1010000100000550918', quantity: '1' },
    ];
    const params: OpenSettlementOptions = {
      orderItems,
      // 测试的应用标识
      appIdentifier: '9ruj3ol12h9t94kb89bjf0tobunjfurf',
    };

    if (!isXmlyApp) {
      message.warn('请在喜马拉雅 App 内测试');
      return;
    }
    openSettlement(params, 'rn');
  };

  return (
    <Button type="primary" onClick={handlerClick}>
      点击前往 RN 结算页
    </Button>
  );
};

仅返回 URL,不跳转

import React, { useState } from 'react';
import { Button, message } from 'antd';
import { openSettlement } from '@activity-maker/payment-process';

export default () => {
  const [url, setUrl] = useState<string>();

  const handlerClick = () => {
    const isXmlyApp = /iting/i.test(window.navigator.userAgent);
    const orderItems = [
      { itemId: '1010000100000452555', quantity: '1' },
      { itemId: '1011700100000120855', quantity: '1' },
      { itemId: '1010000100000551820', quantity: '1' },
      { itemId: '1011700100000222903', quantity: '1' },
      { itemId: '1034200100000226541', quantity: '1' },
      { itemId: '1010000100000550918', quantity: '1' },
    ];
    const params: OpenSettlementOptions = {
      orderItems,
      // 测试的应用标识
      appIdentifier: '9ruj3ol12h9t94kb89bjf0tobunjfurf',
    };

    const url = openSettlement(params, 'h5', true, 'test');

    setUrl(url);
  };

  return (
    <>
      <Button type="primary" onClick={handlerClick}>
        点击获取前往 H5 结算页地址
      </Button>
      <div style={{ wordBreak: 'break-all', padding: '8px' }}>{url}</div>
    </>
  );
};

API

前往结算页

const result: undefined | string = openSettlement(options: OpenSettlementOptions, type: 'h5' | 'rn' = 'h5', onlyUrl: boolean = false, env?: Env);

参数

参数说明类型必填默认值
options结算页参数OpenSettlementOptions-
type类型,H5 或 RN'h5' \| 'rn''h5'
onlyUrl是否仅返回 url,不跳转booleanfalse
env指定环境,仅 H5 结算页有效,RN 结算页不需要区分打开环境。若未指定,将自动根据喜马拉雅的域名规则,运行时判断。Env-

OpenSettlementOptions

参数说明类型必填默认值
orderItems商品列表Goods[]-
appIdentifier应用标识string-
domain下单用户来源Domain1
returnUrl支付成功后跳转地址(无需 encode,函数内部会自动做 encode 处理)stringwindow.location.href
context附带的业务参数,下单时会透传Context-
tradeType交易类型TradeType1
tradeOrderNo交易订单号(通常不需要,后端自动生成,但是,先拿订单号再下单时必填)string-
buyVip如果希望会员引导购买条,初始状态下是勾选,传 truebooleanfalse

返回值

当参数 onlyUrl 为 true 时,返回跳转结算页的 url,方便某些场景处理跳转(如:微信授权登录成功后,直接跳往结算页)。

订阅

Author: haoran.zhang

文档

https://notes.dingtalk.com/doc/Y7kmblblZPAN3zLq?orgId=4730678&dd_progress=false&showmenu=false