Laravel 调用PING++

本文介绍了一个使用PHP实现的支付系统案例,该系统通过Ping++支付网关支持多种支付方式,包括微信支付、支付宝和银联等。文章详细展示了如何配置支付渠道、处理支付请求及验证回调的安全机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class PayController extends Controller
{
    //支付
    public function index(Request $request){
        \Pingpp\Pingpp::setPrivateKeyPath(__DIR__ . '/your_rsa_private_key.pem');//私钥
        $channel = (INT)(strtolower($request['payType']));//支付类型
 


        //$extra 在使用某些渠道的时候,需要填入相应的参数,其它渠道则是 array() .具体见以下代码或者官网中的文档。其他渠道时可以传空值也可以不传。
        $extra = array();
        switch ($channel) {
            case 1:
                $channel = 'wx';
                $ordertype = 0;
                $extra = array(

                );
//                \Pingpp\Pingpp::setPrivateKeyPath(__DIR__ . '/alipay.pem');
                break;
            case 2:
                $channel = 'alipay';
                $ordertype = 1;
                $extra = array(

                );
                break;
            case 3:
                $channel = 'upacp';
                $ordertype = 2;
                $extra = array(
                    'result_url' => 'http://www.yourdomain.com/result?code='
                );
                break;

        }
        \Pingpp\Pingpp::setApiKey('平台的key');

        DB::table('orders')->where('order_sn',$request['orderNo'])->update(['pay_type'=>$ordertype]);
        try {
            $ch = \Pingpp\Charge::create(
                array(
                    'subject'   => '-'.$request['orderNo'],//交易的title
                    'body'      => '-'.$request['orderNo'],//交易的订单号
                    'amount'    =>$amount ,
                    'order_no'  => $request['orderNo'].rand(1000,9999),//防止订单号重复
                    'currency'  => 'cny',
                    'extra'     => $extra,
                    'channel'   => $channel,
                    'client_ip' => $_SERVER['REMOTE_ADDR'],
                    'app'       => array('id' => '平台appid')
                )
            );
            return json_encode(array('code'=>0,'msg'=>'成功','data'=>json_decode($ch)));
        } catch (\Pingpp\Error\Base $e) {
            header('Status: ' . $e->getHttpStatus());
            echo($e->getHttpBody());
        }

    }

    //支付完事之后的事情
    public function webhooks(Request $request){

        $raw_data = file_get_contents('php://input');//这是在ping++设置的回调传回来的值
        $headers = \Pingpp\Util\Util::getRequestHeaders();
        $signature = isset($headers['X-Pingplusplus-Signature']) ? $headers['X-Pingplusplus-Signature'] : NULL;
        $pub_key_path =__DIR__ . '/public_key.pem';//公钥
        $result = $this -> verify_signature($raw_data, $signature, $pub_key_path);//验证身份
        if ($result === 1) {
            // 验证通过
            $event = json_decode($raw_data, true);
            if ($event['type'] == 'charge.succeeded') {
                $charge = $event['data']['object'];
                $order_sn = $charge['order_no'];//订单号
                $return_num = $charge['id'];//付款成功后的回执
                //更改订单状态
                do something by myself

            }
        } elseif ($result === 0) {
            http_response_code(400);
            echo 'verification failed';
            exit;
        } else {
            http_response_code(400);
            echo 'verification error';
            exit;
        }
    }

    //验证安全
    public function verify_signature($raw_data, $signature, $pub_key_path) {
        $pub_key_contents = file_get_contents($pub_key_path);
        // php 5.4.8 以上,第四个参数可用常量 OPENSSL_ALGO_SHA256
        return openssl_verify($raw_data, base64_decode($signature), $pub_key_contents, 'sha256');
    }

}
官网
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值