PHP code example of faysoft / faypay
1. Go to this page and download the library: Download faysoft/faypay 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/ */
faysoft / faypay example snippets
//创建交易
$trade_id = TradeService::service()->create(
1,//金额,单位:分
'测试订单',
array(
array(
'type'=>1,//类型,在业务中定义
'refer_id'=>123,//关联id,可以是用户Id,订单Id等,具体看业务逻辑
)
),
array(//这里是可选参数,可以根据业务逻辑指定需要用到的字段
'return_url'=>UrlHelper::createUrl('test/pay-result'),//支付回调页
)
);
{$base_url}faypay/api/payment/pay?trade_id={$交易ID}&payment_id={$支付方式ID}
return array(
//充值成功事件
\faypay\services\trade\TradePaymentService::EVENT_PAID => array(
array(
'handler'=>function(\faypay\services\trade\TradePaymentItem $tradePaymentItem){
//@todo 业务逻辑
}
),
),
)
//从数据库获取支付方式配置信息
$payment_method = $trade_payment->getPaymentMethod();
//实例化用于支付的支付方式配置模型
$payment_config = new PaymentMethodConfigModel($payment_method['code']);
//设置相关属性
$payment_config->setMchId($payment_method['config']['mch_id'])
->setAppId($payment_method['config']['app_id'])
->setAppSecret($payment_method['config']['app_secret'])
->setKey($payment_method['config']['key'])
;
//获取交易信息
$trade = $trade_payment->getTrade();
//实例化用于支付的交易数据模型
$payment_trade = new PaymentTradeModel();
//设置相关属性
$payment_trade->setOutTradeNo($trade_payment->getOutTradeNo())
->setTotalFee($trade_payment->total_fee)
->setNotifyUrl(UrlHelper::createUrl('api/payment/notify/code/'.$payment_method['code']))
->setBody($trade->body)
->setTradePaymentId($trade_payment->id)
->setReturnUrl($trade->return_url)
->setShowUrl($trade->show_url)
;
//调用支付模块。根据支付方式不同,此方法可能返回json数据,也可能直接跳转到支付页面
PaymentMethodService::service()->buildPay($payment_trade, $payment_config);