Giter VIP home page Giter VIP logo

wechat-pay's Introduction

Wechat Pay

A simple Wechat pay ruby gem, without unnecessary magic or wrapper. Just a simple wrapper for api V3. Refer to wx_pay

Please read official document first: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/pages/index.shtml

If you want check the present public api, you can find them in the Document

Summary:

WechatPay::Direct will contain the public api for direct connection merchant(直连商户)and WechatPay::Ecommerce will contain the public api for ecommerce(服务商,电商平台)。For more detail you can refer to the wechat document.

If you find any issue in this repo, don't shy to create issues https://github.com/lanzhiheng/wechat-pay/issues

For more Information,you can check my posts: https://www.lanzhiheng.com/posts/preview/ruby-gem-for-wechat-pay-v3

Installation

Add this line to your Gemfile:

gem 'wechat-pay'

or development version

gem 'wechat-pay', :github => 'lanzhiheng/wechat-pay'

And then execute:

$ bundle

Usage

Configuration

Create config/initializer/wechat_pay.rband put following configurations into it

WechatPay.apiclient_key = File.read('apiclient_key.pem')
WechatPay.platform_cert = File.read('platform_cert.pem') # You should comment this line before downloaded platform_cert.
WechatPay.apiclient_cert = File.read('apiclient_cert.pem')
WechatPay.app_id = 'Your App Id'
WechatPay.mch_id = 'Your Mch Id'
WechatPay.mch_key = 'Your Mch Key'

Download

I will provide a simple script for you to download the platform_cert

def download_certificate
  download_path = 'Your Download Path'
  raise '必须提供证书下载路径' if download_path.blank?

  response = WechatPay::Ecommerce.certificates

  raise '证书下载失败' unless response.code == 200

  result = JSON.parse(response.body)
  # 需要按生效日期进行排序,获取最新的证书
  array = result['data'].sort_by { |item| -Time.parse(item['effective_time']).to_i }
  current_data = array.first
  encrypt_certificate = current_data['encrypt_certificate']
  associated_data = encrypt_certificate['associated_data']
  nonce = encrypt_certificate['nonce']
  ciphertext = encrypt_certificate['ciphertext']

  content = WechatPay::Sign.decrypt_the_encrypt_params(
    associated_data: associated_data,
    nonce: nonce,
    ciphertext: ciphertext
  )

  File.open(download_path, 'w') do |f|
    f.write(content)
  end

  puts '证书下载成功'
end

wechat-pay's People

Contributors

lanzhiheng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.