wxpay_app.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class wxpay_app {
  3. public function __construct($payment_info = array())
  4. {
  5. define('WXN_APPID', $payment_info['payment_config']['wx_appid']);
  6. define('WXN_APPSECRET', $payment_info['payment_config']['wx_appsecret']);
  7. define('WXN_MCHID', $payment_info['payment_config']['wx_mch_id']);
  8. define('WXN_KEY', $payment_info['payment_config']['wx_key']);
  9. }
  10. public function get_payform($order_info){
  11. require_once PLUGINS_PATH . '/payments/wxpay_native/lib/WxPay.Api.php';
  12. require_once PLUGINS_PATH . '/payments/wxpay_native/WxPay.NativePay.php';
  13. require_once PLUGINS_PATH . '/payments/wxpay_native/log.php';
  14. //统一下单
  15. $input = new WxPayUnifiedOrder();
  16. $input->SetBody(config('ds_config.site_name') . $order_info['pay_sn'] . '订单');
  17. $input->SetAttach($order_info['order_type']);
  18. $input->SetOut_trade_no($order_info['pay_sn'].'_'.TIMESTAMP);//31个字符,微信限制为32字符以内 TIMESTAMP 用来防止做随机数,用户支付订单后取消,已产生的订单不能重复支付
  19. $input->SetTotal_fee(bcmul($order_info['api_pay_amount'] , 100,0));
  20. $input->SetTime_start(date("YmdHis"));
  21. $input->SetTime_expire(date("YmdHis", TIMESTAMP + 600));
  22. $input->SetGoods_tag("");
  23. $input->SetNotify_url(str_replace('/index.php', '', HOME_SITE_URL) . '/payment/wxpay_app_notify.html');
  24. $input->SetTrade_type('APP');
  25. $result = WxPayApi::unifiedOrder($input);
  26. if (is_array($result) && $result['return_code'] == 'SUCCESS') {
  27. if ($result['result_code'] == 'SUCCESS') {
  28. $result['timestamp'] = TIMESTAMP.'';
  29. ds_json_encode(10000,'',array('content'=> $this->sign_again($result)));
  30. } else {
  31. ds_json_encode(10001,$result['err_code_des']);
  32. }
  33. } else {
  34. ds_json_encode(10001,$result['return_msg']);
  35. }
  36. }
  37. function sign_again($order) {
  38. $values=array();
  39. $values['appid'] = WXN_APPID;
  40. $values['partnerid'] = WXN_MCHID;
  41. $values['prepayid'] = $order['prepay_id'];
  42. $values['package'] = 'Sign=WXPay';
  43. $values['noncestr'] = $order['nonce_str'];
  44. $values['timestamp'] = $order['timestamp'];
  45. ksort($values);
  46. $buff = "";
  47. foreach ($values as $key => $value) {
  48. $buff .= $key . "=" . $value . "&";
  49. }
  50. $string = trim($buff, "&");
  51. $string = $string . "&key=" . WXN_KEY;
  52. $string = md5($string);
  53. //签名步骤四:所有字符转为大写
  54. $result = strtoupper($string);
  55. $values['sign'] = $result;
  56. return $values;
  57. }
  58. }