PingZfPlatform.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liukun
  5. * Date: 2018/9/28
  6. * Time: 下午12:32
  7. */
  8. namespace battery\portal\model;
  9. use think\Config;
  10. class PingZfPlatform
  11. {
  12. function __construct()
  13. {
  14. vendor('pingzf.init');
  15. \Pingpp\Pingpp::setApiKey(Config::get('pingzf_apikey'));
  16. \Pingpp\Pingpp::setPrivateKeyPath('../data/conf/pingzf_rsa_private_key.pem');
  17. //设置自定义 CA 证书路径
  18. \Pingpp\Pingpp::$caBundle = '/path/to/cacert.pem';
  19. }
  20. function get_client_ipx()
  21. {
  22. $cip = "unknown";
  23. if ($_SERVER['REMOTE_ADDR'])
  24. {
  25. $cip = $_SERVER['REMOTE_ADDR'];
  26. }
  27. elseif (getenv("REMOTE_ADDR"))
  28. {
  29. $cip = getenv("REMOTE_ADDR");
  30. }
  31. return $cip;
  32. }
  33. //支付
  34. function createCharge($pay='',$goodname='',$channel='',$ip,$openid=''){
  35. $pingZFdata=array(
  36. 'order_no' => $pay['order_num'],
  37. 'amount' => $pay['price'],
  38. 'app' => array('id' => Config::get('pingzf_appid')),
  39. 'channel' => $channel,
  40. 'extra' => $channel,
  41. 'time_expire' =>$pay['create_time']+1800 ,//默认30分钟有效期
  42. 'currency' => 'cny',
  43. 'client_ip' => $ip,
  44. 'subject' => $goodname,
  45. 'body' => $goodname
  46. );
  47. switch ($channel){
  48. case 'wx_pub':
  49. $pingZFdata['extra']=array(
  50. 'open_id'=>$openid?$openid:''
  51. );
  52. break;
  53. case 'alipay_wap':
  54. $pingZFdata['extra']=array(
  55. 'success_url'=>$pay['success_url']?$pay['success_url']:''
  56. );
  57. break;
  58. }
  59. $info=\Pingpp\Charge::create($pingZFdata);
  60. return $info;
  61. }
  62. //退款
  63. function createRefund($pay=''){
  64. $jsonRefund=json_decode($pay->success_data,true);;
  65. $ch = \Pingpp\Charge::retrieve($jsonRefund['data']['object']['id']);
  66. $re = $ch->refunds->create(
  67. array('description'=>'退款')
  68. );
  69. return $re;
  70. }
  71. //取消待支付退款
  72. function createReverse($pay=''){
  73. $jsonRefund=json_decode($pay->pay_data,true);;
  74. $charge = \Pingpp\Charge::reverse($jsonRefund['id']);
  75. return $charge;
  76. }
  77. }