WxPay.NativePay.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. *
  4. * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
  5. * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
  6. * 请勿直接直接使用样例对外提供服务
  7. *
  8. **/
  9. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  10. require_once($wx . '/lib/WxPay.Api.php');
  11. require_once($wx . '/example/WxPay.Config.php');
  12. /**
  13. *
  14. * 刷卡支付实现类
  15. * @author widyhu
  16. *
  17. */
  18. class NativePay
  19. {
  20. /**
  21. *
  22. * 生成扫描支付URL,模式一
  23. * @param BizPayUrlInput $bizUrlInfo
  24. */
  25. public function GetPrePayUrl($productId)
  26. {
  27. $biz = new WxPayBizPayUrl();
  28. $biz->SetProduct_id($productId);
  29. try{
  30. $config = new WxPayConfig();
  31. $values = WxpayApi::bizpayurl($config, $biz);
  32. } catch(Exception $e) {
  33. //Log::ERROR(json_encode($e));
  34. }
  35. $url = "weixin://wxpay/bizpayurl?" . $this->ToUrlParams($values);
  36. return $url;
  37. }
  38. /**
  39. *
  40. * 参数数组转换为url参数
  41. * @param array $urlObj
  42. */
  43. private function ToUrlParams($urlObj)
  44. {
  45. $buff = "";
  46. foreach ($urlObj as $k => $v)
  47. {
  48. $buff .= $k . "=" . $v . "&";
  49. }
  50. $buff = trim($buff, "&");
  51. return $buff;
  52. }
  53. /**
  54. *
  55. * 生成直接支付url,支付url有效期为2小时,模式二
  56. * @param UnifiedOrderInput $input
  57. */
  58. public function GetPayUrl($input,$sjExtend)
  59. {
  60. if($input->GetTrade_type() == "NATIVE")
  61. {
  62. try{
  63. $config = new WxPayConfig();
  64. $mid = $sjExtend['wxPayMerchantId'] ?? '';
  65. $appId = $sjExtend['miniAppId'] ?? '';
  66. $key = $sjExtend['wxPayKey'] ?? '';
  67. $config->SetAppId($appId);
  68. $config->SetMerchantId($mid);
  69. $config->SetKey($key);
  70. $result = WxPayApi::unifiedOrder($config, $input);
  71. return $result;
  72. } catch(Exception $e) {
  73. //Log::ERROR(json_encode($e));
  74. }
  75. }
  76. return false;
  77. }
  78. }