PHP code example of join / php-payment

1. Go to this page and download the library: Download join/php-payment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

join / php-payment example snippets


class PaymentConfig implements IPaymentConfig{
    // 注入配置
    public static function init(){
        $config  = new self();
        PayFactory::init($config);
    }
    /**
    * 注入配置信息
    * @param string $type
    * @return mixed
    */
    public function getPayConfig(string $type){
        // 方式一:代码里直接编写参数
//        $config = [
//           "wxpay"=>[],
//           "alipay"=>[]
//        ];
        // 方式二:单独配置文件  config/payment.php
        $config = Config::get('payment.');
        return $config[$type];
    }
    /**
     * 获取业务类映射关系
     * @return array|mixed
     */
    public function getBusinessMap(){
        // 具体请映射业务类
        return [
            "recharge_order"=>Model_MemberBalance::class,
        ];
    }
    /**
    * 配置客户端 对应支付通道
    * @param string $client
    * @return string
    */
    public function getPayChannel(string $client): string
    {
        // 客戶端支付方式映射支付渠道
        $channel=[
            PayClient::WEIXIN_MP => PayChannel::WEIXIN_PAY_JS,
            PayClient::WEIXIN_QRCODE => PayChannel::WEIXIN_PAY_NATIVE,
            PayClient::ALI_MP => PayChannel::ALI_PAY_JS,
            PayClient::ALI_PAY_QRCODE => PayChannel::ALI_PAY_NATIVE,
        ];
        return $channel[$client];
    }
}

PaymentConfig::init();

class NotifyController extends BaseNotifyController
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        PaymentConfig::init(); //如果全局钩子函数注入配置则不用写次函数
    }
}

class MyOrder extends BasePayableOrder
{
    // 业务名称
    protected $pk ='order_no';
    /**
     * 因为字段不一样 覆盖父级方法
    * 如果业务类包含:title、amount、order_no 则无需编写次方法
     * @return Model_PayOrder
     */
//    public function CreatePayOrder():Model_PayOrder
//    {
//        $pay_order = new Model_PayOrder();
//        $pay_order['title']= $this['name']; // 支付标题
//        $pay_order['amount']=  $this['price']; // 支付金额
//        $pay_order['business_no']=  $this['order_no']; // 订单号
//        return $pay_order;
//    }
    public function PaySuccess(Model_PayOrder $pay_order)
    {
        // 支付成功后 业务单 后续逻辑
    }
    public function RefundedSuccess(Model_PayOrder $pay_refund_order)
    {
        // 退款成功后 业务单 后续逻辑
    }
}

        // 注入配置参数(全局注入则不用写这个)
        PaymentConfig::init();
        // 方式1:创建自己的业务订单
        // $bus_order =  new MyOrder();
        // $bus_order['name'] ='用户充值';
        // $bus_order['price'] = 11.00;
    
        // 方式2: 从数据库查询 后支付
        $bus_order = MyOrder::get('10001');
        
        //支付客户端类型(就是你想调用哪种支付方式)
        $client = PayClient::WEIXIN_QRCODE; //小程序参数
        $params = [];
        // 获得支付参数
        $res = $bus_order->PayOrder($client, $params);

$params = ['openid'=>$openid];

$params = ['buyer_id'=>$buyer_id];