About Mini Program Access to Alipay and WeChat Pay

I have done H5 payment and recently I am doing payment related to mini programs, so I will take the opportunity to sort out a wave

First of all, here we are using onemipay

First download the following class package in composer

Then we will write about WeChat payment and Alipay payment, and we can encapsulate these methods. Let's talk about accessing WeChat payment first

1: First, have the corresponding APP_ID, SECRET, MCH_ID, API_KEY of the current applet, and remember to log in to the backend of the merchant to add the corresponding authorization callback directory

Here, the payment gateway is constructed first, and the H5 payment, applet payment, APP, and payment in the project can be extended based on this. There are currently only two payment methods: Alipay and WeChat payment

2: Note that the payment method of the mini program is WechatPay_Js payment method, set the app_id, mch_id _api_key and other information here, yes, there is a WeChat callback address again, don’t forget to set it

3: The next step is Alipay payment, construct the corresponding gateway, and set relevant information. Because Alipay payment is performed in the applet, it cannot directly jump to Alipay, so here is to generate the corresponding payment link and pay to Alipay. One thing is, because the link in the project is too long (request parameter/user identification token), a short connection conversion can be performed. This part can be processed on the front end or on the back end, depending on the actual situation of the project. .

payment interface sticker code

 /**
     * pay
     * @param Request $request
     */
    public function pay(Request $request)
    {
        if (!$request->has('order_id'))
            return new TheParameterIsEmpty();

        $au_id = $request->get('au_id');

        $orderId = $request->input('order_id');

        $payGenre = $request->input('pay_gateway');

        $WeChat_type = $request->input('WeChat_type', 2);//默认 JSAPI

        $return_url = $request->input('return_url', '');

        $order = ActivityOrder::getActivityOrder($orderId, $au_id);

        if (!$order instanceof ActivityOrder) return response_json(11029, config('code.11029'));

        $ActivityMarketing = ActivityMarket::getActByAmId($order->am_id);

        if (!$ActivityMarketing instanceof ActivityMarket) return response_json(11010, config('code.11010'));

        if ($ActivityMarketing->join_stint != 0 && $ActivityMarketing->join_stint <= $ActivityMarketing->join_stint_num)
            return response_json(11011, config('code.11011'));

        ActivityOrderRepository::setPayGenre($order, $payGenre);

        $gateway = PayGateFactory::getPayGate($payGenre);


        try {
            return $this->getPayParameter($order, $gateway, $return_url ,$WeChat_type);
        } catch (Exception $exception) {
            Log::error($exception);
            return response_json(500, 'Server Error');
        }
    }

    /**
     * @param ActivityOrder $order
     * @param GatewayInterface $gateway
     * @param $return_url //Alipay webpage payment successful jump address
     * @param $WeChat_type 2:JSAPI 3:H5
     * @return \Illuminate\Http\JsonResponse
     */
    protected function getPayParameter(ActivityOrder $order, GatewayInterface $gateway, $return_url, $WeChat_type)
    {

        $pay_type = $gateway instanceof AliPay ? 'AliPay' : 'WeChatPay';

        $parameter = 1;
        if ($pay_type == 'AliPay'){
            $parameter = 2;//Alipay web page payment
        } elseif ($pay_type == 'WeChatPay') {
            $parameter = $WeChat_type;
        }

        $gateway->getGateway($parameter);

        $gateway->setNotifyUrl('shop-ay/ny');

        if ($pay_type == 'AliPay') $gateway->setReturnUrl($return_url);

        $gateway->setGatewayOrder($order);

//        if ($pay_type == 'WeChatPay') $gateway->setTimeExpire(date('yymdHms', time()+300));

        $data = $gateway->response();

        return response_json($data);
    }

Finally, if successful, the request return should be

Note: When the front-end adjusts WeChat payment, the order of the fields should be the same as that of the back-end encryption. Otherwise, there will be problems.

ok that's it

Guess you like

Origin blog.csdn.net/qq_42082023/article/details/125065018