微信公衆番号支払い--4-微信返金


公式サイトhttps://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
一、証明書のダウンロード
公衆番号を使用して返金を申請するには、商戸API証明書を検証し、微信商戸プラットフォーム(pay.weixin.qq.com)にログインする必要があります.-->口座センター-->口座設定-->APIセキュリティ、証明書のダウンロード.apiclient_cert.p 12は商家証明書ファイルであり、PHP以外の開発ではこの証明書ファイルが使用されている.
二、証明書の使用
証明書をサービスがアクセスできるディレクトリに配置しますが、次の点に注意してください.
1.証明書ファイルはwebサーバーの仮想ディレクトリに置くことができず、アクセス権制御のあるディレクトリに置くべきで、他人にダウンロードされないようにする.2.証明書ファイル名を複雑で推測しにくいファイル名に変更することを提案する.3.商戸サーバーはウイルスと木馬の防護をしっかりと行い、不法侵入者に証明書ファイルを盗まれないようにしなければならない.
三、主に呼び出されたapi
微信返金:com.lly835.bestpay.service.impl.BestPayServiceImpl.refund(RefundRequest request)
四、コード作成
1、プロファイルの追加
ローカル開発なので、証明書の名前をh 5に変更します.p 12、D:\h 5に置く.p 12パス、アプリケーション.ymlファイルに構成wechatを追加keyPath: D:\\h5.p 12、そして構成をWechatAccountConfigオブジェクトに書き込む
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

    private String mpAppId;
    private String mpAppSecret;
    /**
     *   id
     */
    private String mchId;
    /**
     *     
     */
    private String mchKey;
    /**
     *       
     */
    private String keyPath;
    /**
     *           
     */
    private String notifyUrl;
}

2、構造com.lly835.bestpay.service.impl.BestPayServiceImplインスタンスオブジェクト、spring管理
@Component
public class WechatPayConfig {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Bean
    public BestPayServiceImpl bestPayService () {
        BestPayServiceImpl bestPayService = new BestPayServiceImpl();
        bestPayService.setWxPayH5Config(wxPayH5Config());
        return bestPayService;
    }

    @Bean
    public WxPayH5Config wxPayH5Config() {
        WxPayH5Config wxPayH5Config = new WxPayH5Config();
        wxPayH5Config.setAppId(wechatAccountConfig.getMpAppId());
        wxPayH5Config.setAppSecret(wechatAccountConfig.getMpAppSecret());
        wxPayH5Config.setMchId(wechatAccountConfig.getMchId());
        wxPayH5Config.setMchKey(wechatAccountConfig.getMchKey());
        wxPayH5Config.setKeyPath(wechatAccountConfig.getKeyPath());
        wxPayH5Config.setNotifyUrl(wechatAccountConfig.getNotifyUrl());
        return wxPayH5Config;
    }
}

3、controller
@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     *     (      ,   D:\\h5.p12,      wechat.keyPath: D:\\h5.p12)
     * @param orderDTO
     */
    @PostMapping
    public void refund(OrderDTO orderDTO) {
        payService.refund(orderDTO);
    }
}

4、サービス実装クラス
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【    】refundRequest={}", JsonUtils.toJson(refundRequest));
        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("【    】refundResponse={}", JsonUtils.toJson(refundResponse));
        return refundResponse;
    }

}